json-stringify-pretty-compact
Advanced tools
Comparing version 3.0.0 to 4.0.0
56
index.js
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
// Note: This regex matches even invalid JSON strings, but since we’re | ||
@@ -7,9 +5,6 @@ // working on the output of `JSON.stringify` we know that only valid strings | ||
// that case we don’t care since the output would be invalid anyway). | ||
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g; | ||
const stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g; | ||
module.exports = function stringify(passedObj, options) { | ||
var indent, maxLength, replacer; | ||
options = options || {}; | ||
indent = JSON.stringify( | ||
export default function stringify(passedObj, options = {}) { | ||
const indent = JSON.stringify( | ||
[1], | ||
@@ -19,3 +14,4 @@ undefined, | ||
).slice(2, -3); | ||
maxLength = | ||
const maxLength = | ||
indent === "" | ||
@@ -26,8 +22,6 @@ ? Infinity | ||
: options.maxLength; | ||
replacer = options.replacer; | ||
let { replacer } = options; | ||
return (function _stringify(obj, currentIndent, reserved) { | ||
// prettier-ignore | ||
var end, index, items, key, keyPart, keys, length, nextIndent, prettified, start, string, value; | ||
if (obj && typeof obj.toJSON === "function") { | ||
@@ -37,3 +31,3 @@ obj = obj.toJSON(); | ||
string = JSON.stringify(obj, replacer); | ||
const string = JSON.stringify(obj, replacer); | ||
@@ -44,9 +38,9 @@ if (string === undefined) { | ||
length = maxLength - currentIndent.length - reserved; | ||
const length = maxLength - currentIndent.length - reserved; | ||
if (string.length <= length) { | ||
prettified = string.replace( | ||
const prettified = string.replace( | ||
stringOrChar, | ||
function (match, stringLiteral) { | ||
return stringLiteral || match + " "; | ||
(match, stringLiteral) => { | ||
return stringLiteral || `${match} `; | ||
} | ||
@@ -65,5 +59,7 @@ ); | ||
if (typeof obj === "object" && obj !== null) { | ||
nextIndent = currentIndent + indent; | ||
items = []; | ||
index = 0; | ||
const nextIndent = currentIndent + indent; | ||
const items = []; | ||
let index = 0; | ||
let start; | ||
let end; | ||
@@ -73,3 +69,3 @@ if (Array.isArray(obj)) { | ||
end = "]"; | ||
length = obj.length; | ||
const { length } = obj; | ||
for (; index < length; index++) { | ||
@@ -84,8 +80,8 @@ items.push( | ||
end = "}"; | ||
keys = Object.keys(obj); | ||
length = keys.length; | ||
const keys = Object.keys(obj); | ||
const { length } = keys; | ||
for (; index < length; index++) { | ||
key = keys[index]; | ||
keyPart = JSON.stringify(key) + ": "; | ||
value = _stringify( | ||
const key = keys[index]; | ||
const keyPart = `${JSON.stringify(key)}: `; | ||
const value = _stringify( | ||
obj[key], | ||
@@ -102,4 +98,4 @@ nextIndent, | ||
if (items.length > 0) { | ||
return [start, indent + items.join(",\n" + nextIndent), end].join( | ||
"\n" + currentIndent | ||
return [start, indent + items.join(`,\n${nextIndent}`), end].join( | ||
`\n${currentIndent}` | ||
); | ||
@@ -111,2 +107,2 @@ } | ||
})(passedObj, "", 0); | ||
}; | ||
} |
{ | ||
"name": "json-stringify-pretty-compact", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"author": "Simon Lydell", | ||
"license": "MIT", | ||
"description": "The best of both `JSON.stringify(obj)` and `JSON.stringify(obj, null, indent)`.", | ||
"type": "commonjs", | ||
"type": "module", | ||
"exports": "./index.js", | ||
@@ -27,11 +27,9 @@ "types": "index.d.ts", | ||
"scripts": { | ||
"test": "eslint . --report-unused-disable-directives && prettier --check . && jest --coverage", | ||
"test": "eslint . --report-unused-disable-directives && prettier --check . && node --test", | ||
"prepublishOnly": "npm test" | ||
}, | ||
"devDependencies": { | ||
"eslint": "7.20.0", | ||
"eslint-plugin-jest": "24.1.5", | ||
"jest": "26.6.3", | ||
"prettier": "2.2.1" | ||
"eslint": "8.15.0", | ||
"prettier": "2.6.2" | ||
} | ||
} |
@@ -28,5 +28,9 @@ # json-stringify-pretty-compact | ||
```js | ||
const stringify = require("json-stringify-pretty-compact"); | ||
import stringify from "json-stringify-pretty-compact"; | ||
``` | ||
> **Note:** This is an [ESM only package]. (I haven’t written that gist, but it’s a great resource.) | ||
> | ||
> If you need CommonJS, install version 3.0.0. You won’t be missing out on anything: This package is _done._ No more features will be added, and no bugs have been found in years. | ||
## `stringify(obj, options = {})` | ||
@@ -54,1 +58,2 @@ | ||
[json.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | ||
[esm only package]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |
Sorry, the diff of this file is not supported yet
2
58
Yes
7412
5
97