Pluralize Strings and More...
The pluralizer
is a default shared instance of the Pluralizer
class.
It comes preloaded with standard English rules, irregular forms, and uncountable nouns.
Acknowledgement
This utility is heavily inspired by Blake Embreyβs excellent pluralize package.
- Many of the core regular expressions, irregular word mappings, and uncountable word lists were studied and adapted from the original implementation.
- Additional improvements, extensions, and refactoring were made to fit the needs of this project, but full credit goes to the author for the foundational work and inspiration.
Many thanks to the author for the original work and inspiration.
When to Use
- Use this instance when you donβt need multiple configurations.
- If you need isolated or custom rules, you can create your own
Pluralizer
instance.
β¨ Featuresβ
- β Convert words between singular and plural.
- β
Builtβin support for irregular forms (e.g.
child β children
). - β
Handles uncountable nouns (e.g.
fish
,information
). - β
Modify rules dynamically at runtime:
- Add pluralization or singularization rules.
- Add irregular forms.
- Add uncountable words or regex patterns.
π¦ Importβ
import { pluralizer } from 'nhb-toolbox';
π Quick Usageβ
- Pluralize
- Singularize
- Check
pluralizer.pluralize('child'); // "children"
pluralizer.pluralize('category', { count: 3 }); // "categories"
pluralizer.pluralize('child', { count: 1, inclusive: true }); // "1 child"
- For
options
details see here
pluralizer.toSingular('geese'); // "goose"
pluralizer.toSingular('children'); // "child"
pluralizer.isPlural('children'); // true
pluralizer.isSingular('child'); // true
pluralizer.isPlural('fish'); // true (uncountable)
Note
Always returns true
for uncountable nouns.
π§ Extending Rulesβ
Since pluralizer
is an actual instance of Pluralizer
,
you can extend or modify it at runtime:
// Add a custom plural rule
pluralizer.addPluralRule(/(foo)$/i, '$1bars');
// Add a custom singular rule
pluralizer.addSingularRule(/(bars)$/i, '$1');
// Add an uncountable noun
pluralizer.addUncountable('luggage');
// Add an irregular word
pluralizer.addIrregular('cactus', 'cacti');
These modifications affect all consumers of the shared instance.
π Need your own configuration?β
If you want an isolated instance with its own rules, instantiate the class directly:
import { Pluralizer } from 'nhb-toolbox';
const myPluralizer = new Pluralizer();
myPluralizer.addPluralRule(/(baz)$/i, '$1zes');
console.log(myPluralizer.pluralize('baz')); // bazzes
See the Pluralizer
class docs for full details.
See alsoβ
- Pluralizer class β lowβlevel API for custom instances.
- formatUnitWithPlural β Simple utility for formatting units with pluralization with just
"s"
.
Summaryβ
Use pluralizer
for common useβcases.
Create new Pluralizer()
for isolated/custom rules.