Skip to content

Instantly share code, notes, and snippets.

@stevekane
Created October 27, 2016 15:20
Show Gist options
  • Save stevekane/f9044834ce6740a3e755fdc7a34389f4 to your computer and use it in GitHub Desktop.
Save stevekane/f9044834ce6740a3e755fdc7a34389f4 to your computer and use it in GitHub Desktop.
JSON copy w/ function objects
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