@bemoje/parse-params
v0.0.1
Published
Parse function parameters and get their names, count, incl rest-parameters and default values.
Downloads
1
Maintainers
Readme
@bemoje/parse-params
Parse function parameters and get their names, count, incl rest-parameters and default values.
install
npm install --save @bemoje/parse-params
usage
import parseParams from '@bemoje/parse-params'
parseParams(() => {})
//=> { name: '', count: 0, rest: false, params: [] }
parseParams(function someParsee(hello = 'world', num, ...palmTrees) {})
// => {
// name: 'someParsee',
// count: 3,
// rest: true,
// params: [
// { arg: 0, name: 'hello', default: "'world'" },
// { arg: 1, name: 'num', default: null },
// { arg: 2, name: '...palmTrees', default: null }
// ]
// }
Docs
/**
* Parse function parameters and get their names, count, incl rest-parameters and default values.
* @param {Function} fn - The function to parse
* @returns {ParseResult} {name,count,rest,params:[...{arg,name,default}]}
*/
export function parseParams(fn: Function): ParseResult
export default parseParams
/**
* @typedef {object} Param
* @property {index} arg - The parameter index number
* @property {string} name - The parameter name
* @property {string} default - The default value. Not the value, but a string because it's just a parsed extract of the
* function source code.
*/
/**
* @typedef {object} ParseResult
* @property {string} name - The function name
* @property {integer} count - The total number of parameters excl. rest-parameters.
* @property {boolean} rest - whether or not the last param is rest parameters.
* @property {Array<Param>} params - Array with details about individual arguments
*/