Created
October 27, 2016 15:20
-
-
Save stevekane/f9044834ce6740a3e755fdc7a34389f4 to your computer and use it in GitHub Desktop.
JSON copy w/ function objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HDR = '#SOURCE' | |
const compile = (f) => { try { return eval(f) } catch (e) { return new Error(f) } } | |
const ser = (_, f) => f instanceof Error ? HDR + f.message : f instanceof Function ? HDR + f.toString() : f | |
const des = (_, f) => f.indexOf && f.indexOf(0, HDR) ? compile(f.slice(HDR.length)) : f | |
var o = { | |
goodFn: () => 5, | |
badFn: new Error('() =>< 6') | |
} | |
var o2 = JSON.parse(JSON.stringify(o, ser), des) | |
console.log(o2) | |
console.log(o2.goodFn()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment