js-serialize
v0.5.3
Published
Like `JSON.stringify`, but supports more types.
Downloads
2
Readme
js-serialize
Like JSON.stringify
, but supports more types.
Installation
yarn add js-serialize
npm i js-serialize -S
Examples
> jsSerialize = require('js-serialize')
{ [Function: jsSerialize] raw: [Function: raw] }
> jsSerialize('foo')
'"foo"'
> jsSerialize(1)
'1'
> jsSerialize([2,3])
'[2,3]'
> jsSerialize(new Set([4,5]))
'new Set([4,5])'
> jsSerialize(new Map([["a",1],["b",2]]))
'new Map([["a",1],["b",2]])'
> jsSerialize(Symbol.for('bar'))
'Symbol.for("bar")'
> jsSerialize([true,false,null,undefined])
'[true,false,null,undefined]'
> jsSerialize(new Array(4))
'new Array(4)'
> jsSerialize([undefined,undefined])
'[undefined,undefined]'
> jsSerialize([,,5,,])
'[,,5,,]'
> jsSerialize(isNaN)
'isNaN'
> jsSerialize(Math.sin)
'Math.sin'
> jsSerialize({a:1,b:{2:3}})
'{a:1,b:{"2":3}}'
> jsSerialize({a:1,b:jsSerialize.raw('() => { console.log("arbitrary"); }')})
'{a:1,b:() => { console.log("arbitrary"); }}'
> jsSerialize(function quux(){})
'function quux(){}'
> jsSerialize(x => x*2)
'x => x*2'
> jsSerialize(() => '</script>')
'() => \'<\\/script>\''
> s=new Set
Set(0) {}
> s.add(s)
<ref *1> Set(1) { [Circular *1] }
> jsSerialize(s)
'($0=>($0=new Set,$0.add($0)))()'
> jsSerialize(["hello world", "hello long string", "hello world", "hello long string"])
'($0=>["hello world",$0="hello long string","hello world",$0])()'