Socket
Socket
Sign inDemoInstall

fast-json-stable-stringify

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-json-stable-stringify - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

.eslintrc.yml

83

index.js
'use strict';
module.exports = function (obj, opts) {
module.exports = function (data, opts) {
if (!opts) opts = {};
if (typeof opts === 'function') opts = { cmp: opts };
var space = opts.space || '';
if (typeof space === 'number') space = Array(space+1).join(' ');
var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
var replacer = opts.replacer || function(key, value) { return value; };

@@ -22,6 +19,3 @@ var cmp = opts.cmp && (function (f) {

var seen = [];
return (function stringify (parent, key, node, level) {
var indent = space ? ('\n' + new Array(level + 1).join(space)) : '';
var colonSeparator = space ? ': ' : ':';
return (function stringify (node) {
if (node && node.toJSON && typeof node.toJSON === 'function') {

@@ -31,56 +25,37 @@ node = node.toJSON();

node = replacer.call(parent, key, node);
if (node === undefined) return;
if (typeof node == 'number') return isFinite(node) ? '' + node : 'null';
if (typeof node !== 'object') return JSON.stringify(node);
if (node === undefined) {
return;
}
if (typeof node !== 'object' || node === null) {
return JSON.stringify(node);
}
if (isArray(node)) {
var out = [];
for (var i = 0; i < node.length; i++) {
var item = stringify(node, i, node[i], level+1) || JSON.stringify(null);
out.push(indent + space + item);
var i, out;
if (Array.isArray(node)) {
out = '[';
for (i = 0; i < node.length; i++) {
if (i) out += ',';
out += stringify(node[i]) || 'null';
}
return '[' + out.join(',') + indent + ']';
return out + ']';
}
else {
if (seen.indexOf(node) !== -1) {
if (cycles) return JSON.stringify('__cycle__');
throw new TypeError('Converting circular structure to JSON');
}
else seen.push(node);
var keys = objectKeys(node).sort(cmp && cmp(node));
var out = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = stringify(node, key, node[key], level+1);
if (node === null) return 'null';
if(!value) continue;
var keyValue = JSON.stringify(key)
+ colonSeparator
+ value;
;
out.push(indent + space + keyValue);
}
seen.splice(seen.indexOf(node), 1);
return '{' + out.join(',') + indent + '}';
if (seen.indexOf(node) !== -1) {
if (cycles) return JSON.stringify('__cycle__');
throw new TypeError('Converting circular structure to JSON');
}
})({ '': obj }, '', obj, 0);
};
var isArray = Array.isArray || function (x) {
return {}.toString.call(x) === '[object Array]';
};
var seenIndex = seen.push(node) - 1;
var keys = Object.keys(node).sort(cmp && cmp(node));
out = '';
for (i = 0; i < keys.length; i++) {
var key = keys[i];
var value = stringify(node[key]);
var objectKeys = Object.keys || function (obj) {
var has = Object.prototype.hasOwnProperty || function () { return true };
var keys = [];
for (var key in obj) {
if (has.call(obj, key)) keys.push(key);
}
return keys;
if (!value) continue;
if (out) out += ',';
out += JSON.stringify(key) + ':' + value;
}
seen.splice(seenIndex, 1);
return '{' + out + '}';
})(data);
};
{
"name": "fast-json-stable-stringify",
"version": "1.0.2",
"description": "deterministic JSON.stringify() to get deterministic hashes from stringified results",
"version": "2.0.0",
"description": "deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify",
"main": "index.js",
"devDependencies": {
"benchmark": "^2.1.4",
"coveralls": "^3.0.0",
"eslint": "^4.9.0",
"fast-stable-stringify": "latest",
"faster-stable-stringify": "latest",
"json-stable-stringify": "latest",
"nyc": "^11.2.1",
"pre-commit": "^1.2.2",
"tape": "~1.0.4"
},
"scripts": {
"test": "tape test/*.js"
"eslint": "eslint index.js test",
"test-spec": "tape test/*.js",
"test": "npm run eslint && nyc npm run test-spec"
},
"testling": {
"files": "test/*.js",
"browsers": [
"ie/8..latest",
"ff/5",
"ff/latest",
"chrome/15",
"chrome/latest",
"safari/latest",
"opera/latest"
]
},
"repository": {

@@ -41,3 +39,13 @@ "type": "git",

},
"license": "MIT"
"license": "MIT",
"nyc": {
"exclude": [
"test",
"node_modules"
],
"reporter": [
"lcov",
"text-summary"
]
}
}

@@ -1,16 +0,14 @@

# json-stable-stringify
# fast-json-stable-stringify
deterministic version of `JSON.stringify()` so you can get a consistent hash
from stringified results
Deterministic `JSON.stringify()` - a faster version of [@substack](https://github.com/substack)'s json-stable-strigify without [jsonify](https://github.com/substack/jsonify).
You can also pass in a custom comparison function.
[![browser support](https://ci.testling.com/substack/json-stable-stringify.png)](https://ci.testling.com/substack/json-stable-stringify)
[![Build Status](https://travis-ci.org/epoberezkin/fast-json-stable-stringify.svg?branch=master)](https://travis-ci.org/epoberezkin/fast-json-stable-stringify)
[![Coverage Status](https://coveralls.io/repos/github/epoberezkin/fast-json-stable-stringify/badge.svg?branch=master)](https://coveralls.io/github/epoberezkin/fast-json-stable-stringify?branch=master)
[![build status](https://secure.travis-ci.org/substack/json-stable-stringify.png)](http://travis-ci.org/substack/json-stable-stringify)
# example
``` js
var stringify = require('json-stable-stringify');
var stringify = require('fast-json-stable-stringify');
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };

@@ -26,6 +24,7 @@ console.log(stringify(obj));

# methods
``` js
var stringify = require('json-stable-stringify')
var stringify = require('fast-json-stable-stringify')
```

@@ -37,2 +36,3 @@

## options

@@ -53,3 +53,3 @@

``` js
var stringify = require('json-stable-stringify');
var stringify = require('fast-json-stable-stringify');

@@ -72,3 +72,3 @@ var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };

```
var stringify = require('json-stable-stringify');
var stringify = require('fast-json-stable-stringify');

@@ -88,48 +88,37 @@ var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };

### space
### cycles
If you specify `opts.space`, it will indent the output for pretty-printing.
Valid values are strings (e.g. `{space: \t}`) or a number of spaces
(`{space: 3}`).
Pass `true` in `opts.cycles` to stringify circular property as `__cycle__` - the result will not be a valid JSON string in this case.
For example:
TypeError will be thrown in case of circular object without this option.
```js
var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
var s = stringify(obj, { space: ' ' });
console.log(s);
```
which outputs:
# install
With [npm](https://npmjs.org) do:
```
{
"a": {
"and": [
1,
2,
3
],
"foo": "bar"
},
"b": 1
}
npm install fast-json-stable-stringify
```
### replacer
The replacer parameter is a function `opts.replacer(key, value)` that behaves
the same as the replacer
[from the core JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_native_JSON#The_replacer_parameter).
# benchmark
# install
To run benchmark (requires Node.js 6+):
```
node benchmark
```
With [npm](https://npmjs.org) do:
Results:
```
npm install json-stable-stringify
fast-json-stable-stringify x 17,189 ops/sec ±1.43% (83 runs sampled)
json-stable-stringify x 13,634 ops/sec ±1.39% (85 runs sampled)
fast-stable-stringify x 20,212 ops/sec ±1.20% (84 runs sampled)
faster-stable-stringify x 15,549 ops/sec ±1.12% (84 runs sampled)
The fastest is fast-stable-stringify
```
# license
MIT
[MIT](https://github.com/epoberezkin/fast-json-stable-stringify/blob/master/LICENSE)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc