Skip to main content

Crate pluralizer

Crate pluralizer 

Source
Expand description

Rust package to pluralize or singularize any word based on a count, inspired by the pluralize NPM package.

A count of exactly 1 produces the singular form; any other count (including 0 and negative counts) produces the plural form. Words that are already in the requested form are kept as they are.

§Example

use pluralizer::pluralize;

// It can convert to plural
assert_eq!(pluralize("House", 2, true), "2 Houses");

// But also can convert to singular
assert_eq!(pluralize("Houses", 1, true), "1 House");

// And keep singularization if needed
assert_eq!(pluralize("House", 1, false), "House");

// Or keep pluralization
assert_eq!(pluralize("Houses", 2, false), "Houses");

§Custom rules

The rule tables can be extended at runtime with add_irregular_rule, add_plural_rule, add_singular_rule and add_uncountable_rule. The tables are global: a rule added from any thread applies to every later call, on every thread.

Irregular words and uncountable words (built-in, or added with UncountableRule::String) are checked first. After that, regex rules are tried from the most recently added one to the oldest, so a custom regex rule takes precedence over the built-in ones.

§Differences from the NPM package

  • The built-in rules mirror the plurals/pluralize GitHub master branch at commit 36f03cd, which is newer than the last NPM release (pluralize@8.0.0). For example my/our and canvas/canvases are irregular, and species is not uncountable.
  • Text that a rule captures from the word keeps its case, and the rest of the replacement takes its case from the whole word rather than from the matched text, so results for mixed-case words can differ: "fooURL" becomes "fooURLs" (NPM: "fooURLS") and "the Lives" becomes "the Life" (NPM: "the life").
  • Rules use the regex crate’s Unicode-aware \b and \w.

Enums§

UncountableRule
An uncountable word rule, passed to add_uncountable_rule.

Functions§

add_irregular_rule
Add an irregular word definition.
add_plural_rule
Add a pluralization rule to the collection.
add_singular_rule
Add a singularization rule to the collection.
add_uncountable_rule
Add an uncountable word rule.
pluralize
Pluralize or singularize a word based on the passed in count.