<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: inflector code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:57:08 GMT</pubDate>
    <description>DZone Snippets: inflector code</description>
    <item>
      <title>Javascript Rails-like inflector</title>
      <link>http://snippets.dzone.com/posts/show/3205</link>
      <description>I was using Ajax to do some DOM manipulation in a Rails app I was working on.  None of what I was doing needed to involve a round trip to server, the only reason I hit the server at all was to take advantage of Rails' (well, ActiveSupport's) pluralize function.  Also, this technique required an extra action that I decided to eliminate as I try to move to a more RESTful architecture.&lt;br /&gt;&lt;br /&gt;The only part of ActiveSupport's Inflector class I implemented are the ones I needed.  I implemented even fewer of Rails' Inflector-based functions.&lt;br /&gt;&lt;br /&gt;Stick all this in a file, such as "inflector.js".&lt;br /&gt;&lt;code&gt;/*&lt;br /&gt; * This script depends on no outside libraries.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;Inflector = {&lt;br /&gt;    /*&lt;br /&gt;     * The order of all these lists has been reversed from the way &lt;br /&gt;     * ActiveSupport had them to keep the correct priority.&lt;br /&gt;     */&lt;br /&gt;    Inflections: {&lt;br /&gt;        plural: [&lt;br /&gt;            [/(quiz)$/i,               "$1zes"  ],&lt;br /&gt;            [/^(ox)$/i,                "$1en"   ],&lt;br /&gt;            [/([m|l])ouse$/i,          "$1ice"  ],&lt;br /&gt;            [/(matr|vert|ind)ix|ex$/i, "$1ices" ],&lt;br /&gt;            [/(x|ch|ss|sh)$/i,         "$1es"   ],&lt;br /&gt;            [/([^aeiouy]|qu)y$/i,      "$1ies"  ],&lt;br /&gt;            [/(hive)$/i,               "$1s"    ],&lt;br /&gt;            [/(?:([^f])fe|([lr])f)$/i, "$1$2ves"],&lt;br /&gt;            [/sis$/i,                  "ses"    ],&lt;br /&gt;            [/([ti])um$/i,             "$1a"    ],&lt;br /&gt;            [/(buffal|tomat)o$/i,      "$1oes"  ],&lt;br /&gt;            [/(bu)s$/i,                "$1ses"  ],&lt;br /&gt;            [/(alias|status)$/i,       "$1es"   ],&lt;br /&gt;            [/(octop|vir)us$/i,        "$1i"    ],&lt;br /&gt;            [/(ax|test)is$/i,          "$1es"   ],&lt;br /&gt;            [/s$/i,                    "s"      ],&lt;br /&gt;            [/$/,                      "s"      ]&lt;br /&gt;        ],&lt;br /&gt;        singular: [&lt;br /&gt;            [/(quiz)zes$/i,                                                    "$1"     ],&lt;br /&gt;            [/(matr)ices$/i,                                                   "$1ix"   ],&lt;br /&gt;            [/(vert|ind)ices$/i,                                               "$1ex"   ],&lt;br /&gt;            [/^(ox)en/i,                                                       "$1"     ],&lt;br /&gt;            [/(alias|status)es$/i,                                             "$1"     ],&lt;br /&gt;            [/(octop|vir)i$/i,                                                 "$1us"   ],&lt;br /&gt;            [/(cris|ax|test)es$/i,                                             "$1is"   ],&lt;br /&gt;            [/(shoe)s$/i,                                                      "$1"     ],&lt;br /&gt;            [/(o)es$/i,                                                        "$1"     ],&lt;br /&gt;            [/(bus)es$/i,                                                      "$1"     ],&lt;br /&gt;            [/([m|l])ice$/i,                                                   "$1ouse" ],&lt;br /&gt;            [/(x|ch|ss|sh)es$/i,                                               "$1"     ],&lt;br /&gt;            [/(m)ovies$/i,                                                     "$1ovie" ],&lt;br /&gt;            [/(s)eries$/i,                                                     "$1eries"],&lt;br /&gt;            [/([^aeiouy]|qu)ies$/i,                                            "$1y"    ],&lt;br /&gt;            [/([lr])ves$/i,                                                    "$1f"    ],&lt;br /&gt;            [/(tive)s$/i,                                                      "$1"     ],&lt;br /&gt;            [/(hive)s$/i,                                                      "$1"     ],&lt;br /&gt;            [/([^f])ves$/i,                                                    "$1fe"   ],&lt;br /&gt;            [/(^analy)ses$/i,                                                  "$1sis"  ],&lt;br /&gt;            [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis"],&lt;br /&gt;            [/([ti])a$/i,                                                      "$1um"   ],&lt;br /&gt;            [/(n)ews$/i,                                                       "$1ews"  ],&lt;br /&gt;            [/s$/i,                                                            ""       ]&lt;br /&gt;        ],&lt;br /&gt;        irregular: [&lt;br /&gt;            ['move',   'moves'   ],&lt;br /&gt;            ['sex',    'sexes'   ],&lt;br /&gt;            ['child',  'children'],&lt;br /&gt;            ['man',    'men'     ],&lt;br /&gt;            ['person', 'people'  ]&lt;br /&gt;        ],&lt;br /&gt;        uncountable: [&lt;br /&gt;            "sheep",&lt;br /&gt;            "fish",&lt;br /&gt;            "series",&lt;br /&gt;            "species",&lt;br /&gt;            "money",&lt;br /&gt;            "rice",&lt;br /&gt;            "information",&lt;br /&gt;            "equipment"&lt;br /&gt;        ]&lt;br /&gt;    },&lt;br /&gt;    ordinalize: function(number) {&lt;br /&gt;        if (11 &lt;= parseInt(number) % 100 &amp;&amp; parseInt(number) % 100 &lt;= 13) {&lt;br /&gt;            return number + "th";&lt;br /&gt;        } else {&lt;br /&gt;            switch (parseInt(number) % 10) {&lt;br /&gt;                case  1: return number + "st";&lt;br /&gt;                case  2: return number + "nd";&lt;br /&gt;                case  3: return number + "rd";&lt;br /&gt;                default: return number + "th";&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    pluralize: function(word) {&lt;br /&gt;        for (var i = 0; i &lt; Inflector.Inflections.uncountable.length; i++) {&lt;br /&gt;            var uncountable = Inflector.Inflections.uncountable[i];&lt;br /&gt;            if (word.toLowerCase == uncountable) {&lt;br /&gt;                return uncountable;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        for (var i = 0; i &lt; Inflector.Inflections.irregular.length; i++) {&lt;br /&gt;            var singular = Inflector.Inflections.irregular[i][0];&lt;br /&gt;            var plural   = Inflector.Inflections.irregular[i][1];&lt;br /&gt;            if ((word.toLowerCase == singular) || (word == plural)) {&lt;br /&gt;                return plural;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        for (var i = 0; i &lt; Inflector.Inflections.plural.length; i++) {&lt;br /&gt;            var regex          = Inflector.Inflections.plural[i][0];&lt;br /&gt;            var replace_string = Inflector.Inflections.plural[i][1];&lt;br /&gt;            if (regex.test(word)) {&lt;br /&gt;                return word.replace(regex, replace_string);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function ordinalize(number) {&lt;br /&gt;    return Inflector.ordinalize(number);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * Javascript doesn't have optional parameters or overloading so I had to use&lt;br /&gt; * the ever popular pseudo options hash object technique.&lt;br /&gt; * required properties:&lt;br /&gt; *     count    Number of objects to pluralize&lt;br /&gt; *     singular Singular noun for the objects&lt;br /&gt; * optional property:&lt;br /&gt; *     plural   Plural (probably irregular) noun for the objects&lt;br /&gt; * examples:&lt;br /&gt; *      pluralize({ count: total_count, singular: "Issue" })&lt;br /&gt; *      pluralize({ count: total_count, singular: "Goose", plural: "Geese" })&lt;br /&gt; */&lt;br /&gt;function pluralize(options) {&lt;br /&gt;    return options.count + " " + (1 == parseInt(options.count) ?&lt;br /&gt;        options.singular :&lt;br /&gt;        options.plural || Inflector.pluralize(options.singular));&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Tue, 26 Dec 2006 07:37:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3205</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
  </channel>
</rss>
