diff --git a/README.md b/README.md index 3f91b3a..e773a60 100644 --- a/README.md +++ b/README.md @@ -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 ----- diff --git a/cmd.js b/cmd.js index 7a02bd7..06650a2 100755 --- a/cmd.js +++ b/cmd.js @@ -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); @@ -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) { diff --git a/index.js b/index.js index 0bd007d..e5321a9 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..5d615e4 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,37 @@ +{ + "name": "sort-json", + "version": "1.4.1", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "detect-indent": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-4.0.0.tgz", + "integrity": "sha1-920GQ1LN9Docts5hnE7jqUdd4gg=", + "requires": { + "repeating": "2.0.1" + } + }, + "is-finite": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-finite/-/is-finite-1.0.2.tgz", + "integrity": "sha1-zGZ3aVYCvlUO8R6LSqYwU0K20Ko=", + "requires": { + "number-is-nan": "1.0.1" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" + }, + "repeating": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/repeating/-/repeating-2.0.1.tgz", + "integrity": "sha1-UhTFOpJtNVJwdSf7q0FdvAjQbdo=", + "requires": { + "is-finite": "1.0.2" + } + } + } +} diff --git a/package.json b/package.json index c55030f..054dd28 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "type": "git", "url": "https://github.com/kesla/sort-json.git" }, - "author": "David Björklund ", + "authors": "David Björklund , Nathan Schwarz ", "license": "MIT", "bugs": { "url": "https://github.com/kesla/sort-json/issues"