实现说明
重复被限制在 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);