A spec-compliant polyfill/ponyfill for Intl.Collator.
Intl.Collator provides locale-aware string comparison for sorting and
searching. The FormatJS polyfill implements the ECMA-402 API surface and uses
generated Unicode Collation Algorithm (UCA) and CLDR collation data for
runtime comparison.
Installation#
npm i @formatjs/intl-collator
Usage#
Global polyfill#
import '@formatjs/intl-collator/polyfill.js'
const collator = new Intl.Collator('sv')
const sorted = ['ä', 'z', 'å'].sort(collator.compare)
Force polyfill#
import '@formatjs/intl-collator/polyfill-force.js'
ES modules#
import {Collator} from '@formatjs/intl-collator'
const collator = new Collator('zh-u-co-pinyin', {
sensitivity: 'variant',
})
Supported Features#
Intl.Collator(locales, options)Intl.Collator.supportedLocalesOf(locales, options)Intl.Collator.prototype.compareIntl.Collator.prototype.resolvedOptions()- Unicode extension keys
co,kn, andkf numeric,caseFirst,sensitivity,usage, andignorePunctuation- Root CLDR collation data plus generated locale collation metadata and tailorings
Data Model#
The package consumes generated CLDR collation data from
@formatjs_generated/cldr.collation. Build-time scripts parse CLDR common UCA
data and LDML collation XML so applications do not parse XML at runtime.
Tests#
This package is tested against FormatJS unit tests, parser tests, and
conformance data generated from ICU4J and native Intl.Collator.