javascript-stringify
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -226,5 +226,8 @@ (function (root, stringify) { | ||
* @param {(Number|String)} [space] | ||
* @param {Object} [options] | ||
* @return {String} | ||
*/ | ||
return function (value, replacer, space) { | ||
return function (value, replacer, space, options) { | ||
options = options || {} | ||
// Convert the spaces into a string. | ||
@@ -235,2 +238,7 @@ if (typeof space !== 'string') { | ||
var maxDepth = options.maxDepth || 200; | ||
var depth = 0; | ||
var cache = []; | ||
/** | ||
@@ -240,8 +248,7 @@ * Handle recursion by checking if we've visited this node every iteration. | ||
* @param {*} value | ||
* @param {Array} cache | ||
* @return {String} | ||
*/ | ||
var recurse = function (value, cache, next) { | ||
var recurse = function (value, next) { | ||
// If we've already visited this node before, break the recursion. | ||
if (cache.indexOf(value) > -1) { | ||
if (cache.indexOf(value) > -1 || depth > maxDepth) { | ||
return; | ||
@@ -251,2 +258,3 @@ } | ||
// Push the value into the values cache to avoid an infinite loop. | ||
depth++; | ||
cache.push(value); | ||
@@ -256,3 +264,8 @@ | ||
return next(value, space, function (value) { | ||
return recurse(value, cache.slice(), next); | ||
var result = recurse(value, next); | ||
depth--; | ||
cache.pop(); | ||
return result; | ||
}); | ||
@@ -264,3 +277,3 @@ }; | ||
if (typeof replacer === 'function') { | ||
return recurse(value, [], function (value, space, next) { | ||
return recurse(value, function (value, space, next) { | ||
return replacer(value, space, function (value) { | ||
@@ -272,4 +285,4 @@ return stringify(value, space, next); | ||
return recurse(value, [], stringify); | ||
return recurse(value, stringify); | ||
}; | ||
}); |
{ | ||
"name": "javascript-stringify", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`", | ||
@@ -5,0 +5,0 @@ "main": "javascript-stringify.js", |
@@ -40,3 +40,3 @@ # JavaScript Stringify | ||
```javascript | ||
javascriptStringify(value[, replacer [, space]]) | ||
javascriptStringify(value[, replacer [, space [, options]]]) | ||
``` | ||
@@ -46,2 +46,6 @@ | ||
The `options` object allows some additional configuration: | ||
* **maxDepth** The maximum depth to stringify to | ||
### Examples | ||
@@ -57,2 +61,4 @@ | ||
javascriptStringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}" | ||
/** | ||
@@ -59,0 +65,0 @@ * Invalid key names are automatically stringified. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13164
245
115