From f4c00b9a5eb28b20823efd5a74ba86744c5cb726 Mon Sep 17 00:00:00 2001 From: Huan Vu Date: Mon, 25 Apr 2022 10:00:06 -0700 Subject: [PATCH 1/3] Add preserveCasing option to addIrregularRule --- Readme.md | 5 +++++ pluralize.js | 13 ++++++++++--- test.js | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index c000ada..a41d08a 100644 --- a/Readme.md +++ b/Readme.md @@ -73,6 +73,11 @@ pluralize.plural('irregular') //=> "irregulars" pluralize.addIrregularRule('irregular', 'regular') pluralize.plural('irregular') //=> "regular" +// Example of new irregular rule preserving casing, e.g. "ID" -> "IDs": +pluralize.plural('ID') //=> "IDS" +pluralize.addIrregularRule('ID', 'IDs', { preserveCasing: true }) +pluralize.plural('ID') //=> "IDs" + // Example of uncountable rule (rules without singular/plural in context): pluralize.plural('paper') //=> "papers" pluralize.addUncountableRule('paper') diff --git a/pluralize.js b/pluralize.js index 65dfcd5..6ff8922 100644 --- a/pluralize.js +++ b/pluralize.js @@ -132,6 +132,11 @@ */ function replaceWord (replaceMap, keepMap, rules) { return function (word) { + // If replaceMap has the exact casing, we preserve the casing + if (replaceMap.hasOwnProperty(word)) { + return replaceMap[word]; + } + // Get the correct token and case restoration functions. var token = word.toLowerCase(); @@ -257,9 +262,11 @@ * @param {string} single * @param {string} plural */ - pluralize.addIrregularRule = function (single, plural) { - plural = plural.toLowerCase(); - single = single.toLowerCase(); + pluralize.addIrregularRule = function (single, plural, { preserveCasing = false } = {}) { + if (!preserveCasing) { + plural = plural.toLowerCase(); + single = single.toLowerCase(); + } irregularSingles[single] = plural; irregularPlurals[plural] = single; diff --git a/test.js b/test.js index fc84d37..49cc56b 100644 --- a/test.js +++ b/test.js @@ -801,6 +801,14 @@ describe('pluralize', function () { expect(pluralize('irregular')).to.equal('regular'); }); + it('should allow new irregular words with preserved casing', function () { + expect(pluralize('ID')).to.equal('IDS'); + + pluralize.addIrregularRule('ID', 'IDs', { preserveCasing: true }); + + expect(pluralize('ID')).to.equal('IDs'); + }); + it('should allow new plural matching rules', function () { expect(pluralize.plural('regex')).to.equal('regexes'); From acd3c154e7aa30a3d0095c22fdbb5f008ead901b Mon Sep 17 00:00:00 2001 From: Huan Vu Date: Mon, 2 May 2022 21:37:03 -0700 Subject: [PATCH 2/3] Remove spreading --- pluralize.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pluralize.js b/pluralize.js index 6ff8922..5c682e4 100644 --- a/pluralize.js +++ b/pluralize.js @@ -262,7 +262,9 @@ * @param {string} single * @param {string} plural */ - pluralize.addIrregularRule = function (single, plural, { preserveCasing = false } = {}) { + pluralize.addIrregularRule = function (single, plural, options) { + var preserveCasing = !!(options && options.preserveCasing); + if (!preserveCasing) { plural = plural.toLowerCase(); single = single.toLowerCase(); From e344930fb1b19b35410c36af7f3d2451de8cc045 Mon Sep 17 00:00:00 2001 From: Huan Vu Date: Mon, 2 May 2022 21:43:41 -0700 Subject: [PATCH 3/3] Semistandard fixes --- pluralize.js | 29 +++++++++++++++-------------- test.js | 10 +++++----- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pluralize.js b/pluralize.js index 5c682e4..1bccc35 100644 --- a/pluralize.js +++ b/pluralize.js @@ -17,11 +17,11 @@ })(this, function () { // Rule storage - pluralize and singularize need to be run sequentially, // while other rules can be optimized using an object for instant lookups. - var pluralRules = []; - var singularRules = []; - var uncountables = {}; - var irregularPlurals = {}; - var irregularSingles = {}; + const pluralRules = []; + const singularRules = []; + const uncountables = {}; + const irregularPlurals = {}; + const irregularSingles = {}; /** * Sanitize a pluralization rule to a usable regular expression. @@ -86,7 +86,7 @@ */ function replace (word, rule) { return word.replace(rule[0], function (match, index) { - var result = interpolate(rule[1], arguments); + const result = interpolate(rule[1], arguments); if (match === '') { return restoreCase(word[index - 1], result); @@ -110,11 +110,11 @@ return word; } - var len = rules.length; + let len = rules.length; // Iterate over the sanitization rules and use the first one to match. while (len--) { - var rule = rules[len]; + const rule = rules[len]; if (rule[0].test(word)) return replace(word, rule); } @@ -138,7 +138,7 @@ } // Get the correct token and case restoration functions. - var token = word.toLowerCase(); + const token = word.toLowerCase(); // Check against the keep object map. if (keepMap.hasOwnProperty(token)) { @@ -160,7 +160,7 @@ */ function checkWord (replaceMap, keepMap, rules, bool) { return function (word) { - var token = word.toLowerCase(); + const token = word.toLowerCase(); if (keepMap.hasOwnProperty(token)) return true; if (replaceMap.hasOwnProperty(token)) return false; @@ -178,8 +178,9 @@ * @return {string} */ function pluralize (word, count, inclusive) { - var pluralized = count === 1 - ? pluralize.singular(word) : pluralize.plural(word); + const pluralized = count === 1 + ? pluralize.singular(word) + : pluralize.plural(word); return (inclusive ? count + ' ' : '') + pluralized; } @@ -263,8 +264,8 @@ * @param {string} plural */ pluralize.addIrregularRule = function (single, plural, options) { - var preserveCasing = !!(options && options.preserveCasing); - + const preserveCasing = !!(options && options.preserveCasing); + if (!preserveCasing) { plural = plural.toLowerCase(); single = single.toLowerCase(); diff --git a/test.js b/test.js index 49cc56b..1b104e1 100644 --- a/test.js +++ b/test.js @@ -1,14 +1,14 @@ /* global describe, it */ -var expect = require('chai').expect; -var pluralize = require('./'); +const expect = require('chai').expect; +const pluralize = require('./'); /** * Standard singular/plural matches. * * @type {Array} */ -var BASIC_TESTS = [ +const BASIC_TESTS = [ // Uncountables. ['firmware', 'firmware'], ['fish', 'fish'], @@ -677,7 +677,7 @@ var BASIC_TESTS = [ * * @type {Array} */ -var SINGULAR_TESTS = [ +const SINGULAR_TESTS = [ ['dingo', 'dingos'], ['mango', 'mangoes'], ['echo', 'echos'], @@ -692,7 +692,7 @@ var SINGULAR_TESTS = [ * * @type {Array} */ -var PLURAL_TESTS = [ +const PLURAL_TESTS = [ ['plateaux', 'plateaux'], ['axis', 'axes'], ['basis', 'bases'],