@boringnode/pluralize

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

用于将英文单词复数和单数化的理论读取端口

中文使用文档
文档目录10 节
  1. 安装
  2. 使用量
  3. 简单函数
  4. 使用不同的区域
  5. 自定义规则, 带有缩写类
  6. 创建自定义语言规则et
  7. API 密码
  8. 职能
  9. 缩写类
  10. 构建器导出( E) @boringnode/pluralize/builder (中文(简体) )

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

@boringnode/pluralize 是多元化特性的 TypeScript 端口 理论体现者。 。 。 .

内建支持:

  • 联合国 (单位:千美元)en, (中文(简体) ), english(中文(简体) )
  • 法语 (单位:千美元)fr, (中文(简体) ), french(中文(简体) )

[注] 功能规则的修改(添加,去除,或修改规则)将不被视为断开修改.

安装

npm install @boringnode/pluralize

使用量

简单函数

使用库最简单的方式是具有简单的功能.

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

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

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

使用不同的区域

您可以创建 Inflector 使用不同语言.

import { Inflector } from '@boringnode/pluralize'

const inflector = new Inflector('fr')

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

自定义规则, 带有缩写类

您可以创建 Inflector 实例以添加自定义规则.

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'

创建自定义语言规则et

您可以为其它语言创建和注册自定义语言规则.

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 密码

职能

  • pluralize(word: string): string - 返回单词的复数形式
  • singularize(word: string): string - 返回单词的单数形式

缩写类

方法说明
new Inflector(locale?: string)创建新反射实例( 默认为 'en'(中文(简体) )
Inflector.register(locale, ruleset)注册自定义语言规则et
pluralize(word)返回单词的复数形式
singularize(word)返回单词的单数形式
addIrregular(singular, plural)添加不规则单词映射
addUninflected(word)添加单数和复数之间不变的单词
addPluralRule(pattern, replacement)添加自定义复数化规则
addSingularRule(pattern, replacement)添加自定义单数化规则

构建器导出( E)@boringnode/pluralize/builder(中文(简体) )

用于创建自定义语言规则:

导出说明
pattern创建大小写不敏感regex 的帮助器功能
Patterns未反映文字的图案集
Transformation接口 : { pattern: RegExp, replacement: string }
Transformations一系列变换
Substitution接口 : { from: string, to: string }
Substitutions一组替代物
Ruleset接口 : { regular, uninflected, irregular }

版本与发布记录

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

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

关于 @boringnode/pluralize 的常见问题

@boringnode/pluralize 是什么?

用于将英文单词复数和单数化的理论读取端口

如何安装 @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 和提交记录判断。