diff options
author | isaacs <i@izs.me> | 2021-09-28 21:18:01 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2021-09-28 21:52:07 +0300 |
commit | dbb90f7997900b8ae6026dddaa718efe9a1db2f4 (patch) | |
tree | 2241c89f49fa3d8291d0123ce52026e6cbe088d4 /node_modules | |
parent | e94ddeaca1e75ecc8f54ebcb3df222965e3635d1 (diff) |
fix: use Intl.Collator for string sorting when availableisaacs/string-locale-compare
The npm/cli form of https://github.com/npm/arborist/pull/324
Required adding options support to package used for this.
PR-URL: https://github.com/npm/cli/pull/3809
Credit: @isaacs
Close: #3809
Reviewed-by: @wraithgar
Diffstat (limited to 'node_modules')
-rw-r--r-- | node_modules/@isaacs/string-locale-compare/index.js | 36 | ||||
-rw-r--r-- | node_modules/@isaacs/string-locale-compare/package.json | 2 |
2 files changed, 29 insertions, 9 deletions
diff --git a/node_modules/@isaacs/string-locale-compare/index.js b/node_modules/@isaacs/string-locale-compare/index.js index a6cec27ef..0f68ab677 100644 --- a/node_modules/@isaacs/string-locale-compare/index.js +++ b/node_modules/@isaacs/string-locale-compare/index.js @@ -2,21 +2,41 @@ const hasIntl = typeof Intl === 'object' && !!Intl const Collator = hasIntl && Intl.Collator const cache = new Map() -const collatorCompare = locale => { - const collator = new Collator(locale) +const collatorCompare = (locale, opts) => { + const collator = new Collator(locale, opts) return (a, b) => collator.compare(a, b) } -const localeCompare = locale => (a, b) => a.localeCompare(b, locale) +const localeCompare = (locale, opts) => (a, b) => a.localeCompare(b, locale, opts) -module.exports = locale => { +const knownOptions = [ + 'sensitivity', + 'numeric', + 'ignorePunctuation', + 'caseFirst', +] + +const { hasOwnProperty } = Object.prototype + +module.exports = (locale, options = {}) => { if (!locale || typeof locale !== 'string') throw new TypeError('locale required') - if (cache.has(locale)) - return cache.get(locale) + const opts = knownOptions.reduce((opts, k) => { + if (hasOwnProperty.call(options, k)) { + opts[k] = options[k] + } + return opts + }, {}) + const key = `${locale}\n${JSON.stringify(opts)}` + + if (cache.has(key)) + return cache.get(key) + + const compare = hasIntl + ? collatorCompare(locale, opts) + : localeCompare(locale, opts) + cache.set(key, compare) - const compare = hasIntl ? collatorCompare(locale) : localeCompare(locale) - cache.set(locale, compare) return compare } diff --git a/node_modules/@isaacs/string-locale-compare/package.json b/node_modules/@isaacs/string-locale-compare/package.json index a322c1c92..58de848a0 100644 --- a/node_modules/@isaacs/string-locale-compare/package.json +++ b/node_modules/@isaacs/string-locale-compare/package.json @@ -1,6 +1,6 @@ { "name": "@isaacs/string-locale-compare", - "version": "1.0.1", + "version": "1.1.0", "files": [ "index.js" ], |