Bolshakova Anna ab21a11190 lab | 2 tuần trước cách đây | |
---|---|---|
.. | ||
LICENSE | 2 tuần trước cách đây | |
README.md | 2 tuần trước cách đây | |
javascript-stringify.d.ts | 2 tuần trước cách đây | |
javascript-stringify.js | 2 tuần trước cách đây | |
package.json | 2 tuần trước cách đây |
Stringify is to
eval
asJSON.stringify
is toJSON.parse
.
npm install javascript-stringify --save
bower install javascript-stringify --save
var javascriptStringify = require('javascript-stringify');
define(function (require, exports, module) {
var javascriptStringify = require('javascript-stringify');
});
<script>
tag<script src="javascript-stringify.js"></script>
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(value)
with the new value.
The options
object allows some additional configuration:
undefined
properties instead of restoring as undefined
javascriptStringify({}); // "{}"
javascriptStringify(true); // "true"
javascriptStringify('foo'); // "'foo'"
javascriptStringify({ x: 5, y: 6}); // "{x:5,y:6}"
javascriptStringify([1, 2, 3, 'string']); // "[1,2,3,'string']"
javascriptStringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}"
/**
* Invalid key names are automatically stringified.
*/
javascriptStringify({ 'some-key': 10 }); // "{'some-key':10}"
/**
* Some object types and values can remain identical.
*/
javascriptStringify([/.+/ig, new Number(10), new Date()]); // "[/.+/gi,new Number(10),new Date(1406623295732)]"
/**
* Unknown or circular references are removed.
*/
var obj = { x: 10 };
obj.circular = obj;
javascriptStringify(obj); // "{x:10}"
javascriptStringify(obj, null, null, { references: true }); // "(function(){var x={x:10};x.circular=x;return x;}())"
/**
* Specify indentation - just like `JSON.stringify`.
*/
javascriptStringify({ a: 2 }, null, ' '); // "{\n a: 2\n}"
javascriptStringify({ uno: 1, dos : 2 }, null, '\t'); // "{\n\tuno: 1,\n\tdos: 2\n}"
/**
* Add custom replacer behaviour - like double quoted strings.
*/
javascriptStringify(['test', 'string'], function (value, indent, stringify) {
if (typeof value === 'string') {
return '"' + value.replace(/"/g, '\\"') + '"';
}
return stringify(value);
});
//=> '["test","string"]'
MIT