json-stable-stringify-pretty
Advanced tools
Comparing version
11
index.js
@@ -12,2 +12,3 @@ var json = typeof JSON !== 'undefined' ? JSON : require('jsonify'); | ||
var sortarrays = opts.sortarrays === true; | ||
var undef = opts.undef === true; | ||
@@ -29,2 +30,6 @@ var cmp = opts.cmp && (function (f) { | ||
// A custom hack for mongodb objectids. | ||
if (node && node.toHexString && typeof node.toHexString === 'function') { | ||
node = 'id$' + node.toHexString(); | ||
} | ||
if (node && node.toJSON && typeof node.toJSON === 'function') { | ||
@@ -37,3 +42,7 @@ node = node.toJSON(); | ||
if (node === undefined) { | ||
if (undef) { | ||
return 'undefined'; | ||
} else { | ||
return; | ||
} | ||
} | ||
@@ -49,3 +58,3 @@ if (pretty && typeof node === 'string') { | ||
for (var i = 0; i < node.length; i++) { | ||
var item = stringify(node, i, node[i], level+1) || json.stringify(null); | ||
var item = stringify(node, i, node[i], level+1); | ||
out.push(indent + space + item); | ||
@@ -52,0 +61,0 @@ } |
{ | ||
"name": "json-stable-stringify-pretty", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A fork of substack's json-stable-stringify: deterministic JSON.stringify() with useful options", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -11,11 +11,15 @@ var test = require('tape'); | ||
test('object with undefined', function (t) { | ||
t.plan(1); | ||
t.plan(4); | ||
var obj = { a: 3, z: undefined }; | ||
t.equal(stringify(obj), '{"a":3}'); | ||
t.equal(stringify(obj, {undef: true}), '{"a":3,"z":undefined}'); | ||
t.equal(stringify(obj, {undef: true, pretty: true}), '{a:3,z:undefined}'); | ||
t.equal(stringify(obj, {undef: true, pretty: true, space: 2}), '{\n a: 3,\n z: undefined\n}'); | ||
}); | ||
test('array with undefined', function (t) { | ||
t.plan(1); | ||
t.plan(2); | ||
var obj = [4, undefined, 6]; | ||
t.equal(stringify(obj), '[4,null,6]'); | ||
t.equal(stringify(obj), '[4,undefined,6]'); | ||
t.equal(stringify(obj, {undef: true}), '[4,undefined,6]'); | ||
}); | ||
@@ -22,0 +26,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19766
8.51%17
6.25%410
7.33%207
11.89%