2.1.0 • Published 3 years ago

javascript-stringify v2.1.0

Weekly downloads
1,210,465
License
MIT
Repository
github
Last release
3 years ago

JavaScript Stringify

NPM version NPM downloads Build status Build coverage

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

Installation

npm install javascript-stringify --save

Usage

import { stringify } from "javascript-stringify";

The API is similar JSON.stringify:

  • value The value to convert to a string
  • replacer A function that alters the behavior of the stringification process
  • space A string or number that's used to insert white space into the output for readability purposes
  • options
    • 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

stringify({}); // "{}"
stringify(true); // "true"
stringify("foo"); // "'foo'"

stringify({ x: 5, y: 6 }); // "{x:5,y:6}"
stringify([1, 2, 3, "string"]); // "[1,2,3,'string']"

stringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}"

/**
 * Invalid key names are automatically stringified.
 */
stringify({ "some-key": 10 }); // "{'some-key':10}"

/**
 * Some object types and values can remain identical.
 */
stringify([/.+/gi, 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;

stringify(obj); // "{x:10}"
stringify(obj, null, null, { references: true }); // "(function(){var x={x:10};x.circular=x;return x;}())"

/**
 * Specify indentation - just like `JSON.stringify`.
 */
stringify({ a: 2 }, null, " "); // "{\n a: 2\n}"
stringify({ uno: 1, dos: 2 }, null, "\t"); // "{\n\tuno: 1,\n\tdos: 2\n}"

/**
 * Add custom replacer behaviour - like double quoted strings.
 */
stringify(["test", "string"], function (value, indent, stringify) {
  if (typeof value === "string") {
    return '"' + value.replace(/"/g, '\\"') + '"';
  }

  return stringify(value);
});
//=> '["test","string"]'

Formatting

You can use your own code formatter on the result of javascript-stringify. Here is an example using eslint:

const { CLIEngine } = require("eslint");
const { stringify } = require("javascript-stringify");

const { APP_ROOT_PATH, ESLINTRC_FILE_PATH } = require("./constants");

const ESLINT_CLI = new CLIEngine({
  fix: true,
  cwd: APP_ROOT_PATH,
  configFile: ESLINTRC_FILE_PATH,
});

module.exports = (objectToStringify) => {
  return ESLINT_CLI.executeOnText(stringify(objectToStringify)).results[0]
    .output;
};

License

MIT

@marketmuse/redux-devtools-inspectorcreate-pickvuedragdropuploadimagesredux-devtools-inspector-maintainedadaption-toolbb-chatkuma-cli@texttree/demo-bsa-reference-rcl@startdt/cli-ui@kontikijs/cli@fundefund/funde_ckeleventy-plugin-slinkityfdt-react-styleguidist@startdt/next-work@startdt/next-clikyle-cli-xhhzero-cli-uiwjy-cli@infinitebrahmanuniverse/nolb-javreact-native-style-maker-cli@everything-registry/sub-chunk-1950@314oner_npm/universal-components-libraryvite-plugin-oml2d@ygorluiz/integration@ygorconfig/integrationreact-icon-loadershowcode-cliduo-yamlsnapletstmagepackera-ckeditor5react-native-redux-devtoolsdex-cliraml2mzraml-javascript-generatorraml-php-generatorraml-typescript-generatorcreate-jslib@darwinia/util@darwishdev/dash-devquickchart-jsquickchart-js-standalonereact-styleguidistreact-styleguidist-extendedsan-cli-uirollup-plugin-envsredux-devtools-inspectorredux-devtools-inspector-check-type-fixredux-devtools-test-generatorredux-devtools-inspector2redux-devtools-coreredux-inspector-monitorremotedev-appremotedev-inspector-monitorremotedev-inspector-monitor-support-navigatiomresolver-demoember-cli-config-buildereleventy-plugin-jstlsolidity-bloom-filtercreate-vuereact-app@ddxjs/clidjq-next-clioz-repl@badu06/cli@axelhzf/react-styleguidist-tsbuild-react-11ty@bitionaire/react-styleguidist@berun/fluent@berun/webpack-chain@best-shot/webpack-chain@best-shot/inspectorpipe-documentationscli-ui-ttnpollen-csscode-challenge@ckeditor/ckeditor5-dev-utilspostcss-bem-to-js@chalbert/react-styleguidistcommand-functions@spreact/cli@southcn/ckeditor5-build-inlinewjd_cli@slash-copilot/jotai-devtoolsmacroifyma-magepackm2m-chartjs-plugin-crosshairmagepackmagepack-2magepack_bundleryaml-loaderprogressive-web-sdkcontentful-to-sanitycontainerinspection_2config-poolproj-creator-cli@creativestyle/magesuite-frontend-builderchint-front-clichenxi-click-wyck-wy-1
2.1.0

3 years ago

2.0.1

4 years ago

2.0.0

5 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago