Created
July 22, 2015 19:41
-
-
Save BenRacicot/fbf632f9f9e7b9b00c41 to your computer and use it in GitHub Desktop.
Found this code to pluralize words, nouns to be exact. It included some functions that where not defined so I've added the var 'last' to replace the original function that grabbed the last character.
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
String.prototype.plural = function () { | |
// last character | |
var last = this.charAt(this.length-1); | |
if (this.last === 'y') { | |
if ( (this.charAt(this.length - 2)).isVowel() ) { | |
// If the y has a vowel before it (i.e. toys), then you just add the s. | |
return this + 's'; | |
} | |
else { | |
// If a this ends in y with a consonant before it (fly), you drop the y and add -ies to make it plural. | |
return this.slice(0, -1) + 'ies'; | |
} | |
} | |
else if (this.substring( this.length - 2) === 'us') { | |
// ends in us -> i, needs to preceed the generic 's' rule | |
return this.slice(0, -2) + 'i'; | |
} | |
else if (['ch', 'sh'].indexOf(this.substring( this.length - 2)) !== -1 || ['x','s'].indexOf(last) !== -1) { | |
// If a this ends in ch, sh, x, s, you add -es to make it plural. | |
return this + 'es'; | |
} | |
else { | |
// anything else, just add s | |
return this + 's'; | |
} | |
} | |
// use like this | |
console.log(somenoun.plural()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment