How to get a functions's body as string?
If you're going to do something ugly, do it with regex:
A.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1];
Don't use a regexp.
const getBody = (string) => string.substring(
string.indexOf("{") + 1,
string.lastIndexOf("}")
)
const f = () => { return 'yo' }
const g = function (some, params) { return 'hi' }
const h = () => "boom"
console.log(getBody(f.toString()))
console.log(getBody(g.toString()))
console.log(getBody(h.toString())) // fail !
You could just stringify the function and extract the body by removing everything else:
A.toString().replace(/^function\s*\S+\s*\([^)]*\)\s*\{|\}$/g, "");
However, there is no good reason to do that and toString
actually doesn't work in all environments.