2022-04-08 10:55:35 +00:00
|
|
|
var isBuffer = require('is-buffer');
|
2022-01-21 08:28:41 +00:00
|
|
|
var toString = Object.prototype.toString;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the native `typeof` a value.
|
|
|
|
*
|
|
|
|
* @param {*} `val`
|
|
|
|
* @return {*} Native javascript type
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function kindOf(val) {
|
|
|
|
// primitivies
|
2022-04-08 10:55:35 +00:00
|
|
|
if (typeof val === 'undefined') {
|
2022-01-21 08:28:41 +00:00
|
|
|
return 'undefined';
|
|
|
|
}
|
|
|
|
if (val === null) {
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
if (val === true || val === false || val instanceof Boolean) {
|
|
|
|
return 'boolean';
|
|
|
|
}
|
2022-04-08 10:55:35 +00:00
|
|
|
if (typeof val === 'string' || val instanceof String) {
|
2022-01-21 08:28:41 +00:00
|
|
|
return 'string';
|
|
|
|
}
|
2022-04-08 10:55:35 +00:00
|
|
|
if (typeof val === 'number' || val instanceof Number) {
|
2022-01-21 08:28:41 +00:00
|
|
|
return 'number';
|
|
|
|
}
|
|
|
|
|
|
|
|
// functions
|
2022-04-08 10:55:35 +00:00
|
|
|
if (typeof val === 'function' || val instanceof Function) {
|
2022-01-21 08:28:41 +00:00
|
|
|
return 'function';
|
|
|
|
}
|
|
|
|
|
|
|
|
// array
|
|
|
|
if (typeof Array.isArray !== 'undefined' && Array.isArray(val)) {
|
|
|
|
return 'array';
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for instances of RegExp and Date before calling `toString`
|
|
|
|
if (val instanceof RegExp) {
|
|
|
|
return 'regexp';
|
|
|
|
}
|
|
|
|
if (val instanceof Date) {
|
|
|
|
return 'date';
|
|
|
|
}
|
|
|
|
|
|
|
|
// other objects
|
2022-04-08 10:55:35 +00:00
|
|
|
var type = toString.call(val);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
|
|
|
if (type === '[object RegExp]') {
|
|
|
|
return 'regexp';
|
|
|
|
}
|
|
|
|
if (type === '[object Date]') {
|
|
|
|
return 'date';
|
|
|
|
}
|
|
|
|
if (type === '[object Arguments]') {
|
|
|
|
return 'arguments';
|
|
|
|
}
|
|
|
|
if (type === '[object Error]') {
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
// buffer
|
|
|
|
if (isBuffer(val)) {
|
|
|
|
return 'buffer';
|
|
|
|
}
|
|
|
|
|
|
|
|
// es6: Map, WeakMap, Set, WeakSet
|
|
|
|
if (type === '[object Set]') {
|
|
|
|
return 'set';
|
|
|
|
}
|
|
|
|
if (type === '[object WeakSet]') {
|
|
|
|
return 'weakset';
|
|
|
|
}
|
|
|
|
if (type === '[object Map]') {
|
|
|
|
return 'map';
|
|
|
|
}
|
|
|
|
if (type === '[object WeakMap]') {
|
|
|
|
return 'weakmap';
|
|
|
|
}
|
|
|
|
if (type === '[object Symbol]') {
|
|
|
|
return 'symbol';
|
|
|
|
}
|
2022-04-08 10:55:35 +00:00
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
// typed arrays
|
|
|
|
if (type === '[object Int8Array]') {
|
|
|
|
return 'int8array';
|
|
|
|
}
|
|
|
|
if (type === '[object Uint8Array]') {
|
|
|
|
return 'uint8array';
|
|
|
|
}
|
|
|
|
if (type === '[object Uint8ClampedArray]') {
|
|
|
|
return 'uint8clampedarray';
|
|
|
|
}
|
|
|
|
if (type === '[object Int16Array]') {
|
|
|
|
return 'int16array';
|
|
|
|
}
|
|
|
|
if (type === '[object Uint16Array]') {
|
|
|
|
return 'uint16array';
|
|
|
|
}
|
|
|
|
if (type === '[object Int32Array]') {
|
|
|
|
return 'int32array';
|
|
|
|
}
|
|
|
|
if (type === '[object Uint32Array]') {
|
|
|
|
return 'uint32array';
|
|
|
|
}
|
|
|
|
if (type === '[object Float32Array]') {
|
|
|
|
return 'float32array';
|
|
|
|
}
|
|
|
|
if (type === '[object Float64Array]') {
|
|
|
|
return 'float64array';
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be a plain object
|
|
|
|
return 'object';
|
|
|
|
};
|