You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.5 KiB

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