package
Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`
npm install javascript-stringifyGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
70.2 kB
Includes dependencies.
Stringify is to
evalasJSON.stringifyis toJSON.parse.
npm install javascript-stringify --saveimport { stringify } from "javascript-stringify";The API is similar JSON.stringify:
value The value to convert to a stringreplacer A function that alters the behavior of the stringification processspace A string or number that's used to insert white space into the output for readability purposesoptions
undefined properties instead of restoring as undefinedstringify({}); // "{}"
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.
*/
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;
};MIT