UNPKG

@cran/gql.core

Version:

Cran/GraphQL Core Utilities

20 lines (19 loc) 600 B
const RE_FUNC = /^(?:\(([^)]*?)\))?\s*=>\s*(.*?)\s*$/u; const RE_BODY = /^(?:\{(.*)\}|(.*?))$/u; export class Executable extends Function { name; constructor(name, body) { super(...getParams(body)); Object.defineProperty(this, "name", { get() { return name; }, }); } } function getParams(body) { const matchFunc = RE_FUNC.exec(body); if (!matchFunc) { return ["", `return(${JSON.stringify(body)})`,]; } const matchBody = RE_BODY.exec(matchFunc[2]); return [matchFunc[1] || "", matchBody[1] || `return(${matchBody[2]})`,]; }