json-stringify-safe
Advanced tools
Comparing version 5.0.0 to 5.0.1
{ | ||
"name": "json-stringify-safe", | ||
"version": "5.0.0", | ||
"description": "Like JSON.stringify, but doesn't blow up on circular refs", | ||
"main": "stringify.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/isaacs/json-stringify-safe" | ||
}, | ||
"version": "5.0.1", | ||
"description": "Like JSON.stringify, but doesn't blow up on circular refs.", | ||
"keywords": [ | ||
@@ -19,5 +11,22 @@ "json", | ||
], | ||
"homepage": "https://github.com/isaacs/json-stringify-safe", | ||
"bugs": "https://github.com/isaacs/json-stringify-safe/issues", | ||
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me)", | ||
"license": "BSD", | ||
"readmeFilename": "README.md" | ||
"contributors": [ | ||
"Andri Möll <andri@dot.ee> (http://themoll.com)" | ||
], | ||
"license": "ISC", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/isaacs/json-stringify-safe" | ||
}, | ||
"main": "stringify.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
}, | ||
"devDependencies": { | ||
"mocha": ">= 2.1.0 < 3", | ||
"must": ">= 0.12 < 0.13", | ||
"sinon": ">= 1.12.2 < 2" | ||
} | ||
} |
@@ -50,1 +50,4 @@ # json-stringify-safe | ||
function that's passed to JSON.stringify. | ||
**Note** that the function returned from `getSerialize` is stateful for now, so | ||
do **not** use it more than once. |
@@ -1,39 +0,27 @@ | ||
module.exports = stringify; | ||
exports = module.exports = stringify | ||
exports.getSerialize = serializer | ||
function getSerialize (fn, decycle) { | ||
var seen = [], keys = []; | ||
decycle = decycle || function(key, value) { | ||
return '[Circular ' + getPath(value, seen, keys) + ']' | ||
}; | ||
function stringify(obj, replacer, spaces, cycleReplacer) { | ||
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces) | ||
} | ||
function serializer(replacer, cycleReplacer) { | ||
var stack = [], keys = [] | ||
if (cycleReplacer == null) cycleReplacer = function(key, value) { | ||
if (stack[0] === value) return "[Circular ~]" | ||
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]" | ||
} | ||
return function(key, value) { | ||
var ret = value; | ||
if (typeof value === 'object' && value) { | ||
if (seen.indexOf(value) !== -1) | ||
ret = decycle(key, value); | ||
else { | ||
seen.push(value); | ||
keys.push(key); | ||
} | ||
if (stack.length > 0) { | ||
var thisPos = stack.indexOf(this) | ||
~thisPos ? stack.splice(thisPos + 1) : stack.push(this) | ||
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key) | ||
if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value) | ||
} | ||
if (fn) ret = fn(key, ret); | ||
return ret; | ||
} | ||
} | ||
else stack.push(value) | ||
function getPath (value, seen, keys) { | ||
var index = seen.indexOf(value); | ||
var path = [ keys[index] ]; | ||
for (index--; index >= 0; index--) { | ||
if (seen[index][ path[0] ] === value) { | ||
value = seen[index]; | ||
path.unshift(keys[index]); | ||
} | ||
return replacer == null ? value : replacer.call(this, key, value) | ||
} | ||
return '~' + path.join('.'); | ||
} | ||
function stringify(obj, fn, spaces, decycle) { | ||
return JSON.stringify(obj, getSerialize(fn, decycle), spaces); | ||
} | ||
stringify.getSerialize = getSerialize; |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12722
9
228
53
3
1