pluralize

repository·master·Indexed 25 days ago

https://github.com/plurals/pluralize

A utility library for singularizing and pluralizing words based on predefined rules and customizable patterns. Version 8.0.0 provides functions to transform words based on count, force plural or singular forms, and check word states. It supports extending behavior via addPluralRule, addSingularRule, addIrregularRule, and addUncountableRule.

Tokens
1.2K
Snippets
5
Records
13
Agent score
31%

What's inside pluralize

  1. Import pluralize

    master

    Depending on your environment, use the following methods to import the library:

    Node.js

    Use require to import the module.

    AMD

    Use require within a define block.

    <script> tag

    Include the pluralize.js file directly in your HTML.

    // Node
    var pluralize = require('pluralize')
    
    // AMD
    define(function (require, exports, module) {
      var pluralize = require('pluralize')
    })
  2. Extend pluralization rules

    master

    You can customize how pluralize handles specific words or patterns using the following methods:

    Add a Plural Rule

    Use addPluralRule(regex, replacement) to define a new pluralization pattern using a regular expression.

    Add a Singular Rule

    Use addSingularRule(regex, replacement) to define how a word should be singularized.

    Add an Irregular Rule

    Use addIrregularRule(singular, plural) to map a specific singular word to a specific plural word (e.g., mapping 'irregular' to 'regular').

    Add an Uncountable Rule

    Use addUncountableRule(word) to mark a word as uncountable, meaning it will always return the same form regardless of count.

    // New plural rule
    pluralize.addPluralRule(/gex$/i, 'gexii')
    pluralize.plural('regex') //=> "regexii"
    
    // New singular rule
    pluralize.addSingularRule(/singles$/i, 'singular')
    pluralize.singular('singles') //=> "singular"
    
    // New irregular rule
    pluralize.addIrregularRule('irregular', 'regular')
    pluralize.plural('irregular') //=> "regular"
    
    // Uncountable rule
    pluralize.addUncountableRule('paper')
    pluralize.plural('paper') //=> "paper"
  3. Pluralize and singularize words

    master

    The core pluralize function converts a word to its plural or singular form based on a provided count. It also supports prefixing the result with the count if requested.

    Arguments:

    • word (string): The word to transform.
    • count (number): The quantity of the word (determines if singular or plural form is used).
    • inclusive (boolean): If true, prefixes the result with the number (e.g., 3 ducks).
    pluralize('test') //=> "tests"
    pluralize('test', 0) //=> "tests"
    pluralize('test', 1) //=> "test"
    pluralize('test', 5) //=> "tests"
    pluralize('test', 1, true) //=> "1 test"
    pluralize('test', 5, true) //=> "5 tests"
    pluralize('蘋果', 2, true) //=> "2 蘋果"
  4. Add custom pluralization rules

    master

    You can extend the library's behavior by adding custom rules using addPluralRule(rule, replacement).

    • rule: A RegExp or a string (which will be converted to a RegExp with ^ and $).
    • replacement: A string that can use $0, $1, etc., to refer to captured groups in the rule.