js-function-reflector
Function Reflection in Javascript With Support for ES2015+ Syntax
Table of Contents generated with DocToc
Installation
npm install js-function-reflector
Usage
const functionReflector = ;const parsedFunction = ;
Examples
Function with simple parameter
{ return a + b;};const output = ;/* output = { type: "TRADITIONAL", name: "add", _rawParameter: "a, b", body: "return a + b;", async: false, params: [ { type: "SIMPLE", name: "a" }, { type: "SIMPLE", name: "b" } ]} */
Arrow function
const arrowFn = { return a + b;}let output = ;/* output = { type: "ARROW", name: null, _rawParameter: "a, b", body: "return a + b;", async: false, params: [ { type: "SIMPLE", name: "a" }, { type: "SIMPLE", name: "b" } ]} */ // inline arrow function automatically added return statementconst arrowWithoutParenthesisAndCurlyBrace = 'hello ' + nameoutput = ;/* output = { type: "ARROW", name: null, _rawParameter: "name", body: "return 'hello ' + name", async: false, params: [ { type: "SIMPLE", name: "name" } ]} */
Async function
const sleep = { return resolve }const output = )/* output = { type: "TRADITIONAL", name: null, _rawParameter: "time", body: "return new Promise(resolve, setTimeout(resolve, time))", async: true, params: [ { type: "SIMPLE", name: "time" } ]}
Generator function
const generatorFn = { for var item of list item }const output = )/* output = { type: "GENERATOR", name: null, _rawParameter: "list", body: "for (var item of list) {\r\n yield item\r\n }", async: false, params: [ { type: "SIMPLE", name: "list" } ]} */
Function with default value parameter
const pow = { return Math;}const output = params/* output = [ { type: "SIMPLE", name: "n" }, { type: "DEFAULT", name: "power", value: 2 }] */
Function with rest parameter
const dummyFn = cconst output = params/* output = [ { type: "SIMPLE", name: "a" }, { type: "DEFAULT", name: "b", value: 5 }, { type: "REST", name: "c" }] */
Function with destructuring parameter
const destructuringFn = {}const output = params/* output = [ { type: "SIMPLE", name: "a" }, { type: "DESTRUCTURING", value: { type: "object", keys: [ { type: "DESTRUCTURING", name: "names", value: { type: "object", keys: [ { type: "KEY", name: "firstNames" }, { type: "KEY", name: "lastNames" } ] } }, { type: "DESTRUCTURING", name: "locations", value: { type: "array", keys: [ { type: "DESTRUCTURING", value: { type: "array", keys: [ { type: "KEY", name: "country" }, { type: "KEY", name: "city" } ] } }, { type: "REST", name: "restLocations" } ] } }, { type: "REST", name: "rest" } ] } }] */
Function with variable as default value
When parameter's default value contains a variable, we need to pass that variable on the second parameter
const a = number: 1const b = 2 const dummyFn = {}const output = params/* output = [ { type: "DEFAULT", name: "x", value: 1 }, { type: "DEFAULT", name: "y", value: 2 }] */