function getFunctionName(func) {
if(typeof func === 'function') {
var funcNameArr = /^\s*(?:function)?\s*([^\(]*)\s*\((.*?)\)(?:\s*\{|\s*\=\>\s*\{?).*/gmi.exec(func.toString());
if(funcNameArr) {
return {
funcName: funcNameArr[1] || 'anonymous',
params: funcNameArr[2]
}
}
} else {
throw new Error('That\'s not function');
}
}
function example(a = {1:2, 2:3}, b=6, {d: d}, c) {
console.log('example function', a);
}
var d = (c = 2, {a}) => {};
function p(a, b=')', d = '{') { function o(){console.log('albert')}}
function MyFn(a,b=1,c=7){};
function s(a=')')
{
}
console.log(getFunctionName(example)); // {funcName: "example", params: "a = {1:2, 2:3}, b=6, {d: d}, c"}
console.log(getFunctionName(d)); // {funcName: "anonymous", params: "c = 2, {a}"}
console.log(getFunctionName(MyFn)); // {funcName: "MyFn", params: "a,b=1,c=7"}
console.log(getFunctionName(p)); // {funcName: "p", params: "a, b=')', d = '{'"}
console.log(getFunctionName(s)); // {funcName: "s", params: "a=')'"}