code-stringify
code-stringify
is the node.js module that converts JavaScript variables into source codes with indents and styles.
Unlike JSON.stringify
, code-stringify
also deals with reference(object) types of variables, and it converts JavaScript variables into strings of codes, not JSON.
Supports:
- Primative variables
- Regular expressions
- Functions
- Arrays
- Your custom formatter
Installation
npm i code-stringify
Usage
const fs = const stringify = const obj = '0': 1 {return n;} 'b': 1 'c-d': 3 // So you can use code-stringify to save your javascript variables into a file:fs
Then 'output.js' will look like:
moduleexports = 0: 1 {return n;} b: 1 'c-d': 3
stringify(subject, replacer, space, indent): string
any
subject The subject to be stringified
Function(key, value) | Array
replacer The replacer
argument acts just like the second parameter of JSON.stringify
.
A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string.
// '{a:1}'
number | string
space The space
argument acts just like the third parameter of JSON.stringify
.
number | string
indent Defaults to 0
The code indent for the entire subject. If indent === 4
, then the content of the output.js
in the first example will be:
moduleexports = 0: 1 {return n;} b: 1 'c-d': 3
new stringify.Code(string)
We could use new code.Code(code_string)
to define an already-stringified property.
So, see the example below:
const output = `module.exports = `
And the output.js will be:
module.exports = {
a: 1,
'foo-bar': 2,
foo: (function(a){return a})(3)
}
stringify.STRINGIFY_SYMBOL
// `stringify.STRINGIFY_SYMBOL` equals tostringifySTRINGIFY_SYMBOL = Symbol
If an object[stringify.STRINGIFY_SYMBOL]
is a function, then the function will be used as the stringifier of the object.
const monkey = iam: { return '"monkey king"' } console// {iam:"monkey king"}
JSON.stringify()
Versus JSON.stringify
makes JSON.code-stringify
makes JavaScript code.
Advanced Section
const Stringifier STRINGIFY_SYMBOL CODE_STRINGIFY_CUSTOM } =
new Stringifier(options)
new in 2.0.0
The constructor Stringifier
allows us to take more control of the stringifer.
- options
Object
- replacer?
(Function | Array)=null
- space?
(number | string)=0
Defaults to0
which indicates there should be no spaces.
- quote?
' | "
the quote character for strings. Defaults to'
. - useNumberKey?
boolean=true
uses number key of an object if possible
- replacer?
options.useNumberKey
// {1:1,'2b':2} useNumberKey: false// {'1':1:'2b':2}
stringifier.stringify(subject, indent = 0): string
- indent?
(number | string)=0
Returns the JavaScript code string.
stringifier.register(customStringifier): this
- customStringifier
CustomStringifier
Register a custom stringifier for certain data type.
{ this_name = name } { return `[king ]` } // {dinasaur:'Godzilla',ape:'[king Kong]'}
CODE_STRINGIFY_CUSTOM
CODE_STRINGIFY_CUSTOM
is a built-in CustomStringifier
to support stringify.STRINGIFY_SYMBOL
.
And a new Stringifier
is not registered CODE_STRINGIFY_CUSTOM
by default.
Utility methods
The following methods has no type checking and fault tolerance
Make sure every argument that passed into the methods has been type-checked
stringifier.string(string)
- string
string
Stringify a string
stringifier.object(object, indent)
- object
Object
Stringify a string
stringifier.array(array, indent)
- array
Array
Stringify an array
stringifier.key(key)
- string
key
Stringify a property of an object.
Known Issues
space
parameter could not affect the code indent inside functions.- Could not deal with variable scope so far.
Those issues or tasks which should be done to enhance the module might be fixed in the future. Or there will be a million thanks if you fork and contribute ~~