fix: use Intl.Collator for string sorting when available · npm/cli@dbb90f7 (original) (raw)

File tree

13 files changed

lines changed

13 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ const semver = require('semver')
8 8 const BaseCommand = require('./base-command.js')
9 9 const npa = require('npm-package-arg')
10 10 const jsonParse = require('json-parse-even-better-errors')
11 +const localeCompare = require('@isaacs/string-locale-compare')('en')
11 12
12 13 const searchCachePackage = async (path, spec, cacheKeys) => {
13 14 const parsed = npa(spec)
@@ -212,10 +213,10 @@ class Cache extends BaseCommand {
212 213 for (const key of keySet)
213 214 results.add(key)
214 215 }
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))
216 217 return
217 218 }
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))
219 220 }
220 221 }
221 222
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ const writeFile = promisify(fs.writeFile)
10 10 const { spawn } = require('child_process')
11 11 const { EOL } = require('os')
12 12 const ini = require('ini')
13 +const localeCompare = require('@isaacs/string-locale-compare')('en')
13 14
14 15 // take an array of `[key, value, k2=v2, k3, v3, ...]` and turn into
15 16 // { key: value, k2: v2, k3: v3 }
@@ -209,7 +210,7 @@ class Config extends BaseCommand {
209 210 ; Configs like \`///:_authToken\` are auth that is restricted
210 211 ; to the registry host specified.
211 212
212 -${data.split('\n').sort((a, b) => a.localeCompare(b, 'en')).join('\n').trim()}
213 +${data.split('\n').sort(localeCompare).join('\n').trim()}
213 214
214 215 ;;;;
215 216 ; all available options shown below with default values
@@ -238,7 +239,7 @@ ${defData}
238 239 if (where === 'default' && !long)
239 240 continue
240 241
241 -const keys = Object.keys(data).sort((a, b) => a.localeCompare(b, 'en'))
242 +const keys = Object.keys(data).sort(localeCompare)
242 243 if (!keys.length)
243 244 continue
244 245
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ const path = require('path')
3 3 const openUrl = require('./utils/open-url.js')
4 4 const { promisify } = require('util')
5 5 const glob = promisify(require('glob'))
6 +const localeCompare = require('@isaacs/string-locale-compare')('en')
6 7
7 8 const BaseCommand = require('./base-command.js')
8 9
@@ -82,7 +83,7 @@ class Help extends BaseCommand {
82 83 if (aManNumber !== bManNumber)
83 84 return aManNumber - bManNumber
84 85
85 -return a.localeCompare(b, 'en')
86 +return localeCompare(a, b)
86 87 })
87 88 const man = mans[0]
88 89
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ const _problems = Symbol('problems')
22 22 const _required = Symbol('required')
23 23 const _type = Symbol('type')
24 24 const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
25 +const localeCompare = require('@isaacs/string-locale-compare')('en')
25 26
26 27 class LS extends ArboristWorkspaceCmd {
27 28 /* istanbul ignore next - see test/lib/load-all-commands.js */
@@ -503,8 +504,7 @@ const augmentNodesWithMetadata = ({
503 504 return node
504 505 }
505 506
506 -const sortAlphabetically = (a, b) =>
507 -a.pkgid.localeCompare(b.pkgid, 'en')
507 +const sortAlphabetically = ({ pkgid: a }, { pkgid: b }) => localeCompare(a, b)
508 508
509 509 const humanOutput = ({ color, result, seenItems, unicode }) => {
510 510 // we need to traverse the entire tree in order to determine which items
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ const color = require('chalk')
6 6 const styles = require('ansistyles')
7 7 const npa = require('npm-package-arg')
8 8 const pickManifest = require('npm-pick-manifest')
9 +const localeCompare = require('@isaacs/string-locale-compare')('en')
9 10
10 11 const Arborist = require('@npmcli/arborist')
11 12
@@ -85,7 +86,7 @@ class Outdated extends ArboristWorkspaceCmd {
85 86 }))
86 87
87 88 // 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))
89 90
90 91 if (outdated.length > 0)
91 92 process.exitCode = 1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1 1 const { resolve } = require('path')
2 2 const Arborist = require('@npmcli/arborist')
3 +const localeCompare = require('@isaacs/string-locale-compare')('en')
3 4
4 5 const installedDeep = async (npm) => {
5 6 const {
@@ -15,8 +16,7 @@ const installedDeep = async (npm) => {
15 16 return i
16 17 })
17 18 .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) |
20 20
21 21 const res = new Set()
22 22 const gArb = new Arborist({ global: true, path: resolve(npm.globalDir, '..') })
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
1 1 const definitions = require('./definitions.js')
2 +const localeCompare = require('@isaacs/string-locale-compare')('en')
2 3 const describeAll = () => {
3 4 // sort not-deprecated ones to the top
4 5 /* istanbul ignore next - typically already sorted in the definitions file,
@@ -7,7 +8,7 @@ const describeAll = () => {
7 8 const sort = ([keya, {deprecated: depa}], [keyb, {deprecated: depb}]) => {
8 9 return depa && !depb ? 1
9 10 : !depa && depb ? -1
10 - : keya.localeCompare(keyb, 'en')
11 + : localeCompare(keya, keyb)
11 12 }
12 13 return Object.entries(definitions).sort(sort)
13 14 .map(([key, def]) => def.describe())
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
1 1 const { dirname } = require('path')
2 2 const { cmdList } = require('./cmd-list')
3 +const localeCompare = require('@isaacs/string-locale-compare')('en')
3 4
4 5 module.exports = (npm) => {
5 6 const usesBrowser = npm.config.get('viewer') === 'browser'
@@ -62,7 +63,7 @@ const usages = (npm) => {
62 63 maxLen = Math.max(maxLen, c.length)
63 64 return set
64 65 }, [])
65 -.sort((a, b) => a[0].localeCompare(b[0], 'en'))
66 +.sort(([a], [b]) => localeCompare(a, b))
66 67 .map(([c, usage]) => `\n c{c}c{' '.repeat(maxLen - c.length + 1)}${
67 68 (usage.split('\n').join('\n' + ' '.repeat(maxLen + 5)))}`)
68 69 .join('\n')
Original file line number Diff line number Diff line change
@@ -3,6 +3,10 @@ const ssri = require('ssri')
3 3 const npmlog = require('npmlog')
4 4 const formatBytes = require('./format-bytes.js')
5 5 const columnify = require('columnify')
6 +const localeCompare = require('@isaacs/string-locale-compare')('en', {
7 +sensitivity: 'case',
8 +numeric: true,
9 +})
6 10
7 11 const logTar = (tarball, opts = {}) => {
8 12 const { unicode = false, log = npmlog } = opts
@@ -75,12 +79,7 @@ const getContents = async (manifest, tarball) => {
75 79 algorithms: ['sha1', 'sha512'],
76 80 })
77 81
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)
84 83
85 84 const isUpper = (str) => {
86 85 const ch = str.charAt(0)