Skip to content

Commit

Permalink
Make sort locale independent
Browse files Browse the repository at this point in the history
Re: npm/cli#2829

PR-URL: #5
Credit: @padinko
Close: #5
Reviewed-by: @isaacs

EDIT(@isaacs): added tests
  • Loading branch information
padinko authored and isaacs committed May 6, 2021
1 parent fbf52b1 commit 08736fa
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const compare = (ak, bk, prefKeys) =>
: prefKeys.includes(bk) && !prefKeys.includes(ak) ? 1
: prefKeys.includes(ak) && prefKeys.includes(bk)
? prefKeys.indexOf(ak) - prefKeys.indexOf(bk)
: ak.localeCompare(bk)
: ak.localeCompare(bk, 'en')

const sort = (replacer, seen) => (key, val) => {
const prefKeys = Array.isArray(replacer) ? replacer : []
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"postpublish": "git push origin --follow-tags"
},
"tap": {
"test-env": [
"LC_ALL=sk"
],
"check-coverage": true
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions tap-snapshots/test/basic.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
'use strict'
exports[`test/basic.js TAP basic sorting operation with default 2-space indent > mix of objects and out of order keys 1`] = `
{
"c": 1,
"ch": 2,
"d": 3,
"y": "z",
"yy": "a",
"z": 1,
Expand Down
3 changes: 3 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const stringify = require('../')
t.test('basic sorting operation with default 2-space indent', t => {
t.plan(1)
t.matchSnapshot(stringify({
c: 1,
ch: 2,
d: 3,
z: 1,
y: 'z',
obj: { a: {}, b: 'x' },
Expand Down

0 comments on commit 08736fa

Please sign in to comment.