erratic-gate/src/modules/activation/helpers/toFunction.ts
Matei Adriel fccc1922fb loading
2019-07-23 22:53:59 +03:00

16 lines
370 B
TypeScript

/**
* Transforms js code into a function
*
* @param source tThe js code
* @param args The name of arguments to pass to the function
*/
export const toFunction = <T extends unknown[]>(
source: string,
...args: string[]
): ((...args: T) => void) => {
const raw = `return (${args.join(',')}) => {
${source}
}`
return new Function(raw)()
}