• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of
2export function miniKindOf(val: any): string {
3    if (val === void 0) return 'undefined'
4    if (val === null) return 'null'
5
6    const type = typeof val
7    switch (type) {
8        case 'boolean':
9        case 'string':
10        case 'number':
11        case 'symbol':
12        case 'function':
13        {
14            return type
15        }
16    }
17
18    if (Array.isArray(val)) return 'array'
19    if (isDate(val)) return 'date'
20    if (isError(val)) return 'error'
21
22    const constructorName = ctorName(val)
23    switch (constructorName) {
24        case 'Symbol':
25        case 'Promise':
26        case 'WeakMap':
27        case 'WeakSet':
28        case 'Map':
29        case 'Set':
30            return constructorName
31    }
32
33    // other
34    return Object.prototype.toString
35        .call(val)
36        .slice(8, -1)
37        .toLowerCase()
38        .replace(/\s/g, '')
39}
40
41function ctorName(val: any): string | null {
42    return typeof val.constructor === 'function' ? val.constructor.name : null
43}
44
45function isError(val: any) {
46    return (
47        val instanceof Error ||
48        (typeof val.message === 'string' &&
49        val.constructor &&
50        typeof val.constructor.stackTraceLimit === 'number')
51    )
52}
53
54function isDate(val: any) {
55    if (val instanceof Date) return true
56    return (
57        typeof val.toDateString === 'function' &&
58        typeof val.getDate === 'function' &&
59        typeof val.setDate === 'function'
60    )
61}
62
63export function kindOf(val: any) {
64    let typeOfVal: string = typeof val
65
66    typeOfVal = miniKindOf(val)
67
68    return typeOfVal
69}
70