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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ usage
```js
var sortJson = require('sort-json');

var copy = sortJson(object);
var copy = sortJson(object, key);
and if there is no key
var copy = sortJson(object, null);
```

CLI usage
---------
`sort-json file.json`

For now sort-json takes no other arguments, so the original file will be overwritten by a sorted JSON file, keeping the indentation of the original file.
or if you want to sort by key ("id" for example):
`sort-json file.json -k id` or `sort-json file.json --key id`
The original file will be overwritten by a sorted JSON file, keeping the indentation of the original file.

tests
-----
Expand Down
19 changes: 17 additions & 2 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@ var detectIndent = require('detect-indent');
var sortJson = require('./');

// Get all the files
var files = process.argv.slice(2);
var files = process.argv.splice(2, 3);
var key = null;

for (var x = 0; x < files.length; x++){
if (files[x] === "-k" || files[x] === '--key'){
if (files.length !== x + 2){
console.log("error: no key was given, ignoring the flag");
files.pop();
break;
} else {
key = files.pop();
files.pop();
break;
}
}
}

files.forEach(readEachFile);

Expand Down Expand Up @@ -37,7 +52,7 @@ function readEachFile(fileName) {
}

// Sorting
var sortedObject = sortJson(json);
var sortedObject = sortJson(json, key);

// Saving to file
fs.writeFile(filePath, JSON.stringify(sortedObject, null, indent) + ((eol && eol.length === 2) ? eol[1] : ''), function(err) {
Expand Down
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
module.exports = visit;

function visit(old) {
function visit(old, mainKey) {
if (typeof(old) !== 'object' || old === null) {
return old;
}
var copy = Array.isArray(old) ? [] : {};
var keys = Object.keys(old).sort();
keys.forEach(function(key) {
copy[key] = visit(old[key]);
});
if (mainKey === null){
var keys = Object.keys(old).sort();
keys.forEach(function(key) {
copy[key] = visit(old[key], null);
});
} else {
for (var x = 0; x < old.length; x++){
old.sort(function(a, b){
return (a[mainKey] - b[mainKey])
});
}
return (old);
}
return copy;
}
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"type": "git",
"url": "https://github.com/kesla/sort-json.git"
},
"author": "David Björklund <david.bjorklund@gmail.com>",
"authors": "David Björklund <david.bjorklund@gmail.com>, Nathan Schwarz <nathan.schwarz97@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/kesla/sort-json/issues"
Expand Down