-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed manual test; renamed inflection to fit with module scheme; re…
…wrote library to no longer change String.prototype; added the license
- Loading branch information
Showing
4 changed files
with
242 additions
and
769 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2011 Sonny Michaud, http://www.gourdian.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 OR COPYRIGHT HOLDERS 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
var Inflect = function() { | ||
}; | ||
|
||
module.exports = Inflect; | ||
|
||
// This is a list of nouns that use the same form for both singular and plural. This list should remain entirely in lower case to correctly match Strings. | ||
var uncountable_words = [ 'equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep', 'moose', 'deer', 'news' ] | ||
|
||
// These rules translate from the singular form of a noun to its plural form. | ||
, plural_rules = [ | ||
[new RegExp('(m)an$', 'gi'), '$1en'], | ||
[new RegExp('(pe)rson$', 'gi'), '$1ople'], | ||
[new RegExp('(child)$', 'gi'), '$1ren'], | ||
[new RegExp('^(ox)$', 'gi'), '$1en'], | ||
[new RegExp('(ax|test)is$', 'gi'), '$1es'], | ||
[new RegExp('(octop|vir)us$', 'gi'), '$1i'], | ||
[new RegExp('(alias|status)$', 'gi'), '$1es'], | ||
[new RegExp('(bu)s$', 'gi'), '$1ses'], | ||
[new RegExp('(buffal|tomat|potat)o$', 'gi'), '$1oes'], | ||
[new RegExp('([ti])um$', 'gi'), '$1a'], | ||
[new RegExp('sis$', 'gi'), 'ses'], | ||
[new RegExp('(?:([^f])fe|([lr])f)$', 'gi'), '$1$2ves'], | ||
[new RegExp('(hive)$', 'gi'), '$1s'], | ||
[new RegExp('([^aeiouy]|qu)y$', 'gi'), '$1ies'], | ||
[new RegExp('(x|ch|ss|sh)$', 'gi'), '$1es'], | ||
[new RegExp('(matr|vert|ind)ix|ex$', 'gi'), '$1ices'], | ||
[new RegExp('([m|l])ouse$', 'gi'), '$1ice'], | ||
[new RegExp('(quiz)$', 'gi'), '$1zes'], | ||
[new RegExp('s$', 'gi'), 's'], | ||
[new RegExp('$', 'gi'), 's'] | ||
] | ||
|
||
// These rules translate from the plural form of a noun to its singular form. | ||
, singular_rules = [ | ||
[new RegExp('(m)en$', 'gi'), '$1an'], | ||
[new RegExp('(pe)ople$', 'gi'), '$1rson'], | ||
[new RegExp('(child)ren$', 'gi'), '$1'], | ||
[new RegExp('([ti])a$', 'gi'), '$1um'], | ||
[new RegExp('((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$','gi'), '$1$2sis'], | ||
[new RegExp('(hive)s$', 'gi'), '$1'], | ||
[new RegExp('(tive)s$', 'gi'), '$1'], | ||
[new RegExp('(curve)s$', 'gi'), '$1'], | ||
[new RegExp('([lr])ves$', 'gi'), '$1f'], | ||
[new RegExp('([^fo])ves$', 'gi'), '$1fe'], | ||
[new RegExp('([^aeiouy]|qu)ies$', 'gi'), '$1y'], | ||
[new RegExp('(s)eries$', 'gi'), '$1eries'], | ||
[new RegExp('(m)ovies$', 'gi'), '$1ovie'], | ||
[new RegExp('(x|ch|ss|sh)es$', 'gi'), '$1'], | ||
[new RegExp('([m|l])ice$', 'gi'), '$1ouse'], | ||
[new RegExp('(bus)es$', 'gi'), '$1'], | ||
[new RegExp('(o)es$', 'gi'), '$1'], | ||
[new RegExp('(shoe)s$', 'gi'), '$1'], | ||
[new RegExp('(cris|ax|test)es$', 'gi'), '$1is'], | ||
[new RegExp('(octop|vir)i$', 'gi'), '$1us'], | ||
[new RegExp('(alias|status)es$', 'gi'), '$1'], | ||
[new RegExp('^(ox)en', 'gi'), '$1'], | ||
[new RegExp('(vert|ind)ices$', 'gi'), '$1ex'], | ||
[new RegExp('(matr)ices$', 'gi'), '$1ix'], | ||
[new RegExp('(quiz)zes$', 'gi'), '$1'], | ||
[new RegExp('s$', 'gi'), ''] | ||
] | ||
|
||
// This is a list of words that should not be capitalized for title case | ||
, non_titlecased_words = [ | ||
'and', 'or', 'nor', 'a', 'an', 'the', 'so', 'but', 'to', 'of', 'at', | ||
'by', 'from', 'into', 'on', 'onto', 'off', 'out', 'in', 'over', | ||
'with', 'for' | ||
] | ||
|
||
// These are regular expressions used for converting between String formats | ||
, id_suffix = new RegExp('(_ids|_id)$', 'g') | ||
, underbar = new RegExp('_', 'g') | ||
, space_or_underbar = new RegExp('[\ _]', 'g') | ||
, uppercase = new RegExp('([A-Z])', 'g') | ||
, underbar_prefix = new RegExp('^_'); | ||
|
||
/* | ||
This is a helper method that applies rules based replacement to a String | ||
Signature: | ||
InflectionJS.apply_rules(str, rules, skip, override) == String | ||
Arguments: | ||
str - String - String to modify and return based on the passed rules | ||
rules - Array: [RegExp, String] - Regexp to match paired with String to use for replacement | ||
skip - Array: [String] - Strings to skip if they match | ||
override - String (optional) - String to return as though this method succeeded (used to conform to APIs) | ||
Returns: | ||
String - passed String modified by passed rules | ||
Examples: | ||
InflectionJS.apply_rules("cows", InflectionJs.singular_rules) === 'cow' | ||
*/ | ||
function apply_rules(str, rules, skip, override) { | ||
if (override) { | ||
str = override; | ||
} else { | ||
var ignore = (skip.indexOf(str.toLowerCase()) > -1); | ||
if (!ignore) { | ||
for (var x = 0; x < rules.length; x++) { | ||
if (str.match(rules[x][0])) { | ||
str = str.replace(rules[x][0], rules[x][1]); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
|
||
Inflect.prototype.pluralize = function(string, plural) { | ||
return InflectionJS.apply_rules(string, this._plural_rules, this._uncountable_words, plural); | ||
}; | ||
|
||
Inflect.prototype.singularize = function(string, singular) { | ||
return InflectionJS.apply_rules(string, this._singular_rules, this._uncountable_words, singular); | ||
}; | ||
|
||
String.prototype.camelize = function(string, lowFirstLetter) { | ||
var str = string.toLowerCase(); | ||
var str_path = str.split('/'); | ||
|
||
for (var i = 0; i < str_path.length; i++) { | ||
var str_arr = str_path[i].split('_'); | ||
var initX = ((lowFirstLetter && i + 1 === str_path.length) ? (1) : (0)); | ||
|
||
for (var x = initX; x < str_arr.length; x++) { | ||
str_arr[x] = str_arr[x].charAt(0).toUpperCase() + str_arr[x].substring(1); | ||
} | ||
|
||
str_path[i] = str_arr.join(''); | ||
} | ||
str = str_path.join('::'); | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.underscore = function(str) { | ||
var str_path = str.split('::'); | ||
for (var i = 0; i < str_path.length; i++) { | ||
str_path[i] = str_path[i].replace(InflectionJS.uppercase, '_$1'); | ||
str_path[i] = str_path[i].replace(InflectionJS.underbar_prefix, ''); | ||
} | ||
str = str_path.join('/').toLowerCase(); | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.humanize = function(string, lowFirstLetter) { | ||
var str = string.toLowerCase(); | ||
str = str.replace(InflectionJS.id_suffix, ''); | ||
str = str.replace(InflectionJS.underbar, ' '); | ||
if (!lowFirstLetter) { | ||
str = str.capitalize(); | ||
} | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.capitalize = function(string) { | ||
var str = string.toLowerCase(); | ||
str = str.substring(0, 1).toUpperCase() + str.substring(1); | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.dasherize = function(str) { | ||
str = str.replace(InflectionJS.space_or_underbar, '-'); | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.titleize = function(string) { | ||
var str = string.toLowerCase(); | ||
str = str.replace(InflectionJS.underbar, ' '); | ||
var str_arr = str.split(' '); | ||
for (var x = 0; x < str_arr.length; x++) { | ||
var d = str_arr[x].split('-'); | ||
for (var i = 0; i < d.length; i++) { | ||
if (this._non_titlecased_words.indexOf(d[i].toLowerCase()) < 0) { | ||
d[i] = d[i].capitalize(); | ||
} | ||
} | ||
str_arr[x] = d.join('-'); | ||
} | ||
str = str_arr.join(' '); | ||
str = str.substring(0, 1).toUpperCase() + str.substring(1); | ||
return str; | ||
}; | ||
|
||
Inflect.demodulize = function(str) { | ||
var str_arr = str.split('::'); | ||
str = str_arr[str_arr.length - 1]; | ||
return str; | ||
}; | ||
|
||
Inflect.tableize = function(str) { | ||
str = str.underscore().pluralize(); | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.classify = function(str) { | ||
str = str.camelize().singularize(); | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.foreign_key = function(str, dropIdUbar) { | ||
str = str.demodulize().underscore() + ((dropIdUbar) ? ('') : ('_')) + 'id'; | ||
return str; | ||
}; | ||
|
||
Inflect.prototype.ordinalize = function(str) { | ||
var str_arr = str.split(' '); | ||
for (var x = 0; x < str_arr.length; x++) { | ||
var i = parseInt(str_arr[x]); | ||
if (i === NaN) { | ||
var ltd = str_arr[x].substring(str_arr[x].length - 2); | ||
var ld = str_arr[x].substring(str_arr[x].length - 1); | ||
var suf = "th"; | ||
if (ltd != "11" && ltd != "12" && ltd != "13") { | ||
if (ld === "1") { suf = "st"; } | ||
else if (ld === "2") { suf = "nd"; } | ||
else if (ld === "3") { suf = "rd"; } | ||
} | ||
str_arr[x] += suf; | ||
} | ||
} | ||
str = str_arr.join(' '); | ||
return str; | ||
}; |
Oops, something went wrong.