Comparing version 3.1.11 to 3.1.12
@@ -107,3 +107,3 @@ var pathGetter = require('./path-getter'); | ||
if (typeof value === 'object' && value !== null) { | ||
if (typeof value === 'object' && options['refs']) { | ||
var foundPath = map.get(value); | ||
@@ -110,0 +110,0 @@ if (foundPath) { |
var cycle = require('./cycle'); | ||
exports.stringify = function stringify(value, replacer, space, _options) { | ||
if (arguments.length < 4) { | ||
try { | ||
if (arguments.length === 1) { | ||
return JSON.stringify(value); | ||
} else { | ||
return JSON.stringify.apply(JSON, arguments); | ||
} | ||
} catch (e) {} | ||
} | ||
var options = _options || false; | ||
@@ -19,2 +30,3 @@ if (typeof options === 'boolean') { | ||
} | ||
if (typeof options.refs === 'undefined') options.refs = true; | ||
@@ -32,10 +44,12 @@ var decycled = cycle.decycle(value, options, replacer); | ||
var needsRetrocycle = /"\$jsan"/.test(text); | ||
var replaced = JSON.parse(text); | ||
var resolved = cycle.retrocycle(replaced); | ||
if(reviver) { | ||
var temp = JSON.stringify(resolved); | ||
var parsed = JSON.parse(temp, reviver); | ||
return parsed; | ||
var parsed; | ||
if (arguments.length === 1) { | ||
parsed = JSON.parse(text); | ||
} else { | ||
parsed = JSON.parse(text, reviver); | ||
} | ||
return resolved; | ||
if (needsRetrocycle) { | ||
parsed = cycle.retrocycle(parsed); | ||
} | ||
return parsed; | ||
} |
{ | ||
"name": "jsan", | ||
"version": "3.1.11", | ||
"version": "3.1.12", | ||
"description": "handle circular references when stringifying and parsing", | ||
@@ -18,8 +18,7 @@ "main": "index.js", | ||
"circular-json": "^0.3.0", | ||
"immutable": "^3.8.2", | ||
"immutable": "^3.7.6", | ||
"json-stringify-safe": "^5.0.1", | ||
"mobx": "^2.4.1", | ||
"mocha": "^2.2.1", | ||
"rimraf": "^2.5.2", | ||
"remotedev-serialize": "^0.1.1" | ||
"rimraf": "^2.5.2" | ||
}, | ||
@@ -26,0 +25,0 @@ "dependencies": {}, |
@@ -193,2 +193,11 @@ var assert = require('assert'); | ||
it('can use the refs option', function() { | ||
var obj1 = { a: 1 }; | ||
var obj = { "prop1": obj1, "prop2": { "prop3": obj1 } }; | ||
assert.equal(jsan.stringify(obj, null, null, {refs: true}), '{"prop1":{"a":1},"prop2":{"prop3":{"$jsan":"$.prop1"}}}'); | ||
assert.equal(jsan.stringify(obj, null, null, true), '{"prop1":{"a":1},"prop2":{"prop3":{"$jsan":"$.prop1"}}}'); | ||
assert.equal(jsan.stringify(obj, null, null, false), '{"prop1":{"a":1},"prop2":{"prop3":{"$jsan":"$.prop1"}}}'); | ||
assert.equal(jsan.stringify(obj, null, null, {refs: false}), '{"prop1":{"a":1},"prop2":{"prop3":{"a":1}}}'); | ||
}); | ||
it('works on objects with "[", "\'", and "]" in the keys', function() { | ||
@@ -232,2 +241,9 @@ var obj = {}; | ||
}); | ||
it('uses reviver function', function() { | ||
var str = '{"a":1,"b":null}'; | ||
assert.deepEqual({"a":1,"b":2}, | ||
jsan.parse(str, function (key, value) { return key === "b" ? 2 : value; }) | ||
); | ||
}); | ||
@@ -240,2 +256,8 @@ it('can decode dates', function() { | ||
it('can decode dates while using reviver', function() { | ||
var str = '{"$jsan":"d1400000000000"}'; | ||
var obj = jsan.parse(str, function (key, value) { return value; }); | ||
assert(obj instanceof Date); | ||
}); | ||
it('can decode regexes', function() { | ||
@@ -242,0 +264,0 @@ str = '{"$jsan":"r,test"}'; |
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
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
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
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
7
40464
19
869