Skip to content

Commit dbb90f7

Browse files
committed
fix: use Intl.Collator for string sorting when available
The npm/cli form of npm/arborist#324 Required adding options support to package used for this. PR-URL: #3809 Credit: @isaacs Close: #3809 Reviewed-by: @wraithgar
1 parent e94ddea commit dbb90f7

File tree

13 files changed

+60
-33
lines changed

13 files changed

+60
-33
lines changed

lib/cache.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const semver = require('semver')
88
const BaseCommand = require('./base-command.js')
99
const npa = require('npm-package-arg')
1010
const jsonParse = require('json-parse-even-better-errors')
11+
const localeCompare = require('@isaacs/string-locale-compare')('en')
1112

1213
const searchCachePackage = async (path, spec, cacheKeys) => {
1314
const parsed = npa(spec)
@@ -212,10 +213,10 @@ class Cache extends BaseCommand {
212213
for (const key of keySet)
213214
results.add(key)
214215
}
215-
[...results].sort((a, b) => a.localeCompare(b, 'en')).forEach(key => this.npm.output(key))
216+
[...results].sort(localeCompare).forEach(key => this.npm.output(key))
216217
return
217218
}
218-
cacheKeys.sort((a, b) => a.localeCompare(b, 'en')).forEach(key => this.npm.output(key))
219+
cacheKeys.sort(localeCompare).forEach(key => this.npm.output(key))
219220
}
220221
}
221222

lib/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const writeFile = promisify(fs.writeFile)
1010
const { spawn } = require('child_process')
1111
const { EOL } = require('os')
1212
const ini = require('ini')
13+
const localeCompare = require('@isaacs/string-locale-compare')('en')
1314

