Skip to content

Instantly share code, notes, and snippets.

@jescalan
Created August 11, 2016 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jescalan/6f9f3c85858ef6e48e38b941f6d579bf to your computer and use it in GitHub Desktop.
Save jescalan/6f9f3c85858ef6e48e38b941f6d579bf to your computer and use it in GitHub Desktop.
serialize an object, with functions, ready to be required
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