json-stable-stringify
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -8,2 +8,13 @@ # Changelog | ||
## [v1.1.1](https://github.com/ljharb/json-stable-stringify/compare/v1.1.0...v1.1.1) - 2024-01-16 | ||
### Fixed | ||
- [Performance] use an array join instead of a string. [`#9`](https://github.com/ljharb/json-stable-stringify/issues/9) | ||
### Commits | ||
- [readme] replaced var with const [`e22d419`](https://github.com/ljharb/json-stable-stringify/commit/e22d419b54d8fd1dc36aa18b685da0032c74ec0f) | ||
- [Dev Deps] update `aud`, `tape` [`dc26af2`](https://github.com/ljharb/json-stable-stringify/commit/dc26af2cac5caa4e9ecb72f384c3c652aa612457) | ||
## [v1.1.0](https://github.com/ljharb/json-stable-stringify/compare/v1.0.2...v1.1.0) - 2023-11-13 | ||
@@ -10,0 +21,0 @@ |
23
index.js
@@ -8,3 +8,7 @@ 'use strict'; | ||
var callBind = require('call-bind'); | ||
var callBound = require('call-bind/callBound'); | ||
var $join = callBound('Array.prototype.join'); | ||
var $push = callBound('Array.prototype.push'); | ||
var strRepeat = function repeat(n, char) { | ||
@@ -57,11 +61,8 @@ var str = ''; | ||
if (isArray(node)) { | ||
var out = ''; | ||
var out = []; | ||
for (var i = 0; i < node.length; i++) { | ||
var item = stringify(node, i, node[i], level + 1) || jsonStringify(null); | ||
out += indent + space + item; | ||
if ((i + 1) < node.length) { | ||
out += ','; | ||
} | ||
$push(out, indent + space + item); | ||
} | ||
return '[' + out + indent + ']'; | ||
return '[' + $join(out, ',') + indent + ']'; | ||
} | ||
@@ -72,7 +73,6 @@ | ||
throw new TypeError('Converting circular structure to JSON'); | ||
} else { seen.push(node); } | ||
} else { $push(seen, node); } | ||
var keys = objectKeys(node).sort(cmp && cmp(node)); | ||
var out = ''; | ||
var needsComma = false; | ||
var out = []; | ||
for (var i = 0; i < keys.length; i++) { | ||
@@ -88,9 +88,8 @@ var key = keys[i]; | ||
out += (needsComma ? ',' : '') + indent + space + keyValue; | ||
needsComma = true; | ||
$push(out, indent + space + keyValue); | ||
} | ||
seen.splice(seen.indexOf(node), 1); | ||
return '{' + out + indent + '}'; | ||
return '{' + $join(out, ',') + indent + '}'; | ||
}({ '': obj }, '', obj, 0)); | ||
}; |
{ | ||
"name": "json-stable-stringify", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"@ljharb/eslint-config": "^21.1.0", | ||
"aud": "^2.0.3", | ||
"aud": "^2.0.4", | ||
"auto-changelog": "^2.4.0", | ||
@@ -21,3 +21,3 @@ "eslint": "=8.8.0", | ||
"safe-publish-latest": "^2.0.0", | ||
"tape": "^5.7.2" | ||
"tape": "^5.7.3" | ||
}, | ||
@@ -24,0 +24,0 @@ "scripts": { |
@@ -21,5 +21,5 @@ # json-stable-stringify <sup>[![Version Badge][npm-version-svg]][package-url]</sup> | ||
``` js | ||
var stringify = require('json-stable-stringify'); | ||
const stringify = require('json-stable-stringify'); | ||
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; | ||
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }; | ||
@@ -38,6 +38,7 @@ console.log(stringify(obj)); | ||
``` js | ||
var stringify = require('json-stable-stringify') | ||
const stringify = require('json-stable-stringify') | ||
``` | ||
## var str = stringify(obj, opts) | ||
<a id="var-str--stringifyobj-opts"></a> | ||
## const str = stringify(obj, opts) | ||
@@ -60,7 +61,7 @@ Return a deterministic stringified string `str` from the object `obj`. | ||
``` js | ||
var stringify = require('json-stable-stringify'); | ||
const stringify = require('json-stable-stringify'); | ||
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 }; | ||
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 },7], a: 3 }; | ||
var s = stringify(obj, function (a, b) { | ||
const s = stringify(obj, function (a, b) { | ||
return b.key.localeCompare(a.key); | ||
@@ -81,7 +82,7 @@ }); | ||
``` js | ||
var stringify = require('json-stable-stringify'); | ||
const stringify = require('json-stable-stringify'); | ||
var obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 }; | ||
const obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 }; | ||
var s = stringify(obj, function (a, b) { | ||
const s = stringify(obj, function (a, b) { | ||
return a.value < b.value ? 1 : -1; | ||
@@ -110,5 +111,5 @@ }); | ||
```js | ||
var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }; | ||
const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }; | ||
var s = stringify(obj, { space: ' ' }); | ||
const s = stringify(obj, { space: ' ' }); | ||
@@ -115,0 +116,0 @@ console.log(s); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27748
162
328