Get parameter names from a given function reference, and optionally cache the results.
ES6+
- Typical parameters
- Array destructuring
- Object destructuring
- Comments
- Default args
- Cache/Memoization
- Wrapping results with argument values
- Parameters are ordered by their appearance in the function definition
It's recommended that you run the factory only once, because it initiates a cache - so initialise it in it's own module and export it:
const factory = require("mr-params").default;
const getParams = factory();
exports = getParams;
const getParams = require("./my-module");
getParams((a) => undefined); // => ["a"]
const factory = require("mr-params").default;
const getParams = factory({
cache: true, // default.
debug: false, // default. Used internally. Not useful, for now.
cacheFactory: nativeCacheFactory // default. Optionally replace the caching mechanism
});
You can wrap the results with their related values - typically you'd use an args array:
getParams((a, b, c) => undefined, [1, 2, 3]); // => {a: 1, b: 2, c: 3}
// with: args = ["foo", "bar", "baz"]
getParams((a, b, c) => undefined, args); // don't spread args. => {a: "foo", b: "bar", c: "baz"}
-
@babel/parse
-
@babel/traverse
-
farmhash
On first usage for a function reference, it will build a Babel AST for the entire function. All tokens are ignored, except parameters that are in scope (of the function body). The results are cached, avoiding the expensive process of rebuilding the AST.
- toString() the function and hash it
- Check the cache, using the hash as the key, return result on hit, continue on miss
- Build an AST for the entire function, and extract the parameters
- Store the result in the cache
- Return the result
You can replace the cache with your own implementation:
const getParams = factory({ cacheFactory: myCacheFactory });
The cache factory must return a cache object with the following (TS) interface:
interface ICacheOps {
get: (key: string) => string[] | false | undefined; // undefined on cache miss.
put: (key: string, val: string[] | false) => void; // an array of strings, or false
}
This interface is also exported from the main module, if you use Typescript.
const myCacheFactory = (): ICacheOps => {{
get: (k) => ["foo", "bar", "baz"], // or false
put: (k, v) => undefined
}}
The internal parse() function can return either an array of strings (parameter names), or false (no parameters found). This parse() function feeds the cache, and these are the only two states that are ever put().
On a cache miss, get() must return undefined. On hit, an array of parameter names (strings), or false - for 0 parameter names.
getParams((a, b, c) => undefined); // ["a", "b", "c"]
function fn1(a = (x, y = () => undefined, z) => undefined, [ b, [ d, [ f, [ g ] ] ] ], { c: { e: { h: { i } } } }, v) {};
getParams(fn1); // => ["a", "b", "d", "f", "g", "i", "v"]
function fn2(a = (x, y = () => undefined, z) => undefined, [ b, [ d, [ f = 2, [ g = 1 ] = [] ] = [] ] ], { c: { e: { h: { i = 3 } = {} } } }, v) {};
getParams(fn2)l // => ["a", "b", "d", "f", "g", "i", "v"]