Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #81 from pappy74/pluralize-phrases
Browse files Browse the repository at this point in the history
Pluralize phrases.
  • Loading branch information
jeresig committed Jun 2, 2011
2 parents ecf9b3d + 19a930c commit a13484e
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions utils/word-problems.js
Expand Up @@ -31,36 +31,68 @@ jQuery.extend( KhanUtil, {
// - plural(NUMBER, singular, plural):
// - return "NUMBER word"
plural: (function() {
var one_offs = {
var oneOffs = {
'quiz': 'quizzes',
'can of food': 'cans of food',
'shelf': 'shelves',
'loaf of bread': 'loaves of bread',
'gallon of milk': 'gallons of milk',
'loaf': 'loaves',
'potato': 'potatoes'
};

var pluralizeWord = function(word) {

// noone really needs extra spaces at the edges, do they?
word = jQuery.trim( word );

// determine if our word is all caps. If so, we'll need to
// re-capitalize at the end
var isUpperCase = (word.toUpperCase() == word);
var one_off = one_offs[word.toLowerCase()];
var oneOff = oneOffs[word.toLowerCase()];
var words = word.split(/\s+/);

if ( one_off ) {
word = one_off;
// first handle simple one-offs
if ( oneOff ) {
return oneOff;
}
else if ( /[^aeiou]y$/i.test( word ) ) {
word = word.replace(/y$/i, "ies");
} else if ( /[sxz]$/i.test( word ) || /[bcfhjlmnqsvwxyz]h$/.test( word ) ) {
word += "es";
} else {
word += "s";

// multiple words
else if ( words.length > 1 ) {
// for 3-word phrases where the middle word is 'in' or 'of',
// pluralize the first word
if ( words.length == 3 && /\b(in|of)\b/i.test(words[1]) ) {
words[0] = KhanUtil.plural( words[0] );
}

// otherwise, just pluraize the last word
else {
words[ words.length-1 ] =
KhanUtil.plural( words[ words.length-1 ] );
}

return words.join(" ");
}

if ( isUpperCase ) {
word = word.toUpperCase();
// single words
else {
// "-y" => "-ies"
if ( /[^aeiou]y$/i.test( word ) ) {
word = word.replace(/y$/i, "ies");
}

// add "es"; things like "fish" => "fishes"
else if ( /[sxz]$/i.test( word ) || /[bcfhjlmnqsvwxyz]h$/.test( word ) ) {
word += "es";
}

// all the rest, just add "s"
else {
word += "s";
}

if ( isUpperCase ) {
word = word.toUpperCase();
}
return word;
}
return word;
};

return function(value, arg1, arg2) {
Expand Down

0 comments on commit a13484e

Please sign in to comment.