← 中文资源

简体中文版本

Collator 的 sensitivity 梯度

Intl.Collator 的 sensitivity 从 base 到 variant 逐级收严:base 同时忽略大小写与变音符,variant 全都区分;ignorePunctuation 再把标点移出比较。

阅读英文原文

运行结果

以下输出由构建管线在沙箱中真实运行该示例捕获。

base     e~E: eq | e~é: eq
accent   e~E: eq | e~é: diff
case     e~E: diff | e~é: eq
variant  e~E: diff | e~é: diff
punct ignored: true
punct counted: false

实现说明

重复被限制在 for (const s of ladder) 里——其余代码按顺序各跑一遍。 各个操作都建立在顶层绑定之上:ladder、loose、strict。

使用要点

  • 每一步打印都带着自己的标签:punct ignored:、punct counted:。

常见问题

除了语言工具链还要下载什么吗?

除了这个语言的工具链,什么都不用再下载。 状态都挂在模块顶层:ladder、loose、strict。

核心语句

const loose = new Intl.Collator('en', { ignorePunctuation: true, sensitivity: 'variant' });
const strict = new Intl.Collator('en', { sensitivity: 'variant' });
const c = new Intl.Collator('en', { sensitivity: s });

代码示例

const ladder = ['base', 'accent', 'case', 'variant'];
for (const s of ladder) {
  const c = new Intl.Collator('en', { sensitivity: s });
  console.log(
    s.padEnd(8),
    'e~E:', c.compare('e', 'E') === 0 ? 'eq' : 'diff',
    '| e~é:', c.compare('e', 'é') === 0 ? 'eq' : 'diff',
  );
}

// ignorePunctuation 让 a-b 与 a b 相等:标点不参与比较
const loose = new Intl.Collator('en', { ignorePunctuation: true, sensitivity: 'variant' });
console.log('punct ignored:', loose.compare('a-b', 'a b') === 0);
const strict = new Intl.Collator('en', { sensitivity: 'variant' });
console.log('punct counted:', strict.compare('a-b', 'a b') === 0);