pax_global_header00006660000000000000000000000064126541700000014505gustar00rootroot0000000000000052 comment=4a3ac9cc006a91e64901f8ebe78d23bf9fc9fbd0 json-stable-stringify-1.0.1/000077500000000000000000000000001265417000000157415ustar00rootroot00000000000000json-stable-stringify-1.0.1/.gitignore000066400000000000000000000000151265417000000177250ustar00rootroot00000000000000node_modules json-stable-stringify-1.0.1/.travis.yml000066400000000000000000000000601265417000000200460ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10" json-stable-stringify-1.0.1/LICENSE000066400000000000000000000020611265417000000167450ustar00rootroot00000000000000This 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. json-stable-stringify-1.0.1/example/000077500000000000000000000000001265417000000173745ustar00rootroot00000000000000json-stable-stringify-1.0.1/example/key_cmp.js000066400000000000000000000002611265417000000213600ustar00rootroot00000000000000var 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); json-stable-stringify-1.0.1/example/nested.js000066400000000000000000000001551265417000000212150ustar00rootroot00000000000000var stringify = require('../'); var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 }; console.log(stringify(obj)); json-stable-stringify-1.0.1/example/str.js000066400000000000000000000001411265417000000205360ustar00rootroot00000000000000var stringify = require('../'); var obj = { c: 6, b: [4,5], a: 3 }; console.log(stringify(obj)); json-stable-stringify-1.0.1/example/value_cmp.js000066400000000000000000000002741265417000000217100ustar00rootroot00000000000000var 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); json-stable-stringify-1.0.1/index.js000066400000000000000000000054551265417000000174170ustar00rootroot00000000000000var json = typeof JSON !== 'undefined' ? JSON : require('jsonify'); module.exports = function (obj, 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; }; 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); var seen = []; return (function stringify (parent, key, node, level) { var indent = space ? ('\n' + new Array(level + 1).join(space)) : ''; var colonSeparator = space ? ': ' : ':'; if (node && node.toJSON && typeof node.toJSON === 'function') { node = node.toJSON(); } node = replacer.call(parent, key, 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); } return '[' + out.join(',') + indent + ']'; } 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(!value) continue; var keyValue = json.stringify(key) + colonSeparator + value; ; out.push(indent + space + keyValue); } seen.splice(seen.indexOf(node), 1); return '{' + out.join(',') + indent + '}'; } })({ '': obj }, '', obj, 0); }; 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; }; json-stable-stringify-1.0.1/package.json000066400000000000000000000017131265417000000202310ustar00rootroot00000000000000{ "name": "json-stable-stringify", "version": "1.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" } json-stable-stringify-1.0.1/readme.markdown000066400000000000000000000051111265417000000207400ustar00rootroot00000000000000# 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. [![browser support](https://ci.testling.com/substack/json-stable-stringify.png)](https://ci.testling.com/substack/json-stable-stringify) [![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 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`. ## options ### cmp 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} ``` ### space 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}`). For example: ```js var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }; var s = stringify(obj, { space: ' ' }); console.log(s); ``` which outputs: ``` { "a": { "and": [ 1, 2, 3 ], "foo": "bar" }, "b": 1 } ``` ### 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). # install With [npm](https://npmjs.org) do: ``` npm install json-stable-stringify ``` # license MIT json-stable-stringify-1.0.1/test/000077500000000000000000000000001265417000000167205ustar00rootroot00000000000000json-stable-stringify-1.0.1/test/cmp.js000066400000000000000000000005171265417000000200400ustar00rootroot00000000000000var 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}'); }); json-stable-stringify-1.0.1/test/nested.js000066400000000000000000000016431265417000000205440ustar00rootroot00000000000000var 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}'); }); test('cyclic (default)', function (t) { t.plan(1); var one = { a: 1 }; var two = { a: 2, one: one }; one.two = two; try { stringify(one); } catch (ex) { t.equal(ex.toString(), 'TypeError: Converting circular structure to JSON'); } }); test('cyclic (specifically allowed)', function (t) { t.plan(1); var one = { a: 1 }; var two = { a: 2, one: one }; one.two = two; t.equal(stringify(one, {cycles:true}), '{"a":1,"two":{"a":2,"one":"__cycle__"}}'); }); test('repeated non-cyclic value', function(t) { t.plan(1); var one = { x: 1 }; var two = { a: one, b: one }; t.equal(stringify(two), '{"a":{"x":1},"b":{"x":1}}'); }); json-stable-stringify-1.0.1/test/replacer.js000066400000000000000000000033211265417000000210520ustar00rootroot00000000000000var test = require('tape'); var stringify = require('../'); test('replace root', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function(key, value) { return 'one'; }; t.equal(stringify(obj, { replacer: replacer }), '"one"'); }); test('replace numbers', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function(key, value) { if(value === 1) return 'one'; if(value === 2) return 'two'; return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":"two","c":false}'); }); test('replace with object', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function(key, value) { if(key === 'b') return { d: 1 }; if(value === 1) return 'one'; return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":{"d":"one"},"c":false}'); }); test('replace with undefined', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function(key, value) { if(value === false) return; return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":1,"b":2}'); }); test('replace with array', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: false }; var replacer = function(key, value) { if(key === 'b') return ['one', 'two']; return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":1,"b":["one","two"],"c":false}'); }); test('replace array item', function (t) { t.plan(1); var obj = { a: 1, b: 2, c: [1,2] }; var replacer = function(key, value) { if(value === 1) return 'one'; if(value === 2) return 'two'; return value; }; t.equal(stringify(obj, { replacer: replacer }), '{"a":"one","b":"two","c":["one","two"]}'); }); json-stable-stringify-1.0.1/test/space.js000066400000000000000000000026311265417000000203530ustar00rootroot00000000000000var test = require('tape'); var stringify = require('../'); test('space parameter', function (t) { t.plan(1); var obj = { one: 1, two: 2 }; t.equal(stringify(obj, {space: ' '}), '' + '{\n' + ' "one": 1,\n' + ' "two": 2\n' + '}' ); }); test('space parameter (with tabs)', function (t) { t.plan(1); var obj = { one: 1, two: 2 }; t.equal(stringify(obj, {space: '\t'}), '' + '{\n' + '\t"one": 1,\n' + '\t"two": 2\n' + '}' ); }); test('space parameter (with a number)', function (t) { t.plan(1); var obj = { one: 1, two: 2 }; t.equal(stringify(obj, {space: 3}), '' + '{\n' + ' "one": 1,\n' + ' "two": 2\n' + '}' ); }); test('space parameter (nested objects)', function (t) { t.plan(1); var obj = { one: 1, two: { b: 4, a: [2,3] } }; t.equal(stringify(obj, {space: ' '}), '' + '{\n' + ' "one": 1,\n' + ' "two": {\n' + ' "a": [\n' + ' 2,\n' + ' 3\n' + ' ],\n' + ' "b": 4\n' + ' }\n' + '}' ); }); test('space parameter (same as native)', function (t) { t.plan(1); // for this test, properties need to be in alphabetical order var obj = { one: 1, two: { a: [2,3], b: 4 } }; t.equal(stringify(obj, {space: ' '}), JSON.stringify(obj, null, ' ')); }); json-stable-stringify-1.0.1/test/str.js000066400000000000000000000013661265417000000200740ustar00rootroot00000000000000var 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}'); }); test('object with undefined', function (t) { t.plan(1); var obj = { a: 3, z: undefined }; t.equal(stringify(obj), '{"a":3}'); }); test('array with undefined', function (t) { t.plan(1); var obj = [4, undefined, 6]; t.equal(stringify(obj), '[4,null,6]'); }); test('object with empty string', function (t) { t.plan(1); var obj = { a: 3, z: '' }; t.equal(stringify(obj), '{"a":3,"z":""}'); }); test('array with empty string', function (t) { t.plan(1); var obj = [4, '', 6]; t.equal(stringify(obj), '[4,"",6]'); }); json-stable-stringify-1.0.1/test/to-json.js000066400000000000000000000010761265417000000206530ustar00rootroot00000000000000var test = require('tape'); var stringify = require('../'); test('toJSON function', function (t) { t.plan(1); var obj = { one: 1, two: 2, toJSON: function() { return { one: 1 }; } }; t.equal(stringify(obj), '{"one":1}' ); }); test('toJSON returns string', function (t) { t.plan(1); var obj = { one: 1, two: 2, toJSON: function() { return 'one'; } }; t.equal(stringify(obj), '"one"'); }); test('toJSON returns array', function (t) { t.plan(1); var obj = { one: 1, two: 2, toJSON: function() { return ['one']; } }; t.equal(stringify(obj), '["one"]'); });