Skip to content

Instantly share code, notes, and snippets.

@wryk
Created February 18, 2014 13:29
Show Gist options
  • Save wryk/9070990 to your computer and use it in GitHub Desktop.
Save wryk/9070990 to your computer and use it in GitHub Desktop.
pluralize word (use regular plural rules => works only on regular words)
/*
plural rules
regular
if -s, -x, -ch or -sh then add -es
if -{consonant}y then remove -y and add -ies
else add -s
irregular
if -f then remove -f and add -ves
if -fe then remove -fe and add -ves
if -us then remove -us and add -i
if -o then add -es
*/
module.exports = function pluralize (word) {
if (!word) return '';
word = word.split('');
var s /* size */ = word.length;
var p /* penultimate */ = word[s - 2];
var l /* last */ = word[s - 1];
if (l == 's' || l == 'x' || l == 'h' && p == 'c' || p == 's') {
word.push('es')
} else {
if (l == 'y' && !(p == 'a' || p == 'e' || p == 'i' || p == 'o' || p == 'u' || p == 'y')) {
word.splice(s - 1, 1, 'ies')
} else {
word.push('s');
}
}
return word.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment