1.1.0 • Published 7 years ago
putil-stringify v1.1.0
putil-stringify
Alternate to native JSON.stringify. It offers more control in replacer callback, etc formatting dates with custom format. And also replacer callback can have 3 arguments which gives current object being serialized.
const a = {a: 1, b: '2', c: {d: new Date()}};
let s = stringify(a, (k, v) => {
if (v instanceof Date)
return 'today';
return v;
});
console.log(s);
{"a":1,"b":"2","c":{"d":"today"}}
const a = {a: {num: 5}};
const b = {a: a, b: 'Hello'};
let s = stringify(b, (o, k, v) => {
if (o === a.a && k === 'num')
return v+1;
return v;
});
console.log(s);
{"a":{"a":{"num":6}},"b":"Hello"}
Installation
$ npm install putil-stringify --save
Node Compatibility
node
>= 6.x
;