1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*
* Copyright (c) 2010 George Moschovitis, http://www.gmosx.com
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* A port of the Rails/ActiveSupport Inflector class
* http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html
*/
/**
@name inflection
@namespace inflection
*/
var inflection = new (function () {
/**
@name inflection#inflections
@public
@object
@description A list of rules and replacements for different inflection types
*/
this.inflections = {
plurals: []
, singulars: []
, uncountables: []
};
var self = this
, setInflection
, setPlural
, setSingular
, setUncountable
, setIrregular;
// Add a new inflection rule/replacement to the beginning of the array for the
// inflection type
setInflection = function (type, rule, replacement) {
self.inflections[type].unshift([rule, replacement]);
};
// Add a new plural inflection rule
setPlural = function (rule, replacement) {
setInflection('plurals', rule, replacement);
};
// Add a new singular inflection rule
setSingular = function (rule, replacement) {
setInflection('singulars', rule, replacement);
};
// Add a new irregular word to the inflection list, by a given singular and plural inflection
setIrregular = function (singular, plural) {
if (singular.substr(0, 1).toUpperCase() == plural.substr(0, 1).toUpperCase()) {
setPlural(new RegExp("(" + singular.substr(0, 1) + ")" + singular.substr(1) + "$", "i"),
'$1' + plural.substr(1));
setPlural(new RegExp("(" + plural.substr(0, 1) + ")" + plural.substr(1) + "$", "i"),
'$1' + plural.substr(1));
setSingular(new RegExp("(" + plural.substr(0, 1) + ")" + plural.substr(1) + "$", "i"),
'$1' + singular.substr(1));
} else {
setPlural(new RegExp(singular.substr(0, 1).toUpperCase() + singular.substr(1) + "$"),
plural.substr(0, 1).toUpperCase() + plural.substr(1));
setPlural(new RegExp(singular.substr(0, 1).toLowerCase() + singular.substr(1) + "$"),
plural.substr(0, 1).toLowerCase() + plural.substr(1));
setPlural(new RegExp(plural.substr(0, 1).toUpperCase() + plural.substr(1) + "$"),
plural.substr(0, 1).toUpperCase() + plural.substr(1));
setPlural(new RegExp(plural.substr(0, 1).toLowerCase() + plural.substr(1) + "$"),
plural.substr(0, 1).toLowerCase() + plural.substr(1));
setSingular(new RegExp(plural.substr(0, 1).toUpperCase() + plural.substr(1) + "$"),
singular.substr(0, 1).toUpperCase() + singular.substr(1));
setSingular(new RegExp(plural.substr(0, 1).toLowerCase() + plural.substr(1) + "$"),
singular.substr(0, 1).toLowerCase() + singular.substr(1));
}
};
// Add a new word to the uncountable inflection list
setUncountable = function (word) {
self.inflections.uncountables[word] = true;
};
// Create inflections
(function () {
setPlural(/$/, "s");
setPlural(/s$/i, "s");
setPlural(/(ax|test)is$/i, "$1es");
setPlural(/(octop|vir)us$/i, "$1i");
setPlural(/(octop|vir)i$/i, "$1i");
setPlural(/(alias|status)$/i, "$1es");
setPlural(/(bu)s$/i, "$1ses");
setPlural(/(buffal|tomat)o$/i, "$1oes");
setPlural(/([ti])a$/i, "$1a");
setPlural(/([ti])um$/i, "$1a");
setPlural(/sis$/i, "ses");
setPlural(/ses$/i, "ses");
setPlural(/(?:([^f])fe|([lr])f)$/i, "$1$2ves");
setPlural(/(hive)$/i, "$1s");
setPlural(/([^aeiouy]|qu)y$/i, "$1ies");
setPlural(/(x|ch|ss|sh)$/i, "$1es");
setPlural(/(matr|vert|ind)(?:ix|ex)$/i, "$1ices");
setPlural(/([m|l])ouse$/i, "$1ice");
setPlural(/([m|l])ice$/i, "$1ice");
setPlural(/^(ox)$/i, "$1en");
setPlural(/^(ox)en$/i, "$1en");
setPlural(/(quiz)$/i, "$1zes");
setSingular(/s$/i, "")
setSingular(/ss$/i, "ss")
setSingular(/(n)ews$/i, "$1ews")
setSingular(/([ti])um$/i, "$1um")
setSingular(/([ti])a$/i, "$1um")
setSingular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis")
setSingular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)sis$/i, "$1$2sis")
setSingular(/(^analy)ses$/i, "$1sis")
setSingular(/(^analy)sis$/i, "$1sis")
setSingular(/([^f])ves$/i, "$1fe")
setSingular(/(hive)s$/i, "$1")
setSingular(/(tive)s$/i, "$1")
setSingular(/([lr])ves$/i, "$1f")
setSingular(/([^aeiouy]|qu)ies$/i, "$1y")
setSingular(/(s)eries$/i, "$1eries")
setSingular(/(m)ovies$/i, "$1ovie")
setSingular(/(x|ch|ss|sh)es$/i, "$1")
setSingular(/([m|l])ice$/i, "$1ouse")
setSingular(/([m|l])ouse$/i, "$1ouse")
setSingular(/(bus)es$/i, "$1")
setSingular(/(bus)$/i, "$1")
setSingular(/(o)es$/i, "$1")
setSingular(/(shoe)s$/i, "$1")
setSingular(/(cris|ax|test)es$/i, "$1is")
setSingular(/(cris|ax|test)is$/i, "$1is")
setSingular(/(octop|vir)i$/i, "$1us")
setSingular(/(octop|vir)us$/i, "$1us")
setSingular(/(alias|status)es$/i, "$1")
setSingular(/(alias|status)$/i, "$1")
setSingular(/^(ox)en/i, "$1")
setSingular(/(vert|ind)ices$/i, "$1ex")
setSingular(/(matr)ices$/i, "$1ix")
setSingular(/(quiz)zes$/i, "$1")
setSingular(/(database)s$/i, "$1")
setIrregular("person", "people");
setIrregular("man", "men");
setIrregular("child", "children");
setIrregular("sex", "sexes");
setIrregular("move", "moves");
setIrregular("cow", "kine");
setUncountable("equipment");
setUncountable("information");
setUncountable("rice");
setUncountable("money");
setUncountable("species");
setUncountable("series");
setUncountable("fish");
setUncountable("sheep");
setUncountable("jeans");
})();
/**
@name inflection#parse
@public
@function
@return {String} The inflection of the word from the type given
@description Parse a word from the given inflection type
@param {String} type A type of the inflection to use
@param {String} word the word to parse
*/
this.parse = function (type, word) {
var lowWord = word.toLowerCase()
, inflections = this.inflections[type];
if (this.inflections.uncountables[lowWord]) {
return word;
}
var i = -1;
while (++i < inflections.length) {
var rule = inflections[i][0]
, replacement = inflections[i][1];
if (rule.test(word)) {
return word.replace(rule, replacement)
}
}
return word;
};
/**
@name inflection#pluralize
@public
@function
@return {String} The plural inflection for the given word
@description Create a plural inflection for a word
@param {String} word the word to create a plural version for
*/
this.pluralize = function (word) {
return this.parse('plurals', word);
};
/**
@name inflection#singularize
@public
@function
@return {String} The singular inflection for the given word
@description Create a singular inflection for a word
@param {String} word the word to create a singular version for
*/
this.singularize = function (word) {
return this.parse('singulars', word);
};
})();
module.exports = inflection;
|