JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2518806
  • Score
    100M100P100Q191656F
  • License MIT

Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`

Package Exports

  • javascript-stringify

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (javascript-stringify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

JavaScript Stringify

NPM version build status Test coverage Gittip

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]])

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.

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

/**
 * 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}"

/**
 * 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