Comparing version 5.0.0 to 5.1.0
{ | ||
"name": "pluralize", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Pluralize and singularize any word", | ||
@@ -35,3 +35,3 @@ "main": "pluralize.js", | ||
"devDependencies": { | ||
"chai": "^3.2.0", | ||
"chai": "^4.0.0", | ||
"istanbul": "^0.4.5", | ||
@@ -38,0 +38,0 @@ "mocha": "^3.2.0", |
112
pluralize.js
@@ -27,12 +27,2 @@ /* global define */ | ||
/** | ||
* Title case a string. | ||
* | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
function toTitleCase (str) { | ||
return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase(); | ||
} | ||
/** | ||
* Sanitize a pluralization rule to a usable regular expression. | ||
@@ -61,14 +51,10 @@ * | ||
// Tokens are an exact match. | ||
if (word === token) { | ||
return token; | ||
} | ||
if (word === token) return token; | ||
// Upper cased words. E.g. "HELLO". | ||
if (word === word.toUpperCase()) { | ||
return token.toUpperCase(); | ||
} | ||
if (word === word.toUpperCase()) return token.toUpperCase(); | ||
// Title cased words. E.g. "Title". | ||
if (word[0] === word[0].toUpperCase()) { | ||
return toTitleCase(token); | ||
return token.charAt(0).toUpperCase() + token.substr(1).toLowerCase(); | ||
} | ||
@@ -94,2 +80,21 @@ | ||
/** | ||
* Replace a word using a rule. | ||
* | ||
* @param {string} word | ||
* @param {Array} rule | ||
* @return {string} | ||
*/ | ||
function replace (word, rule) { | ||
return word.replace(rule[0], function (match, index) { | ||
var result = interpolate(rule[1], arguments); | ||
if (match === '') { | ||
return restoreCase(word[index - 1], result); | ||
} | ||
return restoreCase(match, result); | ||
}); | ||
} | ||
/** | ||
* Sanitize a word by passing in the word and sanitization rules. | ||
@@ -99,6 +104,6 @@ * | ||
* @param {string} word | ||
* @param {Array} collection | ||
* @param {Array} rules | ||
* @return {string} | ||
*/ | ||
function sanitizeWord (token, word, collection) { | ||
function sanitizeWord (token, word, rules) { | ||
// Empty string or doesn't need fixing. | ||
@@ -109,20 +114,9 @@ if (!token.length || uncountables.hasOwnProperty(token)) { | ||
var len = collection.length; | ||
var len = rules.length; | ||
// Iterate over the sanitization rules and use the first one to match. | ||
while (len--) { | ||
var rule = collection[len]; | ||
var rule = rules[len]; | ||
// If the rule passes, return the replacement. | ||
if (rule[0].test(word)) { | ||
return word.replace(rule[0], function (match, index, word) { | ||
var result = interpolate(rule[1], arguments); | ||
if (match === '') { | ||
return restoreCase(word[index - 1], result); | ||
} | ||
return restoreCase(match, result); | ||
}); | ||
} | ||
if (rule[0].test(word)) return replace(word, rule); | ||
} | ||
@@ -162,2 +156,16 @@ | ||
/** | ||
* Check if a word is part of the map. | ||
*/ | ||
function checkWord (replaceMap, keepMap, rules, bool) { | ||
return function (word) { | ||
var token = word.toLowerCase(); | ||
if (keepMap.hasOwnProperty(token)) return true; | ||
if (replaceMap.hasOwnProperty(token)) return false; | ||
return sanitizeWord(token, token, rules) === token; | ||
}; | ||
} | ||
/** | ||
* Pluralize or singularize a word based on the passed in count. | ||
@@ -187,2 +195,11 @@ * | ||
/** | ||
* Check if a word is plural. | ||
* | ||
* @type {Function} | ||
*/ | ||
pluralize.isPlural = checkWord( | ||
irregularSingles, irregularPlurals, pluralRules | ||
); | ||
/** | ||
* Singularize a word. | ||
@@ -197,2 +214,11 @@ * | ||
/** | ||
* Check if a word is singular. | ||
* | ||
* @type {Function} | ||
*/ | ||
pluralize.isSingular = checkWord( | ||
irregularPlurals, irregularSingles, singularRules | ||
); | ||
/** | ||
* Add a pluralization rule to the collection. | ||
@@ -378,4 +404,4 @@ * | ||
// Singular words with no plurals. | ||
'adulthood', | ||
'advice', | ||
'adulthood', | ||
'agenda', | ||
@@ -385,2 +411,3 @@ 'aid', | ||
'ammo', | ||
'anime', | ||
'athletics', | ||
@@ -397,12 +424,12 @@ 'bison', | ||
'clothing', | ||
'cod', | ||
'commerce', | ||
'cod', | ||
'cooperation', | ||
'corps', | ||
'digestion', | ||
'debris', | ||
'diabetes', | ||
'digestion', | ||
'elk', | ||
'energy', | ||
'equipment', | ||
'elk', | ||
'excretion', | ||
@@ -434,2 +461,3 @@ 'expertise', | ||
'music', | ||
'manga', | ||
'news', | ||
@@ -454,5 +482,5 @@ 'pike', | ||
'tennis', | ||
'trout', | ||
'traffic', | ||
'transporation', | ||
'trout', | ||
'tuna', | ||
@@ -466,9 +494,9 @@ 'wealth', | ||
// Regexes. | ||
/pox$/i, // "chickpox", "smallpox" | ||
/ois$/i, | ||
/[^aeiou]ese$/i, // "chinese", "japanese" | ||
/deer$/i, // "deer", "reindeer" | ||
/fish$/i, // "fish", "blowfish", "angelfish" | ||
/sheep$/i, | ||
/measles$/i, | ||
/[^aeiou]ese$/i // "chinese", "japanese" | ||
/o[iu]s$/i, // "carnivorous" | ||
/pox$/i, // "chickpox", "smallpox" | ||
/sheep$/i | ||
].forEach(pluralize.addUncountableRule); | ||
@@ -475,0 +503,0 @@ |
@@ -7,2 +7,3 @@ # Pluralize | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
![File Size][filesize-url] | ||
[![CDNJS][cdnjs-image]][cdnjs-url] | ||
@@ -69,2 +70,5 @@ [![Greenkeeper badge](https://badges.greenkeeper.io/blakeembrey/pluralize.svg)](https://greenkeeper.io/) | ||
pluralize.plural('paper') //=> "paper" | ||
pluralize.isPlural('test') //=> false | ||
pluralize.isSingular('test') //=> true | ||
``` | ||
@@ -84,3 +88,4 @@ | ||
[coveralls-url]: https://coveralls.io/r/blakeembrey/pluralize?branch=master | ||
[filesize-url]: https://img.shields.io/github/size/blakeembrey/pluralize/pluralize.js.svg?style=flat | ||
[cdnjs-image]: https://img.shields.io/cdnjs/v/pluralize.svg | ||
[cdnjs-url]: https://cdnjs.com/libraries/pluralize |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17022
451
89