3 Star 1 Fork 4

VTJ.PRO/node_modules

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

JavaScript Stringify

NPM version NPM downloads Build status Test coverage

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(value) with the new value.

The options object allows some additional configuration:

  • maxDepth (number, default: 100) The maximum depth of values to stringify
  • maxValues (number, default: 100000) The maximum number of values to stringify
  • references (boolean, default: false) Restore circular/repeated references in the object (uses IIFE)
  • skipUndefinedProperties (boolean, default: false) Omits undefined properties instead of restoring as undefined

Examples

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"]'

License

MIT

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/newgateway/node_modules.git
git@gitee.com:newgateway/node_modules.git
newgateway
node_modules
node_modules
master

搜索帮助