@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 } |