perfect-json
Advanced tools
+7
-0
@@ -0,3 +1,10 @@ | ||
| # Changelog | ||
| ## 0.2.0 (2021-07-26) | ||
| - `compact` option is added. | ||
| - Some examples are improved in `README.md`. | ||
| ## 0.1.0 (2021-03-25) | ||
| - Initial release. |
@@ -13,2 +13,4 @@ "use strict"; | ||
| indent = _options$indent === void 0 ? 2 : _options$indent, | ||
| _options$compact = options.compact, | ||
| compact = _options$compact === void 0 ? true : _options$compact, | ||
| singleLine = options.singleLine, | ||
@@ -48,6 +50,4 @@ maxLineLength = options.maxLineLength, | ||
| var values; | ||
| var baseIndent = depth * indent; | ||
| var baseIndentChars = new Array(baseIndent + 1).join(' '); | ||
| var globalIndent = (depth + 1) * indent; | ||
| var globalIndentChars = new Array(globalIndent + 1).join(' '); | ||
| var baseIndentChars = new Array(depth * indent + 1).join(' '); | ||
| var globalIndentChars = new Array((depth + 1) * indent + 1).join(' '); | ||
| var prefixIndentChars = key === undefined ? baseIndentChars : ''; | ||
@@ -106,3 +106,3 @@ | ||
| if (Array.isArray(item) && arrayValuesAreExpandedObjects(values)) { | ||
| if (Array.isArray(item) && arrayValuesAreExpandedObjects(values) && compact) { | ||
| var replaceRegExp = new RegExp("\\n {".concat(indent, "}"), 'g'); | ||
@@ -109,0 +109,0 @@ list = ''; |
+1
-1
| { | ||
| "name": "perfect-json", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "description": "Utility function to beautify JSON string...like JSON.stringify() but better", | ||
@@ -5,0 +5,0 @@ "main": "lib/perfect-json.js", |
+50
-18
@@ -31,13 +31,14 @@ # node-perfect-json | ||
| - `options` — optional parameters: | ||
| - `indent` — count of indentation spaces (defaults to `2`); | ||
| - `singleLine` — tells whether values of object properties must be placed on a single line, it can be of boolean type or a function returning a boolean result and being invoked for each property of an object recursively — the function receives an object argument with the following properties: | ||
| - `key` — name of the current property (zero-based index in case of array); | ||
| - `value` — value of the current property; | ||
| - `path` — array consisting of names of all ascendant properties including the current one; | ||
| - `items` — array of references to all ascendant objects and arrays; | ||
| - `depth` — zero-based depth level (equals to `path.length` and `items.length`); | ||
| - `indent` — count of indentation spaces per level (`(depth + 1) * indent` results in a summary indentation on a given level). | ||
| - `maxLineLength` — places objects and arrays on a single line if resulting line's length is less than or equal to specified value; | ||
| - `arrayMargin` — characters after opening and before closing array brackets when array is placed on a single line (defaults to empty string meaning no gap: `["Javascript", "Node.js", "ES6"]`); | ||
| - `objectMargin` — characters after opening and before closing object brackets when object is placed on a single line (defaults to `' '` meaning a gap: `{ "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }`). | ||
| - `indent` — count of indentation spaces (defaults to `2`); | ||
| - `compact` — tells whether close and open brackets of object array items must be placed on the same line (defaults to `true`); | ||
| - `singleLine` — tells whether values of object properties must be placed on a single line, it can be of boolean type or a function returning a boolean result and being invoked for each property of an object recursively — the function receives an object argument with the following properties: | ||
| - `key` — name of the current property (zero-based index in case of array); | ||
| - `value` — value of the current property; | ||
| - `path` — array consisting of names of all ascendant properties including the current one; | ||
| - `items` — array of references to all ascendant objects and arrays; | ||
| - `depth` — zero-based depth level (equals to `path.length` and `items.length`); | ||
| - `indent` — count of indentation spaces per level (`(depth + 1) * indent` results in a summary indentation on a given level). | ||
| - `maxLineLength` — places objects and arrays on a single line if resulting line's length is less than or equal to specified value; | ||
| - `arrayMargin` — characters after opening and before closing array brackets when array is placed on a single line (defaults to empty string meaning no gap: `["Javascript", "Node.js", "ES6"]`); | ||
| - `objectMargin` — characters after opening and before closing object brackets when object is placed on a single line (defaults to `' '` meaning a gap: `{ "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }`). | ||
@@ -83,2 +84,32 @@ ### Basic example | ||
| ### Incompact object array items | ||
| Use `compact` option: | ||
| ```javascript | ||
| const perfectJson = require('perfect-json'); | ||
| console.log(perfectJson([{ | ||
| name: 'Dmitriy', | ||
| surname: 'Pushkov' | ||
| }, { | ||
| name: 'Tamara', | ||
| surname: 'Pushkova' | ||
| }], { compact: false })); | ||
| ``` | ||
| Result: | ||
| ```json | ||
| [ | ||
| { | ||
| "name": "Dmitriy", | ||
| "surname": "Pushkov" | ||
| }, | ||
| { | ||
| "name": "Tamara", | ||
| "surname": "Pushkova" | ||
| } | ||
| ] | ||
| ``` | ||
| ### Set indentation size | ||
@@ -93,3 +124,7 @@ | ||
| surname: 'Pushkov', | ||
| skills: ["JavaScript", "Node.js", "ES6"], | ||
| skills: [ | ||
| "JavaScript", | ||
| "Node.js", | ||
| "ES6" | ||
| ], | ||
| env: { | ||
@@ -186,3 +221,3 @@ node: "14.0.0", | ||
| console.log(perfectJson(obj, { | ||
| maxLineLength: 30 | ||
| maxLineLength: 40 | ||
| })); | ||
@@ -200,7 +235,3 @@ console.log(perfectJson(obj, { | ||
| "surname": "Pushkov", | ||
| "skills": [ | ||
| "JavaScript", | ||
| "Node.js", | ||
| "ES6" | ||
| ], | ||
| "skills": ["JavaScript", "Node.js", "ES6"], | ||
| "env": { | ||
@@ -213,2 +244,3 @@ "node": "14.0.0", | ||
| } | ||
| ``` | ||
@@ -215,0 +247,0 @@ |
11897
6.11%250
14.68%