This is fork of stringify-object
Stringify object with code chunks insertion
Install
$ yarn add smart-stringify-object
Usage
var obj = foo: 'bar' 'arr': 1 2 3 nested: hello: "world" 'function () { return this.foo; }'; var pretty = ; console;/*{ foo: "bar", arr: [ 1, 2, 3 ], nested: { hello: "world" }, method: function () { return this.foo; }}*/
Stringify functions
You can choose how exactly will be stringified object's properties of function type.
By default, the functions will be stringified as they were described in a code but you can use the functions as a tool to generate javascript code. There are two variants to use the functions in such a way:
Exec
Option: `function: "exec"
Execute a function and insert the result as a code chunk.
const obj = name: path `require()` /*{ name: 'example.js', module: require("path/to/example.js")}*/
Extract
Option: function: "extract"
Get function's body and insert it as code chunk.
var obj = { Symbol } var pretty =
API
stringifyObject(input, [options])
Circular references will be replaced with "[Circular]"
.
input
Required
Type: object
, array
options
indent
Type: string
Default: '\t'
Choose the indentation you prefer.
singleQuotes
Type: boolean
Default: true
Set to false to get double-quoted strings.
filter(obj, prop)
Type: function
Expected to return a boolean of whether to keep the object.
inlineCharacterLimit
Type: number
Default: undefined
When set, will inline values up to inlineCharacterLimit
length for the sake
of more terse output.
For example, given the example at the top of the README:
var obj = foo: 'bar' 'arr': 1 2 3 nested: hello: "world" ; var pretty = ; console;/*{ foo: "bar", arr: [1, 2, 3], nested: { hello: "world" }}*/
As you can see, arr
was printed as a one-liner because its string was shorter
than 12 characters.
functions
Type: "extract" | "exec" | false
Default: false
Choose method to stringify functions
- extract Extract function body as raw code
- exec Execute function and use its result as raw code
Exctract example:
var obj = { Symbol } var pretty =
Exec example:
var moduleName = "path/to/module.js"var obj = `require()` var pretty =
License
BSD license © Vladimir Kalmykov, Yeoman Team