Created
August 11, 2016 22:08
-
-
Save jescalan/6f9f3c85858ef6e48e38b941f6d579bf to your computer and use it in GitHub Desktop.
serialize an object, with functions, ready to be required
This file contains hidden or 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
function serializeVerbatim (obj) { | |
let i = 0 | |
const fns = [] | |
let res = JSON.stringify(obj, (k, v) => { | |
if (typeof v === 'function') { | |
fns.push(v.toString()) | |
return `__REPLACE${i++}` | |
} else { | |
return v | |
} | |
}) | |
res = res.replace(/"__REPLACE(\d{1})"/g, (m, v) => { | |
return fns[v] | |
}) | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment