generated from justjavac/deno_starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
46 lines (41 loc) · 1.11 KB
/
mod.ts
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
43
44
45
46
interface FunctionWithName extends Function {
displayName: string;
}
/**
* Extract names from functions.
*
* @param fn The function who's name we need to extract.
* @returns The name of the function.
*/
export default function name(fn: Function): string {
if (
typeof (fn as FunctionWithName).displayName === "string" &&
fn.constructor.name
) {
return (fn as FunctionWithName).displayName;
} else if (typeof fn.name === "string" && fn.name) {
return fn.name;
}
//
// Check to see if the constructor has a name.
//
if (
typeof fn === "object" &&
(fn as ObjectConstructor).constructor &&
typeof (fn as ObjectConstructor).constructor.name === "string"
) {
return (fn as ObjectConstructor).constructor.name;
}
//
// toString the given function and attempt to parse it out of it, or determine
// the class.
//
var named = fn.toString(),
type = Object.prototype.toString.call(fn).slice(8, -1);
if ("Function" === type) {
named = named.substring(named.indexOf("(") + 1, named.indexOf(")"));
} else {
named = type;
}
return named || "anonymous";
}