Skip to content

Fix pluralize/singularize replacing an irregular word that is not at the end - #541

Merged
NullVoxPopuli merged 1 commit into
emberjs:masterfrom
bardrop-oss:fix-irregular-anchor-end
Nov 16, 2026
Merged

NullVoxPopuli merged 1 commit into
emberjs:masterfrom
bardrop-oss:fix-irregular-anchor-end

Conversation

@bardrop

@bardrop bardrop commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Fixes #540

inflect() checks that an irregular key matches at the end of the word (lowercase.match(`${key}$`)), but then does word.replace(new RegExp(rule, 'i'), substitution), which replaces the first match anywhere in the word. So when the irregular word also shows up earlier, the wrong part is changed:

call before after
pluralize('management/chairman') menagement/chairman management/chairmen
pluralize('personal-spokesperson') peopleal-spokesperson personal-spokespeople
pluralize('personalContactPerson') PeoplealContactPerson personalContactPeople
pluralize('child/grandchild') children/grandchild child/grandchildren
singularize('department-chairmen') departmant-chairmen department-chairman
singularize('personalContactPerson') PersonalContactPerson personalContactPerson

The fix anchors the replace regex to the end too (`${rule}$`), the same as the check above it. Words where the irregular part only appears at the end are unchanged.

Added a unit test with the cases above. It fails on master and passes with the change; the full suite (55 tests) passes in headless Chrome. prettier --check, eslint and ember-tsc --noEmit are clean.

inflect() checked that the irregular key matches at the end, but then
replaced the first match anywhere in the word, so pluralize('management/chairman')
gave 'menagement/chairman'. Anchor the replace regex to the end too.

Fixes #540
@NullVoxPopuli
NullVoxPopuli merged commit 89bd261 into emberjs:master Nov 16, 2026
12 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pluralize and singularize replace the first occurrence of an irregular word instead of the trailing match

2 participants