1 | # JavaScript Stringify
|
2 |
|
3 | [![NPM version][npm-image]][npm-url]
|
4 | [![NPM downloads][downloads-image]][downloads-url]
|
5 | [![Build status][build-image]][build-url]
|
6 | [![Build coverage][coverage-image]][coverage-url]
|
7 |
|
8 | > Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`.
|
9 |
|
10 | ## Installation
|
11 |
|
12 | ```
|
13 | npm install javascript-stringify --save
|
14 | ```
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ```javascript
|
19 | import { stringify } from "javascript-stringify";
|
20 | ```
|
21 |
|
22 | The API is similar `JSON.stringify`:
|
23 |
|
24 | - `value` The value to convert to a string
|
25 | - `replacer` A function that alters the behavior of the stringification process
|
26 | - `space` A string or number that's used to insert white space into the output for readability purposes
|
27 | - `options`
|
28 | - **maxDepth** _(number, default: 100)_ The maximum depth of values to stringify
|
29 | - **maxValues** _(number, default: 100000)_ The maximum number of values to stringify
|
30 | - **references** _(boolean, default: false)_ Restore circular/repeated references in the object (uses IIFE)
|
31 | - **skipUndefinedProperties** _(boolean, default: false)_ Omits `undefined` properties instead of restoring as `undefined`
|
32 |
|
33 | ### Examples
|
34 |
|
35 | ```javascript
|
36 | stringify({}); // "{}"
|
37 | stringify(true); // "true"
|
38 | stringify("foo"); // "'foo'"
|
39 |
|
40 | stringify({ x: 5, y: 6 }); // "{x:5,y:6}"
|
41 | stringify([1, 2, 3, "string"]); // "[1,2,3,'string']"
|
42 |
|
43 | stringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}"
|
44 |
|
45 | /**
|
46 | * Invalid key names are automatically stringified.
|
47 | */
|
48 | stringify({ "some-key": 10 }); // "{'some-key':10}"
|
49 |
|
50 | /**
|
51 | * Some object types and values can remain identical.
|
52 | */
|
53 | stringify([/.+/gi, new Number(10), new Date()]); // "[/.+/gi,new Number(10),new Date(1406623295732)]"
|
54 |
|
55 | /**
|
56 | * Unknown or circular references are removed.
|
57 | */
|
58 | var obj = { x: 10 };
|
59 | obj.circular = obj;
|
60 |
|
61 | stringify(obj); // "{x:10}"
|
62 | stringify(obj, null, null, { references: true }); // "(function(){var x={x:10};x.circular=x;return x;}())"
|
63 |
|
64 | /**
|
65 | * Specify indentation - just like `JSON.stringify`.
|
66 | */
|
67 | stringify({ a: 2 }, null, " "); // "{\n a: 2\n}"
|
68 | stringify({ uno: 1, dos: 2 }, null, "\t"); // "{\n\tuno: 1,\n\tdos: 2\n}"
|
69 |
|
70 | /**
|
71 | * Add custom replacer behaviour - like double quoted strings.
|
72 | */
|
73 | stringify(["test", "string"], function (value, indent, stringify) {
|
74 | if (typeof value === "string") {
|
75 | return '"' + value.replace(/"/g, '\\"') + '"';
|
76 | }
|
77 |
|
78 | return stringify(value);
|
79 | });
|
80 | //=> '["test","string"]'
|
81 | ```
|
82 |
|
83 | ## Formatting
|
84 |
|
85 | You can use your own code formatter on the result of `javascript-stringify`. Here is an example using [eslint](https://www.npmjs.com/package/eslint):
|
86 |
|
87 | ```javascript
|
88 | const { CLIEngine } = require("eslint");
|
89 | const { stringify } = require("javascript-stringify");
|
90 |
|
91 | const { APP_ROOT_PATH, ESLINTRC_FILE_PATH } = require("./constants");
|
92 |
|
93 | const ESLINT_CLI = new CLIEngine({
|
94 | fix: true,
|
95 | cwd: APP_ROOT_PATH,
|
96 | configFile: ESLINTRC_FILE_PATH,
|
97 | });
|
98 |
|
99 | module.exports = (objectToStringify) => {
|
100 | return ESLINT_CLI.executeOnText(stringify(objectToStringify)).results[0]
|
101 | .output;
|
102 | };
|
103 | ```
|
104 |
|
105 | ## License
|
106 |
|
107 | MIT
|
108 |
|
109 | [npm-image]: https://img.shields.io/npm/v/javascript-stringify
|
110 | [npm-url]: https://npmjs.org/package/javascript-stringify
|
111 | [downloads-image]: https://img.shields.io/npm/dm/javascript-stringify
|
112 | [downloads-url]: https://npmjs.org/package/javascript-stringify
|
113 | [build-image]: https://img.shields.io/github/workflow/status/blakeembrey/javascript-stringify/CI/main
|
114 | [build-url]: https://github.com/blakeembrey/javascript-stringify/actions/workflows/ci.yml?query=branch%3Amain
|
115 | [coverage-image]: https://img.shields.io/codecov/c/gh/blakeembrey/javascript-stringify
|
116 | [coverage-url]: https://codecov.io/gh/blakeembrey/javascript-stringify
|