Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
13 changes: 10 additions & 3 deletions pluralize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down