Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create addRestoreCaseException function #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ pluralize.plural('paper') //=> "paper"
// Example of asking whether a word looks singular or plural:
pluralize.isPlural('test') //=> false
pluralize.isSingular('test') //=> true

// Example of adding a token exception whose case should not be restored:
pluralize.plural('promo ID') //=> 'promo IDS'
pluralize.addRestoreCaseException('IDs')
pluralize.plural('promo ID') //=> 'promo IDs'
```

## License
Expand Down
17 changes: 15 additions & 2 deletions pluralize.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
var uncountables = {};
var irregularPlurals = {};
var irregularSingles = {};
var restoreCaseExceptions = [];

/**
* Sanitize a pluralization rule to a usable regular expression.
Expand All @@ -31,7 +32,7 @@
*/
function sanitizeRule (rule) {
if (typeof rule === 'string') {
return new RegExp('^' + rule + '$', 'i');
return new RegExp(rule + '$', 'i');
}

return rule;
Expand All @@ -46,6 +47,9 @@
* @return {Function}
*/
function restoreCase (word, token) {
// Do not restore the case of specified tokens
if (restoreCaseExceptions.indexOf(token) !== -1) return token;

// Tokens are an exact match.
if (word === token) return token;

Expand Down Expand Up @@ -115,7 +119,6 @@
// Iterate over the sanitization rules and use the first one to match.
while (len--) {
var rule = rules[len];

if (rule[0].test(word)) return replace(word, rule);
}

Expand Down Expand Up @@ -225,6 +228,16 @@
pluralRules.push([sanitizeRule(rule), replacement]);
};

/**
* Add an exception to restoreCase.
*
* @param {string} exception
*/

pluralize.addRestoreCaseException = function (exception) {
restoreCaseExceptions.push(exception);
};

/**
* Add a singularization rule to the collection.
*
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,5 +832,12 @@ describe('pluralize', function () {

expect(pluralize.singular('mornings')).to.equal('suck');
});

it('will skip restoring case on specified token exceptions', function () {
expect(pluralize.plural('promo ID')).to.equal('promo IDS');
pluralize.addRestoreCaseException('IDs');
pluralize.addPluralRule('ID', 'IDs');
expect(pluralize.plural('promo ID')).to.equal('promo IDs');
});
});
});