pluralize-esm
Advanced tools
Comparing version
228
dist/main.js
@@ -15,22 +15,14 @@ function $parcel$defineInteropFlag(a) { | ||
var $882b6d93070905b3$var$singularRules = []; | ||
var $882b6d93070905b3$var$uncountables = {}; | ||
var $882b6d93070905b3$var$irregularPlurals = {}; | ||
var $882b6d93070905b3$var$irregularSingles = {}; | ||
const $882b6d93070905b3$var$uncountables = new Set(); | ||
var $882b6d93070905b3$var$irregularPlurals = new Map(); | ||
var $882b6d93070905b3$var$irregularSingles = new Map(); | ||
/** | ||
* Sanitize a pluralization rule to a usable regular expression. | ||
* | ||
* @param {(RegExp|string)} rule | ||
* @return {RegExp} | ||
*/ function $882b6d93070905b3$var$sanitizeRule(rule) { | ||
if (typeof rule === "string") return new RegExp("^" + rule + "$", "i"); | ||
return rule; | ||
} | ||
*/ const $882b6d93070905b3$var$sanitizeRule = (rule)=>typeof rule === "string" ? new RegExp(`^${rule}$`, "i") : rule; | ||
/** | ||
* Pass in a word token to produce a function that can replicate the case on | ||
* another word. | ||
* | ||
* @param {string} word | ||
* @param {string} token | ||
* @return {Function} | ||
*/ function $882b6d93070905b3$var$restoreCase(word, token) { | ||
*/ const $882b6d93070905b3$var$restoreCase = (word, token)=>{ | ||
// Edge case | ||
if (typeof token !== "string") return word; | ||
// Tokens are an exact match. | ||
@@ -46,74 +38,41 @@ if (word === token) return token; | ||
return token.toLowerCase(); | ||
} | ||
}; | ||
/** | ||
* Interpolate a regexp string. | ||
* | ||
* @param {string} str | ||
* @param {Array} args | ||
* @return {string} | ||
*/ function $882b6d93070905b3$var$interpolate(str, args) { | ||
return str.replace(/\$(\d{1,2})/g, function(match, index) { | ||
return args[index] || ""; | ||
}); | ||
} | ||
/** | ||
* Replace a word using a rule. | ||
* | ||
* @param {string} word | ||
* @param {Array} rule | ||
* @return {string} | ||
*/ function $882b6d93070905b3$var$replace(word, rule) { | ||
return word.replace(rule[0], function(match, index) { | ||
var result = $882b6d93070905b3$var$interpolate(rule[1], arguments); | ||
if (match === "") return $882b6d93070905b3$var$restoreCase(word[index - 1], result); | ||
return $882b6d93070905b3$var$restoreCase(match, result); | ||
}); | ||
} | ||
/** | ||
* Sanitize a word by passing in the word and sanitization rules. | ||
* | ||
* @param {string} token | ||
* @param {string} word | ||
* @param {Array} rules | ||
* @return {string} | ||
*/ function $882b6d93070905b3$var$sanitizeWord(token, word, rules) { | ||
*/ const $882b6d93070905b3$var$sanitizeWord = (token, word, rules)=>{ | ||
// Empty string or doesn't need fixing. | ||
if (!token.length || $882b6d93070905b3$var$uncountables.hasOwnProperty(token)) return word; | ||
var len = rules.length; | ||
if (!token.length || $882b6d93070905b3$var$uncountables.has(token)) return word; | ||
// Iterate over the sanitization rules and use the first one to match. | ||
let { length: len } = rules; | ||
while(len--){ | ||
var rule = rules[len]; | ||
if (rule[0].test(word)) return $882b6d93070905b3$var$replace(word, rule); | ||
const rule = rules[len]; | ||
if (rule[0].test(word)) // Replace a word using a rule. | ||
return word.replace(rule[0], (...args)=>{ | ||
const [match, index1] = args; | ||
// Interpolate a regexp string. | ||
const result = rule[1].replace(/\$(\d{1,2})/g, (_, index)=>args[index] || ""); | ||
if (match === "") return $882b6d93070905b3$var$restoreCase(word[index1 - 1], result); | ||
return $882b6d93070905b3$var$restoreCase(match, result); | ||
}); | ||
} | ||
return word; | ||
} | ||
}; | ||
const $882b6d93070905b3$var$compute = (word, replaceMap, keepMap, rules)=>{ | ||
// Get the correct token and case restoration functions. | ||
const token = word.toLowerCase(); | ||
// Check against the keep object map. | ||
if (keepMap.has(token)) return $882b6d93070905b3$var$restoreCase(word, token); | ||
// Check against the replacement map for a direct word replacement. | ||
if (replaceMap.has(token)) return $882b6d93070905b3$var$restoreCase(word, replaceMap.get(token)); | ||
// Run all the rules against the word. | ||
return $882b6d93070905b3$var$sanitizeWord(token, word, rules); | ||
}; | ||
/** | ||
* Replace a word with the updated word. | ||
* | ||
* @param {Object} replaceMap | ||
* @param {Object} keepMap | ||
* @param {Array} rules | ||
* @return {Function} | ||
*/ function $882b6d93070905b3$var$replaceWord(replaceMap, keepMap, rules) { | ||
return function(word) { | ||
// Get the correct token and case restoration functions. | ||
var token = word.toLowerCase(); | ||
// Check against the keep object map. | ||
if (keepMap.hasOwnProperty(token)) return $882b6d93070905b3$var$restoreCase(word, token); | ||
// Check against the replacement map for a direct word replacement. | ||
if (replaceMap.hasOwnProperty(token)) return $882b6d93070905b3$var$restoreCase(word, replaceMap[token]); | ||
// Run all the rules against the word. | ||
return $882b6d93070905b3$var$sanitizeWord(token, word, rules); | ||
}; | ||
} | ||
/** | ||
* Check if a word is part of the map. | ||
*/ function $882b6d93070905b3$var$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 $882b6d93070905b3$var$sanitizeWord(token, token, rules) === token; | ||
}; | ||
} | ||
*/ const $882b6d93070905b3$var$mapHas = (word, replaceMap, keepMap, rules)=>{ | ||
const token = word.toLowerCase(); | ||
if (keepMap.has(token)) return true; | ||
if (replaceMap.has(token)) return false; | ||
return $882b6d93070905b3$var$sanitizeWord(token, token, rules) === token; | ||
}; | ||
/** | ||
@@ -125,6 +84,7 @@ * Pluralize or singularize a word based on the passed in count. | ||
* @param inclusive | ||
*/ function $882b6d93070905b3$var$pluralize(word, count, inclusive) { | ||
var pluralized = count === 1 ? $882b6d93070905b3$var$pluralize.singular(word) : $882b6d93070905b3$var$pluralize.plural(word); | ||
return (inclusive ? count + " " : "") + pluralized; | ||
} | ||
*/ const $882b6d93070905b3$var$pluralize = (word, count, inclusive)=>{ | ||
const pluralized = count === 1 ? $882b6d93070905b3$var$pluralize.singular(word) : $882b6d93070905b3$var$pluralize.plural(word); | ||
if (inclusive) return `${count} ${pluralized}`; | ||
return pluralized; | ||
}; | ||
/** | ||
@@ -134,19 +94,9 @@ * Pluralize a word based. | ||
* @param word | ||
*/ $882b6d93070905b3$var$pluralize.plural = $882b6d93070905b3$var$replaceWord($882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$pluralRules); | ||
*/ $882b6d93070905b3$var$pluralize.plural = (word)=>$882b6d93070905b3$var$compute(word, $882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$pluralRules); | ||
/** | ||
* Test if provided word is plural. | ||
* | ||
* @param word | ||
*/ $882b6d93070905b3$var$pluralize.isPlural = $882b6d93070905b3$var$checkWord($882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$pluralRules); | ||
/** | ||
* Singularize a word based. | ||
* | ||
* @param word | ||
*/ $882b6d93070905b3$var$pluralize.singular = $882b6d93070905b3$var$replaceWord($882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$singularRules); | ||
*/ $882b6d93070905b3$var$pluralize.singular = (word)=>$882b6d93070905b3$var$compute(word, $882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$singularRules); | ||
/** | ||
* Test if provided word is singular. | ||
* | ||
* @param word | ||
*/ $882b6d93070905b3$var$pluralize.isSingular = $882b6d93070905b3$var$checkWord($882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$singularRules); | ||
/** | ||
* Add a pluralization rule to the collection. | ||
@@ -156,6 +106,6 @@ * | ||
* @param replacement | ||
*/ $882b6d93070905b3$var$pluralize.addPluralRule = function(rule, replacement) { | ||
*/ $882b6d93070905b3$var$pluralize.addPluralRule = (rule, replacement2)=>{ | ||
$882b6d93070905b3$var$pluralRules.push([ | ||
$882b6d93070905b3$var$sanitizeRule(rule), | ||
replacement | ||
replacement2 | ||
]); | ||
@@ -166,37 +116,45 @@ }; | ||
* | ||
* @param {(string|RegExp)} rule | ||
* @param {string} replacement | ||
*/ $882b6d93070905b3$var$pluralize.addSingularRule = function(rule, replacement) { | ||
* @param rule | ||
* @param replacement | ||
*/ $882b6d93070905b3$var$pluralize.addSingularRule = (rule, replacement3)=>{ | ||
$882b6d93070905b3$var$singularRules.push([ | ||
$882b6d93070905b3$var$sanitizeRule(rule), | ||
replacement | ||
replacement3 | ||
]); | ||
}; | ||
/** | ||
* Add an irregular word definition. | ||
* | ||
* @param single | ||
* @param plural | ||
*/ $882b6d93070905b3$var$pluralize.addIrregularRule = (single1, plural1)=>{ | ||
const _plural = plural1.toLowerCase(); | ||
const _single = single1.toLowerCase(); | ||
$882b6d93070905b3$var$irregularSingles.set(_single, _plural); | ||
$882b6d93070905b3$var$irregularPlurals.set(_plural, _single); | ||
}; | ||
/** | ||
* Add an uncountable word rule. | ||
* | ||
* @param {(string|RegExp)} word | ||
*/ $882b6d93070905b3$var$pluralize.addUncountableRule = function(word) { | ||
if (typeof word === "string") { | ||
$882b6d93070905b3$var$uncountables[word.toLowerCase()] = true; | ||
* @param rule | ||
*/ $882b6d93070905b3$var$pluralize.addUncountableRule = (rule)=>{ | ||
if (typeof rule === "string") { | ||
$882b6d93070905b3$var$uncountables.add(rule.toLowerCase()); | ||
return; | ||
} | ||
// Set singular and plural references for the word. | ||
$882b6d93070905b3$var$pluralize.addPluralRule(word, "$0"); | ||
$882b6d93070905b3$var$pluralize.addSingularRule(word, "$0"); | ||
$882b6d93070905b3$var$pluralize.addPluralRule(rule, "$0"); | ||
$882b6d93070905b3$var$pluralize.addSingularRule(rule, "$0"); | ||
}; | ||
/** | ||
* Add an irregular word definition. | ||
* Test if provided word is plural. | ||
* | ||
* @param {string} single | ||
* @param {string} plural | ||
*/ $882b6d93070905b3$var$pluralize.addIrregularRule = function(single, plural) { | ||
plural = plural.toLowerCase(); | ||
single = single.toLowerCase(); | ||
$882b6d93070905b3$var$irregularSingles[single] = plural; | ||
$882b6d93070905b3$var$irregularPlurals[plural] = single; | ||
} /** | ||
* Irregular rules. | ||
*/ ; | ||
[ | ||
* @param word | ||
*/ $882b6d93070905b3$var$pluralize.isPlural = (word)=>$882b6d93070905b3$var$mapHas(word, $882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$pluralRules); | ||
/** | ||
* Test if provided word is singular. | ||
* | ||
* @param word | ||
*/ $882b6d93070905b3$var$pluralize.isSingular = (word)=>$882b6d93070905b3$var$mapHas(word, $882b6d93070905b3$var$irregularPlurals, $882b6d93070905b3$var$irregularSingles, $882b6d93070905b3$var$singularRules); | ||
const $882b6d93070905b3$var$defaultIrregulars = [ | ||
// Pronouns. | ||
@@ -415,8 +373,4 @@ [ | ||
], | ||
].forEach(function(rule) { | ||
return $882b6d93070905b3$var$pluralize.addIrregularRule(rule[0], rule[1]); | ||
}) /** | ||
* Pluralization rules. | ||
*/ ; | ||
[ | ||
]; | ||
const $882b6d93070905b3$var$defaultPlurals = [ | ||
[ | ||
@@ -522,8 +476,4 @@ /s?$/i, | ||
], | ||
].forEach(function(rule) { | ||
return $882b6d93070905b3$var$pluralize.addPluralRule(rule[0], rule[1]); | ||
}) /** | ||
* Singularization rules. | ||
*/ ; | ||
[ | ||
]; | ||
const $882b6d93070905b3$var$defaultSingles = [ | ||
[ | ||
@@ -625,8 +575,4 @@ /s$/i, | ||
], | ||
].forEach(function(rule) { | ||
return $882b6d93070905b3$var$pluralize.addSingularRule(rule[0], rule[1]); | ||
}) /** | ||
* Uncountable rules. | ||
*/ ; | ||
[ | ||
]; | ||
const $882b6d93070905b3$var$defaultUncountables = [ | ||
// Singular words with no plurals. | ||
@@ -735,6 +681,12 @@ "adulthood", | ||
/sheep$/i, | ||
].forEach($882b6d93070905b3$var$pluralize.addUncountableRule); | ||
var $882b6d93070905b3$export$2e2bcd8739ae039 = $882b6d93070905b3$var$pluralize; | ||
]; | ||
// Now lets add all the defaults | ||
for (const [single, plural] of $882b6d93070905b3$var$defaultIrregulars)$882b6d93070905b3$var$pluralize.addIrregularRule(single, plural); | ||
for (const [search, replacement] of $882b6d93070905b3$var$defaultPlurals)$882b6d93070905b3$var$pluralize.addPluralRule(search, replacement); | ||
for (const [search1, replacement1] of $882b6d93070905b3$var$defaultSingles)$882b6d93070905b3$var$pluralize.addSingularRule(search1, replacement1); | ||
for (const search2 of $882b6d93070905b3$var$defaultUncountables)$882b6d93070905b3$var$pluralize.addUncountableRule(search2); | ||
var // D O N E, let's export! š | ||
$882b6d93070905b3$export$2e2bcd8739ae039 = $882b6d93070905b3$var$pluralize; | ||
//# sourceMappingURL=main.js.map |
@@ -0,1 +1,2 @@ | ||
export type Rule = RegExp | string; | ||
/** | ||
@@ -8,15 +9,58 @@ * Pluralize or singularize a word based on the passed in count. | ||
*/ | ||
export default function pluralize(word: string, count?: number, inclusive?: boolean): string; | ||
declare namespace pluralize { | ||
var plural: (word: any) => string; | ||
var isPlural: (word: string) => boolean; | ||
var singular: (word: any) => string; | ||
var isSingular: (word: string) => boolean; | ||
var addPluralRule: (rule: any, replacement: any) => void; | ||
var addSingularRule: (rule: any, replacement: any) => void; | ||
var addUncountableRule: (word: any) => void; | ||
var addIrregularRule: (single: string, plural: string) => void; | ||
} | ||
declare const pluralize: { | ||
(word: string, count?: number, inclusive?: boolean): string; | ||
/** | ||
* Pluralize a word based. | ||
* | ||
* @param word | ||
*/ | ||
plural(word: string): string; | ||
/** | ||
* Singularize a word based. | ||
* | ||
* @param word | ||
*/ | ||
singular(word: string): string; | ||
/** | ||
* Add a pluralization rule to the collection. | ||
* | ||
* @param rule | ||
* @param replacement | ||
*/ | ||
addPluralRule(rule: Rule, replacement: string): void; | ||
/** | ||
* Add a singularization rule to the collection. | ||
* | ||
* @param rule | ||
* @param replacement | ||
*/ | ||
addSingularRule(rule: Rule, replacement: string): void; | ||
/** | ||
* Add an irregular word definition. | ||
* | ||
* @param single | ||
* @param plural | ||
*/ | ||
addIrregularRule(single: string, plural: string): void; | ||
/** | ||
* Add an uncountable word rule. | ||
* | ||
* @param rule | ||
*/ | ||
addUncountableRule(rule: Rule): void; | ||
/** | ||
* Test if provided word is plural. | ||
* | ||
* @param word | ||
*/ | ||
isPlural(word: string): boolean; | ||
/** | ||
* Test if provided word is singular. | ||
* | ||
* @param word | ||
*/ | ||
isSingular(word: string): boolean; | ||
}; | ||
export default pluralize; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "pluralize-esm", | ||
"version": "1.0.0", | ||
"version": "9.0.0", | ||
"description": "Pluralize and singularize any word", | ||
@@ -23,7 +23,7 @@ "keywords": [ | ||
"require": "./dist/main.js", | ||
"default": "./dist/module.js" | ||
"default": "./dist/module.mjs" | ||
} | ||
}, | ||
"main": "dist/main.js", | ||
"module": "dist/module.js", | ||
"module": "dist/module.mjs", | ||
"source": "src/index.ts", | ||
@@ -37,11 +37,10 @@ "types": "dist/types.d.ts", | ||
"build": "parcel build", | ||
"check": "tsc --noEmit", | ||
"format": "prettier --write .", | ||
"lint": "semistandard", | ||
"package-check": "package-check", | ||
"prepublishOnly": "npm run build", | ||
"test": "npm run lint && npm run test-cov", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec --bail", | ||
"test-spec": "mocha -R spec --bail" | ||
"test": "node --test" | ||
}, | ||
"prettier": { | ||
"arrowParens": "avoid", | ||
"semi": false, | ||
@@ -54,5 +53,2 @@ "singleQuote": true | ||
"@skypack/package-check": "^0.2.2", | ||
"chai": "^4.0.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.0.0", | ||
"parcel": "^2.6.0", | ||
@@ -62,5 +58,4 @@ "prettier": "^2.6.2", | ||
"rimraf": "^3.0.2", | ||
"semistandard": "^12.0.0", | ||
"typescript": "^4.7.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8
-33.33%89748
-0.83%1413
-3.55%90
-1.1%1
Infinity%