1415
// take an array of `[key, value, k2=v2, k3, v3, ...]` and turn into
1516
// { key: value, k2: v2, k3: v3 }
@@ -209,7 +210,7 @@ class Config extends BaseCommand {
209210
; Configs like \`//<hostname>/:_authToken\` are auth that is restricted
210211
; to the registry host specified.
211212
212-
${data.split('\n').sort((a, b) => a.localeCompare(b, 'en')).join('\n').trim()}
213+
${data.split('\n').sort(localeCompare).join('\n').trim()}
213214
214215
;;;;
215216
; all available options shown below with default values
@@ -238,7 +239,7 @@ ${defData}
238239
if (where === 'default' && !long)
239240
continue
240241

241-
const keys = Object.keys(data).sort((a, b) => a.localeCompare(b, 'en'))
242+
const keys = Object.keys(data).sort(localeCompare)
242243
if (!keys.length)
243244
continue
244245

lib/help.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const openUrl = require('./utils/open-url.js')
44
const { promisify } = require('util')
55
const glob = promisify(require('glob'))
6+
const localeCompare = require('@isaacs/string-locale-compare')('en')
67

78
const BaseCommand = require('./base-command.js')
89

@@ -82,7 +83,7 @@ class Help extends BaseCommand {
8283
if (aManNumber !== bManNumber)
8384
return aManNumber - bManNumber
8485

85-
return a.localeCompare(b, 'en')
86+
return localeCompare(a, b)
8687
})
8788
const man = mans[0]
8889

lib/ls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const _problems = Symbol('problems')
2222
const _required = Symbol('required')
2323
const _type = Symbol('type')
2424
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
25+
const localeCompare = require('@isaacs/string-locale-compare')('en')
2526

2627
class LS extends ArboristWorkspaceCmd {
2728
/* istanbul ignore next - see test/lib/load-all-commands.js */
@@ -503,8 +504,7 @@ const augmentNodesWithMetadata = ({
503504
return node
504505
}
505506

506-
const sortAlphabetically = (a, b) =>
507-
a.pkgid.localeCompare(b.pkgid, 'en')
507+
const sortAlphabetically = ({ pkgid: a }, { pkgid: b }) => localeCompare(a, b)
508508

509509
const humanOutput = ({ color, result, seenItems, unicode }) => {
510510
// we need to traverse the entire tree in order to determine which items

lib/outdated.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const color = require('chalk')
66
const styles = require('ansistyles')
77
const npa = require('npm-package-arg')
88
const pickManifest = require('npm-pick-manifest')
9+
const localeCompare = require('@isaacs/string-locale-compare')('en')
910

1011
const Arborist = require('@npmcli/arborist')
1112

@@ -85,7 +86,7 @@ class Outdated extends ArboristWorkspaceCmd {
8586
}))
8687

8788
// sorts list alphabetically
88-
const outdated = this.list.sort((a, b) => a.name.localeCompare(b.name, 'en'))
89+
const outdated = this.list.sort((a, b) => localeCompare(a.name, b.name))
8990

9091
if (outdated.length > 0)
9192
process.exitCode = 1

lib/utils/completion/installed-deep.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { resolve } = require('path')
22
const Arborist = require('@npmcli/arborist')
3+
const localeCompare = require('@isaacs/string-locale-compare')('en')
34

45
const installedDeep = async (npm) => {
56
const {
@@ -15,8 +16,7 @@ const installedDeep = async (npm) => {
1516
return i
1617
})
1718
.filter(i => (i.depth - 1) <= depth)
18-
.sort((a, b) => a.depth - b.depth)
19-
.sort((a, b) => a.depth === b.depth ? a.name.localeCompare(b.name, 'en') : 0)
19+
.sort((a, b) => (a.depth - b.depth) || localeCompare(a.name, b.name))
2020

2121
const res = new Set()
2222
const gArb = new Arborist({ global: true, path: resolve(npm.globalDir, '..') })

lib/utils/config/describe-all.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const definitions = require('./definitions.js')
2+
const localeCompare = require('@isaacs/string-locale-compare')('en')
23
const describeAll = () => {
34
// sort not-deprecated ones to the top
45
/* istanbul ignore next - typically already sorted in the definitions file,
@@ -7,7 +8,7 @@ const describeAll = () => {
78
const sort = ([keya, {deprecated: depa}], [keyb, {deprecated: depb}]) => {
89
return depa && !depb ? 1
910
: !depa && depb ? -1
10-
: keya.localeCompare(keyb, 'en')
11+
: localeCompare(keya, keyb)
1112
}
1213
return Object.entries(definitions).sort(sort)
1314
.map(([key, def]) => def.describe())

lib/utils/npm-usage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { dirname } = require('path')
22
const { cmdList } = require('./cmd-list')
3+
const localeCompare = require('@isaacs/string-locale-compare')('en')
34

45
module.exports = (npm) => {
56
const usesBrowser = npm.config.get('viewer') === 'browser'
@@ -62,7 +63,7 @@ const usages = (npm) => {
6263
maxLen = Math.max(maxLen, c.length)
6364
return set
6465
}, [])
65-
.sort((a, b) => a[0].localeCompare(b[0], 'en'))
66+
.sort(([a], [b]) => localeCompare(a, b))
6667
.map(([c, usage]) => `\n ${c}${' '.repeat(maxLen - c.length + 1)}${
6768
(usage.split('\n').join('\n' + ' '.repeat(maxLen + 5)))}`)
6869
.join('\n')

lib/utils/tar.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ const ssri = require('ssri')
33
const npmlog = require('npmlog')
44
const formatBytes = require('./format-bytes.js')
55
const columnify = require('columnify')
6+
const localeCompare = require('@isaacs/string-locale-compare')('en', {
7+
sensitivity: 'case',
8+
numeric: true,
9+
})
610

711
const logTar = (tarball, opts = {}) => {
812
const { unicode = false, log = npmlog } = opts
@@ -75,12 +79,7 @@ const getContents = async (manifest, tarball) => {
7579
algorithms: ['sha1', 'sha512'],
7680
})
7781

78-
const comparator = (a, b) => {
79-
return a.path.localeCompare(b.path, 'en', {
80-
sensitivity: 'case',
81-
numeric: true,
82-
})
83-
}
82+
const comparator = ({ path: a }, { path: b }) => localeCompare(a, b)
8483

8584
const isUpper = (str) => {
8685
const ch = str.charAt(0)

node_modules/@isaacs/string-locale-compare/index.js

Lines changed: 28 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@isaacs/string-locale-compare/package.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"./package.json": "./package.json"
5454
},
5555
"dependencies": {
56+
"@isaacs/string-locale-compare": "^1.1.0",
5657
"@npmcli/arborist": "^2.9.0",
5758
"@npmcli/ci-detect": "^1.2.0",
5859
"@npmcli/config": "^2.3.0",

0 commit comments

Comments
 (0)