Comparing version 2.0.3 to 2.0.4
@@ -59,5 +59,7 @@ var pathGetter = require('./path-getter'); | ||
if (value instanceof Date) { | ||
return {$ref: {$date: value}}; | ||
} | ||
if (typeof value === 'object' && value !== null && | ||
!(value instanceof Boolean) && | ||
!(value instanceof Date) && | ||
!(value instanceof Number) && | ||
@@ -140,2 +142,4 @@ !(value instanceof RegExp) && | ||
value[i] = pathGetter($, path); | ||
} else if (typeof path === 'object' && path.$date) { | ||
value[name] = new Date(path.$date); | ||
} else { | ||
@@ -153,5 +157,7 @@ rez(item); | ||
if (typeof path === 'string') { | ||
value[name] = pathGetter($, path); | ||
value[name] = pathGetter($, path); | ||
} else if (typeof path === 'object' && path.$date) { | ||
value[name] = new Date(path.$date); | ||
} else { | ||
rez(item); | ||
rez(item); | ||
} | ||
@@ -158,0 +164,0 @@ } |
{ | ||
"name": "jsan", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "handle circular references when stringifying and parsing", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ jsan | ||
Easily stringify and parse any object including objects with circular references, using the familar `parse` and `stringify` methods. | ||
Easily stringify and parse any object including objects with circular references or dates, using the familar `parse` and `stringify` methods. | ||
@@ -21,7 +21,9 @@ ### Usage | ||
obj['sub']['subSelf'] = obj['sub']; | ||
obj.now = new Date(); | ||
var str = jsan.stringify(obj); | ||
str === '{"self":{"$ref":"$"},"sub":{"subSelf":{"$ref":"[\\"sub\\"]"}}}'; // true | ||
str === '{"self":{"$ref":"$"},"sub":{"subSelf":{"$ref":"[\\"sub\\"]"}},"now":{"$ref":{"$date":"2015-03-24T15:08:00.000Z"}}}'; // true | ||
var obj2 = jsan.parse(str); | ||
obj2 === obj2['self']; // true | ||
obj2['sub']['subSelf'] === obj2['sub']; // true | ||
obj2.now instanceof Date; // true | ||
``` | ||
@@ -33,3 +35,3 @@ | ||
This ulitilty has been heavily optimized and performs as well as the native `JSON.parse` and `JSON.stringify`. It doesn't by default handle self references (non-circular) when stringifing but you can force it to check for that by passing in `true` as a forth arg: | ||
This ulitilty has been heavily optimized and performs as well as the native `JSON.parse` and `JSON.stringify`. It doesn't by default handle self references (non-circular), or dates when stringifing but you can force it to by passing in `true` as a forth arg: | ||
@@ -43,2 +45,6 @@ ```js | ||
var str2 = jsan.stringify(obj, null, null, true) // '{"a":{},"b":{"$ref":"[\\"a\\"]"}}' | ||
obj = {now: new Date()}; | ||
var str1 = jsan.stringify(obj) // '{"now":"2015-03-24T15:13:23.291Z"}' | ||
var str2 = jsan.stringify(obj, null, null, true) // '{"now":{"$ref":{"$date":"2015-03-24T15:13:23.291Z"}}}' | ||
``` |
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
13456
12
311
47