You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
toSource is a super simple function that converts JavaScript objects back to source code.
Introduction
Motivation: JSON doesn't support serializing functions, dates, or regular expressions. I wanted
a quick and simple way to push trusted data structures with code from Node down to the browser.
This should make it easier to share code and modules between the server and client.
Installation
npm install tosource
Examples
The following code:
importtoSourcefrom'tosource';console.log(toSource([4,5,6,'hello',{a: 2,b: 3,'1': 4,if: 5,yes: true,no: false,nan: NaN,infinity: Infinity,undefined: undefined,null: null,foo: function(bar){console.log('woo! a is '+a);console.log('and bar is '+bar);},},/we$/gi,newDate('Wed, 09 Aug 1995 00:00:00 GMT'),]),);
Output:
[ 4,
5,
6,
"hello",
{ 1:4,
a:2,
b:3,
"if":5,
yes:true,
no:false,
nan:NaN,
infinity:Infinity,
"undefined":undefined,
"null":null,
foo:function (bar) {
console.log('woo! a is ' + a);
console.log('and bar is ' + bar);
} },
/we$/gi,
new Date(807926400000) ]