From ca0159f62e42acf6fa516f28fbad7eaf1515ab8f Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Sat, 6 Apr 2019 00:57:56 +0100 Subject: [PATCH 1/5] Make all indentation 4 spaces, like original json2 --- index.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index e7988ef..8345e84 100644 --- a/index.js +++ b/index.js @@ -188,28 +188,28 @@ function beautify (value, replacer, space, limit) { // A default replacer method can be provided. Use of the space parameter can // produce text that is more easily readable. - var i; - gap = ''; - indent = ''; + var i; + gap = ''; + indent = ''; - if (!limit) limit = 0; + if (!limit) limit = 0; - if (typeof limit !== "number") - throw new Error("beaufifier: limit must be a number"); + if (typeof limit !== "number") + throw new Error("beaufifier: limit must be a number"); // If the space parameter is a number, make an indent string containing that // many spaces. - if (typeof space === 'number') { - for (i = 0; i < space; i += 1) { - indent += ' '; - } + if (typeof space === 'number') { + for (i = 0; i < space; i += 1) { + indent += ' '; + } // If the space parameter is a string, it will be used as the indent string. - } else if (typeof space === 'string') { - indent = space; - } + } else if (typeof space === 'string') { + indent = space; + } // If there is a replacer, it must be a function or an array. // Otherwise, throw an error. @@ -224,7 +224,7 @@ function beautify (value, replacer, space, limit) { // Make a fake root object containing our value under the key of ''. // Return the result of stringifying the value. - return str('', {'': value}, limit); + return str('', {'': value}, limit); } module.exports = beautify; From 052b95a2833ce226410ff67a0661e495aa9d40b0 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Sat, 6 Apr 2019 01:10:56 +0100 Subject: [PATCH 2/5] Rename limit var to maxwidth --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8345e84..3ecdc2b 100644 --- a/index.js +++ b/index.js @@ -180,7 +180,7 @@ function str(key, holder, limit) { } -function beautify (value, replacer, space, limit) { +function beautify (value, replacer, space, maxwidth) { // The stringify method takes a value and an optional replacer, and an optional // space parameter, and returns a JSON text. The replacer can be a function @@ -192,9 +192,9 @@ function beautify (value, replacer, space, limit) { gap = ''; indent = ''; - if (!limit) limit = 0; + if (!maxwidth) maxwidth = 0; - if (typeof limit !== "number") + if (typeof maxwidth !== "number") throw new Error("beaufifier: limit must be a number"); // If the space parameter is a number, make an indent string containing that @@ -224,7 +224,7 @@ function beautify (value, replacer, space, limit) { // Make a fake root object containing our value under the key of ''. // Return the result of stringifying the value. - return str('', {'': value}, limit); + return str('', {'': value}, maxwidth); } module.exports = beautify; From 05d97ecb31c1321e8e9fbe32e6fc35f0be1e247f Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Sat, 6 Apr 2019 00:17:47 +0000 Subject: [PATCH 3/5] Allow passing a config object instead of parameters It has keys for replacer, space and maxwidth, and can easily be extended with more parameters. --- README.md | 16 ++++++++++++++++ index.js | 39 ++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 581891b..45c536a 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ It has the exact same signature of `JSON.stringify` but it also adds an optional The maximum fixed character width (for instance 80). +Alternatively, the second parameter can be an object with the `replacer` and `space` keys corresponding to the +standard `JSON.stringify` parameters and a `maxwidth` key for the maximum fixed character width. When the +second parameter is an object, space defaults to 2 space and maxwidth defaults to 80 characters. + ## Examples: ```js @@ -20,6 +24,12 @@ var obj = { str: "Hello World", num: 42, smallarray: [ 1, 2, 3, "foo", {} ], sma ### With 100 fixed-spaces: +```js +console.log(beautify(obj, { maxwidth: 100})); +``` + +or: + ```js console.log(beautify(obj, null, 2, 100)); ``` @@ -37,6 +47,12 @@ console.log(beautify(obj, null, 2, 100)); ### With 80 fixed-spaces: +```js +console.log(beautify(obj, {})); +``` + +or: + ```js console.log(beautify(obj, null, 2, 80)); ``` diff --git a/index.js b/index.js index 3ecdc2b..b690cf6 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,7 @@ var gap, '"': '\\"', '\\': '\\\\' }, + options, rep; function quote(string) { @@ -188,14 +189,34 @@ function beautify (value, replacer, space, maxwidth) { // A default replacer method can be provided. Use of the space parameter can // produce text that is more easily readable. +// Alternatively, the method can take a value and a config object with the +// replacer, space and maxwidth parameters as keys. + var i; gap = ''; indent = ''; + options = {}; + +// If second and no further parameters are supplied, and it isn't a replacer, +// it is an options object + if (replacer && !isReplacer(replacer) && !space && !maxwidth) { + options = replacer; + replacer = options.replacer; + space = options.hasOwnProperty('space') ? options.space : ' '; + maxwidth = options.maxwidth || 80; + } + +// If there is a replacer, it must be a function or an array. +// Otherwise, throw an error. + if (!isReplacer(replacer)) { + throw new Error('beautifier: wrong replacer parameter'); + } + rep = replacer; if (!maxwidth) maxwidth = 0; if (typeof maxwidth !== "number") - throw new Error("beaufifier: limit must be a number"); + throw new Error("beaufifier: maxwidth must be a number"); // If the space parameter is a number, make an indent string containing that // many spaces. @@ -211,20 +232,16 @@ function beautify (value, replacer, space, maxwidth) { indent = space; } -// If there is a replacer, it must be a function or an array. -// Otherwise, throw an error. - - rep = replacer; - if (replacer && typeof replacer !== 'function' && - (typeof replacer !== 'object' || - typeof replacer.length !== 'number')) { - throw new Error('beautifier: wrong replacer parameter'); - } - // Make a fake root object containing our value under the key of ''. // Return the result of stringifying the value. return str('', {'': value}, maxwidth); } +function isReplacer(replacer) { + return !(replacer && typeof replacer !== 'function' && + (typeof replacer !== 'object' || + typeof replacer.length !== 'number')) +} + module.exports = beautify; From 1786665fd9b80460e3634fbfa4d3f2cb53b4256a Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Sat, 6 Apr 2019 10:04:57 +0000 Subject: [PATCH 4/5] Add option to sort keys alphabetically --- README.md | 34 +++++++++++++++++++++++++++++++++- index.js | 6 ++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 45c536a..7926d80 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,11 @@ The maximum fixed character width (for instance 80). Alternatively, the second parameter can be an object with the `replacer` and `space` keys corresponding to the standard `JSON.stringify` parameters and a `maxwidth` key for the maximum fixed character width. When the -second parameter is an object, space defaults to 2 space and maxwidth defaults to 80 characters. +second parameter is an object, space defaults to 2 space and maxwidth defaults to 80 characters. There are +also some additional keys for even finer control: + +* `sortkeys` (boolean) If true, json objects will be sorted by key, rather than printed in the order they are + stored. Defaults to false. ## Examples: @@ -78,3 +82,31 @@ console.log(beautify(obj, null, 2, 80)); } } ``` + +### Sorting keys + +```js +console.log(beautify(obj, { sortkeys: true })); +``` + +```json +{ + "bigarray": [ + 1, + 2, + 3, + "foo", + { "arr": [ 1, 2, 3, "foo", {} ], "bar": 42, "foo": "bar" } + ], + "bigobject": { + "a": { "b": { "c": 42 } }, + "bar": 42, + "foo": [ 1, 2, 3, "foo", {} ], + "foobar": "FooBar" + }, + "num": 42, + "smallarray": [ 1, 2, 3, "foo", {} ], + "smallobject": { "bar": 42, "foo": "bar" }, + "str": "Hello World" +} +``` diff --git a/index.js b/index.js index b690cf6..ec00105 100644 --- a/index.js +++ b/index.js @@ -163,6 +163,12 @@ function str(key, holder, limit) { } } +// Sort keys if applicable + + if (options.sortkeys) { + partial.sort(); + } + // Join all of the member texts together, separated with commas, // and wrap them in braces. From 66341b5fbac2b0ebacdd813929aaead1692b8e47 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Mon, 8 Apr 2019 14:02:51 +0100 Subject: [PATCH 5/5] Add spacearrays and spaceobjects options These control spacing around {} and [] --- README.md | 5 +++++ index.js | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7926d80..3cf0ca9 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ also some additional keys for even finer control: * `sortkeys` (boolean) If true, json objects will be sorted by key, rather than printed in the order they are stored. Defaults to false. +* `spacearrays` (string) Space to leave around single line arrays, defaults to a single space (`' '`), which + produces output like `[ 1, 2, 3 ]`. Set to an empty string and it will output `[1, 2, 3]`. +* `spaceobjects` (string) As above but for objects, not arrays, defaults to a single space (`' '`), which + produces output like `{ "foo": "bar", "bar": 42 }`. Set to an empty string and it will output + `{"foo": "bar", "bar": 42}`. ## Examples: diff --git a/index.js b/index.js index ec00105..f1e3508 100644 --- a/index.js +++ b/index.js @@ -121,7 +121,7 @@ function str(key, holder, limit) { ? ( gap.length + partial.join(', ').length + 4 > limit ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : - '[ ' + partial.join(', ') + ' ]' + '[' + options.spacearrays + partial.join(', ') + options.spacearrays + ']' ) : '[' + partial.join(',') + ']'; gap = mind; @@ -178,7 +178,7 @@ function str(key, holder, limit) { ? ( gap.length + partial.join(', ').length + 4 > limit ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' : - '{ ' + partial.join(', ') + ' }' + '{' + options.spaceobjects + partial.join(', ') + options.spaceobjects + '}' ) : '{' + partial.join(',') + '}'; gap = mind; @@ -221,6 +221,10 @@ function beautify (value, replacer, space, maxwidth) { if (!maxwidth) maxwidth = 0; + if (!options.hasOwnProperty('spacearrays')) options.spacearrays = ' '; + + if (!options.hasOwnProperty('spaceobjects')) options.spaceobjects = ' '; + if (typeof maxwidth !== "number") throw new Error("beaufifier: maxwidth must be a number");