stringify-x
v3.0.0
Published
Enhanced object stringifier for JavaScript
Downloads
4
Readme
stringifyX.js
Enhanced object stringifier for JavaScript
Features
stringify-x works similar to JSON.stringify with the following enhancements:
- Regex values are displayed in their string form
- undefined values are written out as the string "*undefined*"
- null values are written out as the string "*null*"
- Functions are displayed as their name suffixed by parentheses e.g., fn()
- Circular references can be handled up to a specified maximum depth
- Other primitive values are displayed as their string representation
API
stringify(obj, maxDepth); // stringifies obj up to optional maxDepth
Example
var stringify = require("stringify-x");
var _object = {
_boolean: false,
_string: "Hello",
_date: new Date(),
_number: 123,
_null: null,
_undefined: undefined,
_regexp: /abc[0-9]+/g,
_array: [1, 2, "three"],
_function: function hello() {},
_object: {
_boolean: true,
_string: "World",
_date: new Date(2011, 10, 30),
_number: -123.123456789,
_regexp: new RegExp('a-z'),
_array: ["a", "b", "c"],
_function: function() {},
_object: {
_boolean: true,
_string: "Hello World",
_date: new Date(2011, 10, 30),
_number: -123.123456789,
_regexp: new RegExp('a-z'),
_array: ["a", "b", "c"],
_function: function() {}
}
}
}
// use custom maxDepth of 1
console.log(stringify(_object, 1));
// use default maxDepth (10)
console.log(stringify(_object));
console.log("implicit undefined value: " + stringify());
console.log("undefined value: " + stringify(undefined));
console.log('"undefined" string: ' + stringify("undefined"));
console.log("null value: " + stringify(null));
console.log('"null" string: ' + stringify("null"));
Result:
{_boolean: false,_string: "Hello",_date: Mon Sep 12 2016 13:53:07 GMT-0700 (PDT),_number: 123,_null: *null*,_undefined: *undefined*,_regexp: /abc[0-9]+/g,_array: "1,2,three",_function: hello(),_object: "[object Object]"}
{_boolean: false,_string: "Hello",_date: Mon Sep 12 2016 13:53:07 GMT-0700 (PDT),_number: 123,_null: *null*,_undefined: *undefined*,_regexp: /abc[0-9]+/g,_array: [1,2,"three"],_function: hello(),_object: {_boolean: true,_string: "World",_date: Wed Nov 30 2011 00:00:00 GMT-0800 (PST),_number: -123.123456789,_regexp: /a-z/,_array: ["a","b","c"],_function: (),_object: {_boolean: true,_string: "Hello World",_date: Wed Nov 30 2011 00:00:00 GMT-0800 (PST),_number: -123.123456789,_regexp: /a-z/,_array: ["a","b","c"],_function: ()}}}
implicit undefined value: *undefined*
undefined value: *undefined*
"undefined" string: "undefined"
null value: *null*
"null" string: "null"
Install
npm install stringify-x --save