Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ Ignore case when sorting.
`--reverse, -r`\
Reverse the ordering z -> a

`--secondary-sort-by-value, -sv`\
Sort arrays of objects with identical keys by their value as well.

```
[
{
"b":"c",
"a": "b",
"g": "h"
},
{
"b":"c",
"a": "a",
"g": "h"
},
] -> [
{
"a": "a",
"b":"c",
"g": "h"
},
{
"a": "b",
"b":"c",
"g": "h"
},
]
```

`--depth=DEPTH, -d`\
The sorting _DEPTH_ on multidimensional objects.
Use a number greater then 0 for the _DEPTH_ value.
Expand Down
1 change: 1 addition & 0 deletions app/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const sortJson = require('./');
const alias = {
depth: ['d'],
reverse: ['r'],
secondaryValueSort: ['secondary-sort-by-value', 'sv'],
ignoreCase: ['ignore-case', 'i'],
indentSize: ['indent-size', 'spaces'],
noFinalNewLine: ['no-final-newline', 'nn'],
Expand Down
1 change: 1 addition & 0 deletions app/overwrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function overwriteFile(path, options) {
fileContent = fs.readFileSync(path, 'utf8');
newData = visit(JSON.parse(fileContent), options);
} catch (e) {
/* eslint-disable-next-line no-console */
console.error('Failed to retrieve json object from file');
throw e;
}
Expand Down
32 changes: 27 additions & 5 deletions app/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
function visit(old, options) {
const sortOptions = options || {};

const ignoreCase = sortOptions.ignoreCase || false;
const reverse = sortOptions.reverse || false;
const depth = sortOptions.depth || Infinity;
const level = sortOptions.level || 1;
Expand All @@ -27,9 +26,26 @@ function visit(old, options) {
const copy = Array.isArray(old) ? [] : {};
let keys = Object.keys(old);
if (processing) {
keys = ignoreCase ?
keys.sort((left, right) => left.toLowerCase().localeCompare(right.toLowerCase())) :
keys.sort();
const sorter = (left, right, { ignoreCase, secondaryValueSort }) => {
if (secondaryValueSort && old[left] instanceof Object && old[right] instanceof Object) {
const sortedLeft = visit(old[left], options);
const sortedRight = visit(old[right], options);

if (ignoreCase) {
return JSON.stringify(sortedLeft)
.toLowerCase()
.localeCompare(JSON.stringify(sortedRight).toLowerCase());
}
return JSON.stringify(sortedLeft).localeCompare(JSON.stringify(sortedRight));
}

if (ignoreCase) {
return left.toLowerCase().localeCompare(right.toLowerCase());
}

return left.localeCompare(right);
};
keys.sort((left, right) => sorter(left, right, sortOptions));
}

if (reverse) {
Expand All @@ -39,7 +55,13 @@ function visit(old, options) {
keys.forEach((key) => {
const subSortOptions = Object.assign({}, sortOptions);
subSortOptions.level = level + 1;
copy[key] = visit(old[key], subSortOptions);
const sortedValue = visit(old[key], subSortOptions);

if (Array.isArray(copy)) {
copy.push(sortedValue);
} else {
copy[key] = sortedValue;
}
});

return copy;
Expand Down
275 changes: 275 additions & 0 deletions tests/visit.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,279 @@ describe('visit', () => {

expect(JSON.stringify(visit(givenData))).to.equal(JSON.stringify(expectedData));
});

it('sorts nested array of similar objects by value case-invariant', () => {
const givenData = {
"abc": "def",
"aa": [
{
"y": "c",
"g": 3,
"a": "b",
},
{
"g": 3,
"y": "c",
"a": "b",
"b": "b",
},
{
"a": "b",
"y": "c",
"g": 3,
},
],
};
const expectedData = {
"aa": [
{
"a": "b",
"b": "b",
"g": 3,
"y": "c",
},
{
"a": "b",
"g": 3,
"y": "c",
},
{
"a": "b",
"g": 3,
"y": "c",
},
],
"abc": "def",
};

const options = {
secondaryValueSort: true,
ignoreCase: true,
};

expect(JSON.stringify(visit(givenData, options))).to.equal(JSON.stringify(expectedData));
});

it('sorts nested array of objects by value case-invariant', () => {
const givenData = [
{
"y": "c",
"g": 3,
"a": "b",
},
{
"a": "aa",
"g": 4,
"y": 100,
},
{
"g": 1,
"y": "b",
"a": "h",
},
{
"a": "a",
"g": 4,
"y": 100,
},
{
"a": "aa",
"g": 3,
"y": 100,
},
{
"y": 101,
"a": "A",
"g": 3,
},
];
const expectedData = [
{
"a": "A",
"g": 3,
"y": 101
},
{
"a": "a",
"g": 4,
"y": 100
},
{
"a": "aa",
"g": 3,
"y": 100
},
{
"a": "aa",
"g": 4,
"y": 100
},
{
"a": "b",
"g": 3,
"y": "c"
},
{
"a": "h",
"g": 1,
"y": "b"
},
];

const options = {
secondaryValueSort: true,
ignoreCase: true,
};

expect(JSON.stringify(visit(givenData, options))).to.equal(JSON.stringify(expectedData));
});

it('sorts nested array of objects by value case-variant', () => {
const givenData = [
{
"y": "c",
"g": 3,
"a": "b",
},
{
"a": "aa",
"g": 4,
"y": 100,
},
{
"a": "aa",
"g": 3,
"y": 100,
},
{
"g": 1,
"y": "b",
"a": "h",
},
{
"a": "a",
"g": 4,
"y": 100,
},
{
"y": 101,
"a": "A",
"g": 3,
},
];
const expectedData = [
{
"a": "A",
"g": 3,
"y": 101
},
{
"a": "a",
"g": 4,
"y": 100
},
{
"a": "aa",
"g": 3,
"y": 100
},
{
"a": "aa",
"g": 4,
"y": 100
},
{
"a": "b",
"g": 3,
"y": "c"
},
{
"a": "h",
"g": 1,
"y": "b"
}
];

const options = {
secondaryValueSort: true,
ignoreCase: false,
};

expect(JSON.stringify(visit(givenData, options))).to.equal(JSON.stringify(expectedData));
});

it('sorts nested array of objects not value', () => {
const givenData = [
{
"y": "c",
"g": 3,
"a": "b",
},
{
"a": "aa",
"g": 4,
"y": 100,
},
{
"a": "aa",
"g": 3,
"y": 100,
},
{
"g": 1,
"y": "b",
"a": "h",
},
{
"a": "a",
"g": 4,
"y": 100,
},
{
"y": 101,
"a": "A",
"g": 3,
},
];
const expectedData = [
{
"a": "b",
"g": 3,
"y": "c",
},
{
"a": "aa",
"g": 4,
"y": 100,
},
{
"a": "aa",
"g": 3,
"y": 100,
},
{
"a": "h",
"g": 1,
"y": "b",
},
{
"a": "a",
"g": 4,
"y": 100,
},
{
"a": "A",
"g": 3,
"y": 101,
}
];

const options = {
secondaryValueSort: false,
ignoreCase: false,
};

expect(JSON.stringify(visit(givenData, options))).to.equal(JSON.stringify(expectedData));
});

});
Loading