2012-08-30 67 views
5

मुझे अभी तक इसके लिए एक अच्छी लाइब्रेरी एक्शनस्क्रिप्ट लाइब्रेरी नहीं मिली है।क्या एक्शनस्क्रिप्ट के लिए एक अच्छी स्ट्रिंग बहुवचन पुस्तकालय है?

मैं जैसी चीजों क्या करना चाहते हैं:

Inflection.pluralize("cat") == "cats" 
Inflection.pluralize("fish") == "fish" 

मैं रेल पर रूबी पता की तरह इस में निर्माण एक पुस्तकालय है; और जावास्क्रिप्ट में वास्तव में एक अच्छा भी है: http://code.google.com/p/inflection-js/

कोई भी कार्यवाही के लिए एक ही चीज़ के बारे में जानता है?

+3

अगर आप जावास्क्रिप्ट में स्रोत है, यह बहुत ही में यह बंदरगाह के लिए आसान होना चाहिए। – Daniel

उत्तर

4

गूगल का कहना है: http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/

package 
{ 
public class Inflect 
{ 
    private static var plural : Array = [ 
     [/(quiz)$/i,      "$1zes"], 
     [/^(ox)$/i,      "$1en"], 
     [/([m|l])ouse$/i,    "$1ice"], 
     [/(matr|vert|ind)ix|ex$/i,  "$1ices"], 
     [/(x|ch|ss|sh)$/i,    "$1es"], 
     [/([^aeiouy]|qu)y$/i,   "$1ies"], 
     [/(hive)$/i,      "$1s"], 
     [/(?:([^f])fe|([lr])f)$/i,  "$1$2ves"], 
     [/(shea|lea|loa|thie)f$/i,  "$1ves"], 
     [/sis$/i,      "ses"], 
     [/([ti])um$/i,     "$1a"], 
     [/(tomat|potat|ech|her|vet)o$/i, "$1oes"], 
     [/(bu)s$/i,      "$1ses"], 
     [/(alias|status)$/i,    "$1es"], 
     [/(octop)us$/i,     "$1i"], 
     [/(ax|test)is$/i,    "$1es"], 
     [/(us)$/i,      "$1es"], 
     [/s$/i,       "s"], 
     [/$/i,       "s"] 
    ]; 

    private static var singular : Array = [ 
     [/(quiz)zes$/i,    "$1"], 
     [/(matr)ices$/i,   "$1ix"], 
     [/(vert|ind)ices$/i,  "$1ex"], 
     [/^(ox)en$/i,    "$1"], 
     [/(alias|status)es$/i,  "$1"], 
     [/(octop|vir)i$/i,   "$1us"], 
     [/(cris|ax|test)es$/i,  "$1is"], 
     [/(shoe)s$/i,    "$1"], 
     [/(o)es$/i,     "$1"], 
     [/(bus)es$/i,    "$1"], 
     [/([m|l])ice$/i,   "$1ouse"], 
     [/(x|ch|ss|sh)es$/i,  "$1"], 
     [/(m)ovies$/i,    "$1ovie"], 
     [/(s)eries$/i,    "$1eries"], 
     [/([^aeiouy]|qu)ies$/i,  "$1y"], 
     [/([lr])ves$/i,    "$1f"], 
     [/(tive)s$/i,    "$1"], 
     [/(hive)s$/i,    "$1"], 
     [/(li|wi|kni)ves$/i,  "$1fe"], 
     [/(shea|loa|lea|thie)ves$/i,"$1f"], 
     [/(^analy)ses$/i,   "$1sis"], 
     [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis"], 
     [/([ti])a$/i,    "$1um"], 
     [/(n)ews$/i,    "$1ews"], 
     [/(h|bl)ouses$/i,   "$1ouse"], 
     [/(corpse)s$/i,    "$1"], 
     [/(us)es$/i,    "$1"], 
     [/s$/i,      ""] 
    ]; 

    private static var irregular : Array = [ 
     ['move' , 'moves'], 
     ['foot' , 'feet'], 
     ['goose' , 'geese'], 
     ['sex' , 'sexes'], 
     ['child' , 'children'], 
     ['man' , 'men'], 
     ['tooth' , 'teeth'], 
     ['person' , 'people'] 
    ]; 

    private static var uncountable : Array = [ 
     'sheep', 
     'fish', 
     'deer', 
     'series', 
     'species', 
     'money', 
     'rice', 
     'information', 
     'equipment' 
    ]; 

    public static function pluralize(string : String) : String 
    { 
     var pattern : RegExp; 
     var result : String; 

     // save some time in the case that singular and plural are the same 
     if (uncountable.indexOf(string.toLowerCase()) != -1) 
      return string; 

     // check for irregular singular forms 
     var item : Array; 
     for each (item in irregular) 
     { 
      pattern = new RegExp(item[0] + "$", "i"); 
      result = item[1]; 

      if (pattern.test(string)) 
      { 
       return string.replace(pattern, result); 
      } 
     } 

     // check for matches using regular expressions 
     for each (item in plural) 
     { 
      pattern = item[0]; 
      result = item[1]; 

      if (pattern.test(string)) 
      { 
       return string.replace(pattern, result); 
      } 
     } 

     return string; 
    } 

    public static function singularize(string : String) : String 
    { 
     var pattern : RegExp; 
     var result : String 

     // save some time in the case that singular and plural are the same 
     if (uncountable.indexOf(string.toLowerCase()) != -1) 
      return string; 

     // check for irregular singular forms 
     var item : Array; 
     for each (item in irregular) 
     { 
      pattern = new RegExp(item[1] + "$", "i"); 
      result = item[0]; 

      if (pattern.test(string)) 
      { 
       return string.replace(pattern, result); 
      } 
     } 

     // check for matches using regular expressions 
     for each (item in singular) 
     { 
      pattern = item[0]; 
      result = item[1]; 

      if (pattern.test(string)) 
      { 
       return string.replace(pattern, result); 
      } 
     } 

     return string; 

    } 

    public static function pluralizeIf(count : int, string : String) : String 
    { 
     if (count == 1) 
      return "1 " + string; 
     else 
      return count.toString() + " " + pluralize(string); 
    } 
} 
} 
+0

@ TheDarkIn1978 इतिहास के लिए कोड जोड़ने के लिए धन्यवाद, इसे स्वयं करना चाहिए था लेकिन देर हो चुकी थी और मैं थक गया था। – shaunhusain