blob: a6cec27efb8ecd15ee593d8590b9f35a8c796a8e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
const hasIntl = typeof Intl === 'object' && !!Intl
const Collator = hasIntl && Intl.Collator
const cache = new Map()
const collatorCompare = locale => {
const collator = new Collator(locale)
return (a, b) => collator.compare(a, b)
}
const localeCompare = locale => (a, b) => a.localeCompare(b, locale)
module.exports = locale => {
if (!locale || typeof locale !== 'string')
throw new TypeError('locale required')
if (cache.has(locale))
return cache.get(locale)
const compare = hasIntl ? collatorCompare(locale) : localeCompare(locale)
cache.set(locale, compare)
return compare
}
|