Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/inflection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ const uncountableWords = [

const regex = {
plural: {
men: new RegExp('^(m|wom)en$', 'gi'),
men: new RegExp('(m|wom)en$', 'gi'),
// singular nouns that end in -men, not plurals of -man (specimen, omen)
menSingular: new RegExp(
'^(a|abdo|acu|agno|albu|bitu|catechu|ceru|cogno|cul|cycla|dol|dura|exa|fla|fora|grava|hy|legu|li|lu|no|nu|o|praeno|puta|regi|ru|se|speci|sta|teg|tegu|vela)men$',
'gi'
),
people: new RegExp('(pe)ople$', 'gi'),
children: new RegExp('(child)ren$', 'gi'),
tia: new RegExp('([ti])a$', 'gi'),
Expand Down Expand Up @@ -383,7 +388,12 @@ const regex = {
},

singular: {
man: new RegExp('^(m|wom)an$', 'gi'),
man: new RegExp('(m|wom)an$', 'gi'),
// nouns that end in -man but are not compounds of man (human, German)
manNonCompound: new RegExp(
'(?:^(?:ata|brah|cai|cay|des|dol|ger|het|mussul|nor|otto|ro|sha|talis|turko)|hu)man$',
'gi'
),
person: new RegExp('(pe)rson$', 'gi'),
child: new RegExp('(child)$', 'gi'),
drive: new RegExp('(drive)$', 'gi'),
Expand Down Expand Up @@ -418,6 +428,9 @@ const regex = {
};

const pluralRules: [RegExp, string?][] = [
[regex.plural.menSingular, '$&s'],
[regex.singular.manNonCompound, '$&s'],

// do not replace if its already a plural word
[regex.plural.men],
[regex.plural.people],
Expand Down Expand Up @@ -494,6 +507,7 @@ const pluralRules: [RegExp, string?][] = [
*/
const singularRules: [RegExp, string?][] = [
// do not replace if its already a singular word
[regex.plural.menSingular],
[regex.singular.man],
[regex.singular.person],
[regex.singular.child],
Expand Down
33 changes: 33 additions & 0 deletions test/inflection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ describe('test .pluralize', function () {
expect(inflection.pluralize('nuclei')).toEqual('nuclei');
expect(inflection.pluralize('nucleuses')).toEqual('nucleuses');
});

it('should pluralize compounds of man and woman', function () {
expect(inflection.pluralize('policeman')).toEqual('policemen');
expect(inflection.pluralize('Fireman')).toEqual('Firemen');
expect(inflection.pluralize('gentleman')).toEqual('gentlemen');
expect(inflection.pluralize('policewoman')).toEqual('policewomen');
expect(inflection.pluralize('policemen')).toEqual('policemen');
expect(inflection.pluralize('policewomen')).toEqual('policewomen');
// -man words that are not compounds of man
expect(inflection.pluralize('superhuman')).toEqual('superhumans');
expect(inflection.pluralize('German')).toEqual('Germans');
expect(inflection.pluralize('shaman')).toEqual('shamans');
expect(inflection.pluralize('talisman')).toEqual('talismans');
// singular nouns that end in -men
expect(inflection.pluralize('specimen')).toEqual('specimens');
expect(inflection.pluralize('omen')).toEqual('omens');
expect(inflection.pluralize('abdomen')).toEqual('abdomens');
});
});

describe('test .singularize', function () {
Expand Down Expand Up @@ -115,6 +133,21 @@ describe('test .singularize', function () {
expect(inflection.singularize('bases')).toEqual('basis');
expect(inflection.singularize('diagnoses')).toEqual('diagnosis');
});

it('should singularize compounds of man and woman', function () {
expect(inflection.singularize('policemen')).toEqual('policeman');
expect(inflection.singularize('Firemen')).toEqual('Fireman');
expect(inflection.singularize('gentlemen')).toEqual('gentleman');
expect(inflection.singularize('policewomen')).toEqual('policewoman');
expect(inflection.singularize('policeman')).toEqual('policeman');
expect(inflection.singularize('superhumans')).toEqual('superhuman');
expect(inflection.singularize('shamans')).toEqual('shaman');
// singular nouns that end in -men
expect(inflection.singularize('specimen')).toEqual('specimen');
expect(inflection.singularize('specimens')).toEqual('specimen');
expect(inflection.singularize('omen')).toEqual('omen');
expect(inflection.singularize('regimen')).toEqual('regimen');
});
});

describe('test .inflect', function () {
Expand Down
Loading