inspect-function
Advanced tools
Comparing version 0.1.2 to 0.2.0
#!/usr/bin/env node | ||
'use strict'; | ||
const isArrayLike = p => p.match(/^\[+/) || null; | ||
const isObjectLike = p => p.match(/^{+/) || null; | ||
const matchObjectProperty = p => p.match(/^([^{]+):(.*)/) || null; | ||
const unpackArrayOrObject = p => p.replace(/^[\[{]|[\]}]$/g, ''); | ||
const destructureParameter = (param, options) => { | ||
let destructured = getParametersArray(unpackArrayOrObject(param)); | ||
if (isArrayLike(param)) { | ||
return destructured.map(destructuredParam => getDestructuredParameterName(destructuredParam, options)); | ||
} | ||
if (isObjectLike(param)) { | ||
return destructured.reduce((paramValue, destructuredParam) => { | ||
let objectProperty = matchObjectProperty(destructuredParam); | ||
if(objectProperty){ | ||
let [, key, value] = objectProperty.map(v => v.trim()); | ||
paramValue[key] = getDestructuredParameterName(value, options); | ||
} else { | ||
paramValue[destructuredParam] = getDestructuredParameterName(destructuredParam, options); | ||
} | ||
return paramValue; | ||
}, {}); | ||
} | ||
}; | ||
const getDestructuredParameterName = (param, list) => { | ||
if(isArrayLike(param) || isObjectLike(param)){ | ||
return destructureParameter(param, list); | ||
} | ||
list.push(param); | ||
return param; | ||
} | ||
function getDestructuredParametersNames(parameters) { | ||
const parametersNames = []; | ||
inspectFunction(fnTest).parameters.names.forEach(param => { | ||
console.log('!', getDestructuredParameterName(param, parametersNames)); | ||
}); | ||
return parametersNames; | ||
} | ||
/** | ||
@@ -87,2 +131,3 @@ * Returns the parameters signature stringified | ||
let names = []; | ||
let expects = []; | ||
let defaultValues = {}; | ||
@@ -94,11 +139,19 @@ | ||
let defaultValue = paramSplit[1]; | ||
names.push(name); | ||
expects.push(name); | ||
if(defaultValue){ | ||
defaultValues[name] = defaultValue; | ||
} | ||
names = names.concat(getDestructuredParameterNames(name)); | ||
}); | ||
return { parameters: { names, defaultValues } }; | ||
return { parameters: { expects, defaultValues, names } }; | ||
} | ||
function getDestructuredParameterNames(name){ | ||
let desttructuredParameterNames = []; | ||
getDestructuredParameterName(name, desttructuredParameterNames); | ||
return desttructuredParameterNames; | ||
} | ||
function getSignatureFromParametersArray(name, paramsArray) { | ||
@@ -133,2 +186,3 @@ return `${name}(${paramsArray.map(v => v.replace(/^([^=]*)=/, `$1 = `)).join(', ')});` | ||
const { parameters } = getParametersInfo(parametersDefinitions); | ||
parameters.definitions = parametersDefinitions; | ||
const signature = getSignatureFromParametersArray(name, parametersDefinitions); | ||
@@ -139,3 +193,2 @@ | ||
name, | ||
parametersDefinitions, | ||
parameters, | ||
@@ -142,0 +195,0 @@ signature |
{ | ||
"name": "inspect-function", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Inspects a function and returns informations about it (e.g. name, parameters names, parameters and default values, signature)", | ||
@@ -5,0 +5,0 @@ "main": "lib/inspect-function.js", |
@@ -23,3 +23,3 @@ # inspect-function | ||
// Just a function to test | ||
const testFunction = (a = 'z', b = [1,2,3], c) => console.log(a,b,c); | ||
const testFunction = (a = 'z', b = [1,2,3], c, {d,e: {f}, g} = {}) => console.log('noop'); | ||
@@ -29,25 +29,39 @@ // Inspects | ||
// If the second param, `name`, is passed in. | ||
// This will be the value of result.name | ||
// `result` will be: | ||
{ | ||
// If the second param, `name`, is passed in, | ||
// it will be the value of "name" here | ||
"name": "testFunction", | ||
"parametersDefinitions": [ | ||
"a='z'", | ||
"b=[1,2,3]", | ||
"c" | ||
], | ||
"parameters": { | ||
"names": [ | ||
"expects": [ | ||
"a", | ||
"b", | ||
"c" | ||
"c", | ||
"{d,e:{f},g}" | ||
], | ||
"defaultValues": { | ||
"a": "'z'", | ||
"b": "[1,2,3]" | ||
} | ||
"b": "[1,2,3]", | ||
"{d,e:{f},g}": "{}" | ||
}, | ||
// Note that `"names"` contains also | ||
// The parameters names after Destructuring | ||
"names": [ | ||
"a", | ||
"b", | ||
"c", | ||
"d", | ||
"f", | ||
"g" | ||
], | ||
"definitions": [ | ||
"a='z'", | ||
"b=[1,2,3]", | ||
"c", | ||
"{d,e:{f},g}={}" | ||
] | ||
}, | ||
"signature": "testFunction(a = 'z', b = [1,2,3], c);" | ||
"signature": "testFunction(a = 'z', b = [1,2,3], c, {d,e:{f},g} = {});" | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13442
275
65