/
type.js
34 lines (29 loc) · 968 Bytes
/
type.js
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
import { _isArray } from './_internals/_isArray'
export function type(input){
const typeOf = typeof input
if (input === null){
return 'Null'
} else if (input === undefined){
return 'Undefined'
} else if (typeOf === 'boolean'){
return 'Boolean'
} else if (typeOf === 'number'){
return Number.isNaN(input) ? 'NaN' : 'Number'
} else if (typeOf === 'string'){
return 'String'
} else if (_isArray(input)){
return 'Array'
} else if (typeOf === 'symbol'){
return 'Symbol'
} else if (input instanceof RegExp){
return 'RegExp'
}
const asStr = input && input.toString ? input.toString() : ''
if ([ 'true', 'false' ].includes(asStr)) return 'Boolean'
if (!Number.isNaN(Number(asStr))) return 'Number'
if (asStr.startsWith('async')) return 'Async'
if (asStr === '[object Promise]') return 'Promise'
if (typeOf === 'function') return 'Function'
if (input instanceof String) return 'String'
return 'Object'
}