basant307/AI_Governance_Project
048
1'use strict';2 3/* !4 * Chai - getFuncName utility5 * Copyright(c) 2012-2016 Jake Luer <jake@alogicalparadox.com>6 * MIT Licensed7 */8 9/**10 * ### .getFuncName(constructorFn)11 *12 * Returns the name of a function.13 * When a non-function instance is passed, returns `null`.14 * This also includes a polyfill function if `aFunc.name` is not defined.15 *16 * @name getFuncName17 * @param {Function} funct18 * @namespace Utils19 * @api public20 */21 22var toString = Function.prototype.toString;23var functionNameMatch = /\s*function(?:\s|\s*\/\*[^(?:*\/)]+\*\/\s*)*([^\s\(\/]+)/;24var maxFunctionSourceLength = 512;25function getFuncName(aFunc) {26 if (typeof aFunc !== 'function') {27 return null;28 }29 30 var name = '';31 if (typeof Function.prototype.name === 'undefined' && typeof aFunc.name === 'undefined') {32 // eslint-disable-next-line prefer-reflect33 var functionSource = toString.call(aFunc);34 // To avoid unconstrained resource consumption due to pathalogically large function names,35 // we limit the available return value to be less than 512 characters.36 if (functionSource.indexOf('(') > maxFunctionSourceLength) {37 return name;38 }39 // Here we run a polyfill if Function does not support the `name` property and if aFunc.name is not defined40 var match = functionSource.match(functionNameMatch);41 if (match) {42 name = match[1];43 }44 } else {45 // If we've got a `name` property we just use it46 name = aFunc.name;47 }48 49 return name;50}51 52module.exports = getFuncName;53 