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

Add preserveCasing option to addIrregularRule #193

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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 } = {}) {
huanvu marked this conversation as resolved.
Show resolved Hide resolved
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