init
This commit is contained in:
4
node_modules/json-stable-stringify/.travis.yml
generated
vendored
Normal file
4
node_modules/json-stable-stringify/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.8"
|
||||
- "0.10"
|
18
node_modules/json-stable-stringify/LICENSE
generated
vendored
Normal file
18
node_modules/json-stable-stringify/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
This software is released under the MIT license:
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
7
node_modules/json-stable-stringify/example/key_cmp.js
generated
vendored
Normal file
7
node_modules/json-stable-stringify/example/key_cmp.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
var stringify = require('../');
|
||||
|
||||
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
|
||||
var s = stringify(obj, function (a, b) {
|
||||
return a.key < b.key ? 1 : -1;
|
||||
});
|
||||
console.log(s);
|
3
node_modules/json-stable-stringify/example/nested.js
generated
vendored
Normal file
3
node_modules/json-stable-stringify/example/nested.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var stringify = require('../');
|
||||
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
|
||||
console.log(stringify(obj));
|
3
node_modules/json-stable-stringify/example/str.js
generated
vendored
Normal file
3
node_modules/json-stable-stringify/example/str.js
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
var stringify = require('../');
|
||||
var obj = { c: 6, b: [4,5], a: 3 };
|
||||
console.log(stringify(obj));
|
7
node_modules/json-stable-stringify/example/value_cmp.js
generated
vendored
Normal file
7
node_modules/json-stable-stringify/example/value_cmp.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
var stringify = require('../');
|
||||
|
||||
var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
|
||||
var s = stringify(obj, function (a, b) {
|
||||
return a.value < b.value ? 1 : -1;
|
||||
});
|
||||
console.log(s);
|
50
node_modules/json-stable-stringify/index.js
generated
vendored
Normal file
50
node_modules/json-stable-stringify/index.js
generated
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');
|
||||
|
||||
module.exports = function (obj, opts) {
|
||||
if (!opts) opts = {};
|
||||
if (typeof opts === 'function') opts = { cmp: opts };
|
||||
var cmp = opts.cmp && (function (f) {
|
||||
return function (node) {
|
||||
return function (a, b) {
|
||||
var aobj = { key: a, value: node[a] };
|
||||
var bobj = { key: b, value: node[b] };
|
||||
return f(aobj, bobj);
|
||||
};
|
||||
};
|
||||
})(opts.cmp);
|
||||
|
||||
return (function stringify (node) {
|
||||
if (typeof node !== 'object' || node === null) {
|
||||
return json.stringify(node);
|
||||
}
|
||||
if (isArray(node)) {
|
||||
var out = [];
|
||||
for (var i = 0; i < node.length; i++) {
|
||||
out.push(stringify(node[i]));
|
||||
}
|
||||
return '[' + out.join(',') + ']';
|
||||
}
|
||||
else {
|
||||
var keys = objectKeys(node).sort(cmp && cmp(node));
|
||||
var out = [];
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
out.push(stringify(key) + ':' + stringify(node[key]));
|
||||
}
|
||||
return '{' + out.join(',') + '}';
|
||||
}
|
||||
})(obj);
|
||||
};
|
||||
|
||||
var isArray = Array.isArray || function (x) {
|
||||
return {}.toString.call(x) === '[object Array]';
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
44
node_modules/json-stable-stringify/package.json
generated
vendored
Normal file
44
node_modules/json-stable-stringify/package.json
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "json-stable-stringify",
|
||||
"version": "0.0.1",
|
||||
"description": "deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"jsonify": "~0.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tape": "~1.0.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "tape test/*.js"
|
||||
},
|
||||
"testling": {
|
||||
"files": "test/*.js",
|
||||
"browsers": [
|
||||
"ie/8..latest",
|
||||
"ff/5", "ff/latest",
|
||||
"chrome/15", "chrome/latest",
|
||||
"safari/latest",
|
||||
"opera/latest"
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/substack/json-stable-stringify.git"
|
||||
},
|
||||
"homepage": "https://github.com/substack/json-stable-stringify",
|
||||
"keywords": [
|
||||
"json",
|
||||
"stringify",
|
||||
"deterministic",
|
||||
"hash",
|
||||
"sort",
|
||||
"stable"
|
||||
],
|
||||
"author": {
|
||||
"name": "James Halliday",
|
||||
"email": "mail@substack.net",
|
||||
"url": "http://substack.net"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
90
node_modules/json-stable-stringify/readme.markdown
generated
vendored
Normal file
90
node_modules/json-stable-stringify/readme.markdown
generated
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
# json-stable-stringify
|
||||
|
||||
deterministic version of `JSON.stringify()` so you can get a consistent hash
|
||||
from stringified results
|
||||
|
||||
You can also pass in a custom comparison function.
|
||||
|
||||
[](https://ci.testling.com/substack/json-stable-stringify)
|
||||
|
||||
[](http://travis-ci.org/substack/json-stable-stringify)
|
||||
|
||||
# example
|
||||
|
||||
``` js
|
||||
var stringify = require('json-stable-stringify');
|
||||
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
|
||||
console.log(stringify(obj));
|
||||
```
|
||||
|
||||
output:
|
||||
|
||||
```
|
||||
{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
|
||||
```
|
||||
|
||||
# methods
|
||||
|
||||
``` js
|
||||
var stringify = require('json-stable-stringify')
|
||||
```
|
||||
|
||||
## var str = stringify(obj, opts)
|
||||
|
||||
Return a deterministic stringified string `str` from the object `obj`.
|
||||
|
||||
If `opts` is given, you can supply an `opts.cmp` to have a custom comparison
|
||||
function for object keys. Your function `opts.cmp` is called with these
|
||||
parameters:
|
||||
|
||||
``` js
|
||||
opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })
|
||||
```
|
||||
|
||||
For example, to sort on the object key names in reverse order you could write:
|
||||
|
||||
``` js
|
||||
var stringify = require('json-stable-stringify');
|
||||
|
||||
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
|
||||
var s = stringify(obj, function (a, b) {
|
||||
return a.key < b.key ? 1 : -1;
|
||||
});
|
||||
console.log(s);
|
||||
```
|
||||
|
||||
which results in the output string:
|
||||
|
||||
```
|
||||
{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
|
||||
```
|
||||
|
||||
Or if you wanted to sort on the object values in reverse order, you could write:
|
||||
|
||||
```
|
||||
var stringify = require('json-stable-stringify');
|
||||
|
||||
var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
|
||||
var s = stringify(obj, function (a, b) {
|
||||
return a.value < b.value ? 1 : -1;
|
||||
});
|
||||
console.log(s);
|
||||
```
|
||||
|
||||
which outputs:
|
||||
|
||||
```
|
||||
{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
|
||||
```
|
||||
|
||||
# install
|
||||
|
||||
With [npm](https://npmjs.org) do:
|
||||
|
||||
```
|
||||
npm install json-stable-stringify
|
||||
```
|
||||
|
||||
# license
|
||||
|
||||
MIT
|
11
node_modules/json-stable-stringify/test/cmp.js
generated
vendored
Normal file
11
node_modules/json-stable-stringify/test/cmp.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
var test = require('tape');
|
||||
var stringify = require('../');
|
||||
|
||||
test('custom comparison function', function (t) {
|
||||
t.plan(1);
|
||||
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
|
||||
var s = stringify(obj, function (a, b) {
|
||||
return a.key < b.key ? 1 : -1;
|
||||
});
|
||||
t.equal(s, '{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}');
|
||||
});
|
8
node_modules/json-stable-stringify/test/nested.js
generated
vendored
Normal file
8
node_modules/json-stable-stringify/test/nested.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
var test = require('tape');
|
||||
var stringify = require('../');
|
||||
|
||||
test('nested', function (t) {
|
||||
t.plan(1);
|
||||
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
|
||||
t.equal(stringify(obj), '{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}');
|
||||
});
|
8
node_modules/json-stable-stringify/test/str.js
generated
vendored
Normal file
8
node_modules/json-stable-stringify/test/str.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
var test = require('tape');
|
||||
var stringify = require('../');
|
||||
|
||||
test('simple object', function (t) {
|
||||
t.plan(1);
|
||||
var obj = { c: 6, b: [4,5], a: 3, z: null };
|
||||
t.equal(stringify(obj), '{"a":3,"b":[4,5],"c":6,"z":null}');
|
||||
});
|
Reference in New Issue
Block a user