un-eval
uneval
is a function that converts an Object to its source.
Usage
You may use uneval in node or browser like this:
```javascript
un
eval(yourObject)
```
Support types
The un
eval support following types
| Type | Support |
|----|----|
| null |
(Yes) |
| undefined |
(Yes) |
| number |
(Yes) |
| string |
(Yes) |
| boolean |
(Yes) |
| Number |
(Yes) |
| String |
(Yes) |
| Boolean |
(Yes) |
| Date |
(Yes) |
| RegExp |
(Yes) |
| function |
(Yes)1 |
| Array |
(Yes) |
| Object |
(Yes)23 |
Notes:
- ES5 style function only. Arrow functions, generator functions, and, native functions are not supported.
- All other object is treated as Object type.
({})
, or
will be generated if any circular found.
Examples
```javascript
un
eval(3); // '3'
uneval(Math.PI); // '3.141592653589793'
un
eval('hello'); // '"hello"'
uneval(Object(false)); // 'new Boolean(false)'
un
eval(function (x) { return x + 1; }); // '(function (x) { return x + 1; })'
uneval(/regexp/ig); // '/regexp/gi'
un
eval(new Date(978307200000)); // '(new Date(978307200000))'
uneval(
1,2,3); // '
1, 2, 3'
un
eval({x: 2, y: 3}); // '({"x":2, "y":3})'
var obj1 = {};
obj1.x = obj1.y = { value: 3 };
uneval(obj1); // '({"y":({"value":3}), "x":({"value":3})})'
var obj2 = {};
obj2.x = {};
obj2.y = {};
obj2.x.y = obj2.y;
obj2.y.x = obj2.x;
un
eval(obj2); // '({"x":({"y":({"x":({})})}), "y":({"x":({"y":({})})})})'
```
eval & uneval
un
eval is disgned to be similar to
uneval
but works cross browser.
uneval
is similar to
uneval
, but there are few difference:
uneval
currently do not support some types such as Error
.
uneval
will convert native function to "null"
.
uneval
will format codes differently from uneval
.
Warning
Although,
uneval
is designed to output source with no side effect. But there may be some bugs or limitations in the code. You should avoid using
uneval
any untrustable objects (maybe from user input) then
eval
it. NEVER use
eval
unless you know what will happen.
USE AS YOUR OWN RISK.
License
The MIT License