Skip to content

Instantly share code, notes, and snippets.

@tmarshall
Last active May 10, 2020 22:46
Show Gist options
  • Save tmarshall/1fffd5eb228e7268a452c0b0c5b4a951 to your computer and use it in GitHub Desktop.
Save tmarshall/1fffd5eb228e7268a452c0b0c5b4a951 to your computer and use it in GitHub Desktop.
gets the 'type' of a value
/*
returns the 'type' of a single value
getType({}) // 'object'
getType([]) // 'array'
getType('') // 'string'
getType(12) // 'number'
getType(5n) // 'bigint'
getType(new Date()) // 'date'
getType(new Error()) // 'error'
gotchyas:
- only basic arrays will return 'array' - Int8Array and others will return 'object'
- anything not determined will return 'object'
- this does not differentiate between things like functions and async functions
- set up for Node, tested on v12 - not set up for browsers
*/
function getType(value) {
// captures primitives
// may give `'object'` or other value if a custom class
// overrides the expected `.valueOf()`
const primitiveValue = !(value instanceof Date) && value && value.valueOf && value.valueOf.toString() === 'function valueOf() { [native code] }' ? value.valueOf() : value
let valueType = typeof primitiveValue
if (valueType !== 'object') {
return valueType
}
// the following code deals with built-in objects
// that are not primitives
// (thus `typeof` is `'object'`)
// undefined is a primitive
// null is a built-in value
if (value === null) {
return 'null'
}
if (Array.isArray(value)) {
return 'array'
}
if (value instanceof Date) {
return 'date'
}
if (value instanceof Error) {
return 'error'
}
if (value instanceof RegExp) {
return 'regexp'
}
if (
value instanceof Map ||
value instanceof WeakMap
) {
return 'map'
}
if (
value instanceof Set ||
value instanceof WeakSet
) {
return 'set'
}
if (value instanceof Promise) {
return 'promise'
}
return 'object'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment