Skip to content

Instantly share code, notes, and snippets.

@MattiasFestin
Last active January 7, 2020 13:55
Show Gist options
  • Save MattiasFestin/e455bd80c68db6a4da51 to your computer and use it in GitHub Desktop.
Save MattiasFestin/e455bd80c68db6a4da51 to your computer and use it in GitHub Desktop.
uneval "polyfill"
let uneval = (o, noNativeFns = true) => {
var retVal = '';
if (typeof o === 'object') {
if (Array.isArray(o)) {
retVal = '[' + o.map((el) => uneval(el)).join(',') + ']';
} else if (o instanceof RegExp) {
retVal = o.toString();
} else if (o instanceof Date) {
retVal = `new Date(${o})`;
} else if (o === null) {
retVal = 'null';
} else if (Number.isNaN(o)) {
retVal = 'NaN';
}else {
//[TODO] add support for defineProperty & getters & setters
retVal = '{' + Object.keys(o).map((k) => `"${k}":${uneval(o[k])}`).join(',') + '}';
}
} else if (typeof o === 'function') {
let isNative: boolean = o.toString().match(/^function \(\) \{ \[native code\] \}$/);
if (isNative && noNativeFns) {
throw new Error('No native functions is allowed');
}
retVal = `(${isNative ? o.name : o.toString()})`;
} else {
retVal = JSON.stringify(o);
}
return retVal;
};
@tiansh
Copy link

tiansh commented Dec 20, 2016

By using eval(uneval(some_value)) to test this uneval function, it will fail on following values:

  1. (void 0)
  2. -0
  3. Infinity, -Infinity
  4. NaN
  5. [, ,]
  6. new Date(1)
  7. (function () { var x = {}; x.x = x; return x }())
  8. Object(1), Object(''), Object(false)
  9. (function () { p = function () {}; p.toString = function () { return 'alert(/xss/)' }; return p; }())
  10. ({})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment