JavaScript Stringify

Stringify is to eval as JSON.stringify is to JSON.parse.
Installation
npm install javascript-stringify --save
bower install javascript-stringify --save
Node
var javascriptStringify = require('javascript-stringify');
AMD
define(function (require, exports, module) {
var javascriptStringify = require('javascript-stringify');
});
<script> tag
<script src="javascript-stringify.js"></script>
Usage
javascriptStringify(value[, replacer [, space [, options]]])
The API is similar to JSON.stringify. However, any value returned by the replacer will be used literally. For this reason, the replacer is passed three arguments - value, indentation and stringify. If you need to continue the stringification process inside your replacer, you can call stringify with the updated value.
The options object allows some additional configuration:
- maxDepth The maximum depth to stringify to
Examples
javascriptStringify({});
javascriptStringify(true);
javascriptStringify('foo');
javascriptStringify({ x: 5, y: 6});
javascriptStringify([1, 2, 3, 'string']);
javascriptStringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 });
javascriptStringify({ 'some-key': 10 });
javascriptStringify([/.+/ig, new Number(10), new Date()]);
var obj = { x: 10 };
obj.circular = obj;
javascriptStringify(obj);
javascriptStringify({ a: 2 }, null, ' ');
javascriptStringify({ uno: 1, dos : 2 }, null, '\t');
javascriptStringify(['test', 'string'], function (value, indent, stringify) {
if (typeof value === 'string') {
return '"' + value.replace(/"/g, '\\"') + '"';
}
return stringify(value);
});
License
MIT