All files / src/utils fnName.ts

57.14% Statements 12/21
55.56% Branches 10/18
50% Functions 2/4
57.14% Lines 12/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42    15x 15x 15x   15x                         15x 15x 7945x         7991x     7991x 46x   7945x   15x              
// from https://github.com/Xotic750/get-function-name-x/blob/master/index.js
 
import isFunction from "../preds/isFn";
import s from "./whiteSpace";
 
var _getFnName;
I
if ( ( function test() {} ).name !== 'test' ) {
  var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
  var fToString = Function.prototype.toString;
  var pMatch = String.prototype.match;
  var pReplace = String.prototype.replace;
  var reName = new RegExp(
      '^[' + s + ']*(?:function|class)[' + s + ']*\\*?[' + s + ']+([\\w\\$]+)[' + s + ']*',
      'i'
    );
  _getFnName = function getName( fn ) {
    var name = pMatch.call(
    pReplace.call( fToString.call( fn ), STRIP_COMMENTS, ' ' ),
    EreName
    );
    return name && name[ 1 ] !== 'anonymous' ? name[ 1 ] : '';
  };
} else {
    /*jshint evil:true */
  if ( ( new Function() ).name === 'anonymous' ) {
    I_getFnName = function getName( fn ) {
      return fn.name && fn.name !== 'anonymous' ? fn.name : '';
    };
  }
}
 
export default function getFunctionName( fn ) {
  if ( !isFunction( fn ) ) {
    return null;
  }
  if ( fn.__predToString ) {
    return fn.__predToString();
  }
  return _getFnName ? _getFnName( fn ) : fn.name;
}