Package Exports
- uneval.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (uneval.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
uneval.js
Convert an object to its source code (With circular references too!)
Usage
You can both import the package like this...
const uneval = require("uneval.js");
...and like this
const { uneval } = require("uneval.js");
Simply pass the function as an argument to obtain the source code and eval it to obtain the object again.
You additionally can give some options to personalize the output
const a = {};
a.b = a.c = { a, url: /^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/ };
a.e = a;
console.log(uneval(a, { tab: " " }));
And the output will be
(x => (
x[2] = {
c: x[1] = {
url: /^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/
},
b: x[1]
},
x[1].a = x[2],
x[2].e = x[2]
))({})
Note that the complexity of the output depends on the complexity of the input
console.log(uneval([1, { a: "hi" }], { pretty: false })) // [1,{a:"hi"}]
Options
Additionally to the object to stringify you can pass an option object to personalize your output. The available options are
pretty
- Setting it to
false
deactivatesspace
,endl
andtab
- It defaults to
true
- Setting it to
space
- Set the string that will replace the spaces in the output
- Setting it to
false
is like setting it to""
- It defaults to
" "
endl
- Set the string that will replace the new lines in the output
- Setting it to
false
is like setting it to""
- It defaults to
"\n"
tab
- Set the string that will replace the tabs in the output
- Setting it to
false
is like setting it to""
- It defaults to
"\t"
proto
- Saves the class of objects (Including the
__proto__
property) - It defaults to
true
- Saves the class of objects (Including the
safe
- Wraps object literals in brackets to not confuse them with blocks
- It defaults to
true
func
- Put the top object in a function that defines the cache variable
- It defaults to
true
val
- The name of the variable which will cache the repeated references
- It defaults to
"x"
Supported
- All the things supported by json
- Multiple references (Even in Symbol keys)
- Circular references (Are much worse, trust me)
undefined
NaN
,Infinity
,-Infinity
- Symbols
- Symbol keys
- Dates
- Regular Expressions
- Functions
- "Objectified primitives", like
new String("hello")
- Big Integers
- Custom types
Coming Soon (Hopefully) in order of probability
- Maps and Sets
- Arrays and Functions custom fields
- Non enumerable properties
- Getters and Setters
- New syntax for object's methods, like
{ func() {} }
Known Problems
- The serialized prototypes will not be the same instance as the class' default prototype
const temp = eval(uneval(new class a { })); console.log( temp, temp instanceof (temp.constructor), temp.__proto__ === temp.__proto__.constructor.prototype ); // {} false false
- If an object of special type (Such as
String
,Date
, ...) contains the first reference to an other object, that object will become undefined everywhereconst a = new Date(); a.b = {}; console.log(eval(uneval({ a, b: a.b }))); // { a: 2021-08-16T02:57:10.125Z, b: undefined }