Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork15
Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`
License
NotificationsYou must be signed in to change notification settings
blakeembrey/javascript-stringify
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Stringify is to
evalasJSON.stringifyis toJSON.parse.
npm install javascript-stringify --saveimport{stringify}from"javascript-stringify";
The API is similarJSON.stringify:
valueThe value to convert to a stringreplacerA function that alters the behavior of the stringification processspaceA string or number that's used to insert white space into the output for readability purposesoptions- 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
undefinedproperties instead of restoring asundefined
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,newNumber(10),newDate()]);// "[/.+/gi,new Number(10),new Date(1406623295732)]"/** * Unknown or circular references are removed. */varobj={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(typeofvalue==="string"){return'"'+value.replace(/"/g,'\\"')+'"';}returnstringify(value);});//=> '["test","string"]'
You can use your own code formatter on the result ofjavascript-stringify. Here is an example usingeslint:
const{ CLIEngine}=require("eslint");const{ stringify}=require("javascript-stringify");const{APP_ROOT_PATH,ESLINTRC_FILE_PATH}=require("./constants");constESLINT_CLI=newCLIEngine({fix:true,cwd:APP_ROOT_PATH,configFile:ESLINTRC_FILE_PATH,});module.exports=(objectToStringify)=>{returnESLINT_CLI.executeOnText(stringify(objectToStringify)).results[0].output;};
MIT
About
Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Contributors10
Uh oh!
There was an error while loading.Please reload this page.