@boringnode/pluralize

中文文档
0.1.0发布于 2025/12/31MITTypeScriptES Module零依赖近 30 天下载 1.2万

A TypeScript port of Doctrine Inflector to pluralize and singularize English words

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. Simple Functions
  4. Using a Different Locale
  5. Custom Rules with Inflector Class
  6. Creating a Custom Language Ruleset
  7. API
  8. Functions
  9. Inflector Class
  10. Builder Exports ( @boringnode/pluralize/builder )

typescript-image npm-image npm-download-image license-image

@boringnode/pluralize is a TypeScript port of the pluralization features from Doctrine Inflector.

Built-in support for:

  • English (en, english)
  • French (fr, french)

[!NOTE] Changes to inflection rules (adding, removing, or modifying rules) will not be considered as breaking changes.

Installation

npm install @boringnode/pluralize

Usage

Simple Functions

The easiest way to use the library is with the simple functions.

import { pluralize, singularize } from '@boringnode/pluralize'

pluralize('word') // 'words'
pluralize('person') // 'people'
pluralize('child') // 'children'

singularize('words') // 'word'
singularize('people') // 'person'
singularize('children') // 'child'

Using a Different Locale

You can create an Inflector instance with a different locale.

import { Inflector } from '@boringnode/pluralize'

const inflector = new Inflector('fr')

inflector.pluralize('cheval')   // 'chevaux'
inflector.pluralize('bijou')    // 'bijoux'
inflector.singularize('chevaux') // 'cheval'

Custom Rules with Inflector Class

You can create an Inflector instance to add custom rules.

import { Inflector } from '@boringnode/pluralize'

const inflector = new Inflector()
  .addIrregular('gex', 'gexes')
  .addUninflected('pokemon')
  .addPluralRule(/(.*)gon$/i, '$1gons')
  .addSingularRule(/(.*)gons$/i, '$1gon')

inflector.pluralize('gex') // 'gexes'
inflector.pluralize('pokemon') // 'pokemon'
inflector.singularize('dragons') // 'dragon'

Creating a Custom Language Ruleset

You can create and register custom language rulesets for other languages.

import { Inflector, type LanguageRuleset } from '@boringnode/pluralize'
import {
  pattern,
  Patterns,
  Substitutions,
  Transformations,
} from '@boringnode/pluralize/builder'

const SpanishRuleset: LanguageRuleset = {
  getSingularRuleset: () => ({
    regular: new Transformations([
      { pattern: pattern('es$'), replacement: '' },
      { pattern: pattern('s$'), replacement: '' },
    ]),
    uninflected: new Patterns([pattern('lunes'), pattern('martes')]),
    irregular: new Substitutions([{ from: 'hombres', to: 'hombre' }]),
  }),
  getPluralRuleset: () => ({
    regular: new Transformations([
      { pattern: pattern('[aeiou]$'), replacement: '$&s' },
      { pattern: pattern('$'), replacement: 'es' },
    ]),
    uninflected: new Patterns([pattern('lunes'), pattern('martes')]),
    irregular: new Substitutions([{ from: 'hombre', to: 'hombres' }]),
  }),
}

// Register the ruleset
Inflector.register('es', SpanishRuleset)

// Use it
const inflector = new Inflector('es')
inflector.pluralize('gato') // 'gatos'

API

Functions

  • pluralize(word: string): string - Returns the plural form of a word
  • singularize(word: string): string - Returns the singular form of a word

Inflector Class

MethodDescription
new Inflector(locale?: string)Creates a new inflector instance (defaults to 'en')
Inflector.register(locale, ruleset)Registers a custom language ruleset
pluralize(word)Returns the plural form of a word
singularize(word)Returns the singular form of a word
addIrregular(singular, plural)Adds an irregular word mapping
addUninflected(word)Adds a word that doesn't change between singular and plural
addPluralRule(pattern, replacement)Adds a custom pluralization rule
addSingularRule(pattern, replacement)Adds a custom singularization rule

Builder Exports (@boringnode/pluralize/builder)

For creating custom language rulesets:

ExportDescription
patternHelper function to create a case-insensitive regex
PatternsA collection of patterns for uninflected words
TransformationInterface: { pattern: RegExp, replacement: string }
TransformationsA collection of transformations
SubstitutionInterface: { from: string, to: string }
SubstitutionsA collection of substitutions
RulesetInterface: { regular, uninflected, irregular }

版本与发布记录

@boringnode/pluralize 共发布 1 个版本,首次发布于 2025/12/31;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.0latest2025/12/3110 个月前

关于 @boringnode/pluralize 的常见问题

@boringnode/pluralize 是什么?

A TypeScript port of Doctrine Inflector to pluralize and singularize English words

如何安装 @boringnode/pluralize?

在项目目录运行 npm install @boringnode/pluralize,或使用 pnpm add / yarn add / bun add。

@boringnode/pluralize 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@boringnode/pluralize 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@boringnode/pluralize 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@boringnode/pluralize 可以商用吗?

@boringnode/pluralize 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@boringnode/pluralize 的下载量有多少?

根据 npm 官方下载统计,@boringnode/pluralize 在 2026/10/09 – 2026/11/07 这 30 天内被下载 11,619 次,日均约 387 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@boringnode/pluralize 还在维护吗?

最新版本 0.1.0 发布于 2025/12/31(10 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/12/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。