un-eval
un_eval
is a function that converts an Object to its source.
Usage
You may use un_eval in node or browser like this:
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)[2][3] |
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
; // '3'; // '3.141592653589793'; // '"hello"'; // 'new Boolean(false)'; // '(function (x) { return x + 1; })'; // '/regexp/gi'; // '(new Date(978307200000))'; // '[1, 2, 3]'; // '({"x":2, "y":3})' var obj1 = {};obj1x = obj1y = value: 3 ;; // '({"y":({"value":3}), "x":({"value":3})})' var obj2 = {};obj2x = {};obj2y = {};obj2xy = obj2y;obj2yx = obj2x;; // '({"x":({"y":({"x":({})})}), "y":({"x":({"y":({})})})})'
eval & uneval
un_eval
is disgned to be similar to uneval
but works cross browser.
un_eval
is similar to uneval
, but there are few difference:
un_eval
currently do not support some types such asError
.un_eval
will convert native function to"null"
.un_eval
will format codes differently fromuneval
.
Warning
Although, un_eval
is designed to output source with no side effect. But there may be some bugs or limitations in the code. You should avoid using un_eval
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