kind-of
Advanced tools
Comparing version 5.1.0 to 6.0.0
225
index.js
var toString = Object.prototype.toString; | ||
/** | ||
* Get the native `typeof` a value. | ||
* | ||
* @param {*} `val` | ||
* @return {*} Native javascript type | ||
*/ | ||
module.exports = function kindOf(val) { | ||
if (val === void 0) return 'undefined'; | ||
if (val === null) return 'null'; | ||
module.exports = function kindOf(val) { | ||
var type = typeof val; | ||
// primitivies | ||
if (type === 'undefined') { | ||
return 'undefined'; | ||
if (type === 'boolean') return 'boolean'; | ||
if (type === 'string') return 'string'; | ||
if (type === 'number') return 'number'; | ||
if (type === 'symbol') return 'symbol'; | ||
if (type === 'function') { | ||
return isGeneratorFn(val) ? 'generatorfunction' : 'function'; | ||
} | ||
if (val === null) { | ||
return 'null'; | ||
} | ||
if (val === true || val === false || val instanceof Boolean) { | ||
return 'boolean'; | ||
} | ||
if (type === 'string' || val instanceof String) { | ||
return 'string'; | ||
} | ||
if (type === 'number' || val instanceof Number) { | ||
return 'number'; | ||
} | ||
// functions | ||
if (type === 'function' || val instanceof Function) { | ||
if (typeof val.constructor.name !== 'undefined' && val.constructor.name.slice(0, 9) === 'Generator') { | ||
return 'generatorfunction'; | ||
} | ||
return 'function'; | ||
} | ||
// array | ||
if (typeof Array.isArray !== 'undefined' && Array.isArray(val)) { | ||
if (Array.isArray) { | ||
if (Array.isArray(val)) return 'array'; | ||
} else if (val instanceof Array) { | ||
return 'array'; | ||
} | ||
// check for instances of RegExp and Date before calling `toString` | ||
if (val instanceof RegExp) { | ||
return 'regexp'; | ||
} | ||
if (val instanceof Date) { | ||
return 'date'; | ||
} | ||
if (isBuffer(val)) return 'buffer'; | ||
if (isArguments(val)) return 'arguments'; | ||
if (isDate(val)) return 'date'; | ||
if (isError(val)) return 'error'; | ||
if (isRegexp(val)) return 'regexp'; | ||
// other objects | ||
type = toString.call(val); | ||
switch (ctorName(val)) { | ||
case 'Symbol': return 'symbol'; | ||
case 'Promise': return 'promise'; | ||
if (type === '[object RegExp]') { | ||
return 'regexp'; | ||
// Set, Map, WeakSet, WeakMap | ||
case 'WeakMap': return 'weakmap'; | ||
case 'WeakSet': return 'weakset'; | ||
case 'Map': return 'map'; | ||
case 'Set': return 'set'; | ||
// 8-bit typed arrays | ||
case 'Int8Array': return 'int8array'; | ||
case 'Uint8Array': return 'uint8array'; | ||
case 'Uint8ClampedArray': return 'uint8clampedarray'; | ||
// 16-bit typed arrays | ||
case 'Int16Array': return 'int16array'; | ||
case 'Uint16Array': return 'uint16array'; | ||
// 32-bit typed arrays | ||
case 'Int32Array': return 'int32array'; | ||
case 'Uint32Array': return 'uint32array'; | ||
case 'Float32Array': return 'float32array'; | ||
case 'Float64Array': return 'float64array'; | ||
} | ||
if (type === '[object Date]') { | ||
return 'date'; | ||
} | ||
if (type === '[object Arguments]') { | ||
return 'arguments'; | ||
} | ||
if (type === '[object Error]') { | ||
return 'error'; | ||
} | ||
if (type === '[object Promise]') { | ||
return 'promise'; | ||
} | ||
// buffer | ||
if (isBuffer(val)) { | ||
return 'buffer'; | ||
if (isGeneratorObj(val)) { | ||
return 'generator'; | ||
} | ||
// es6: Map, WeakMap, Set, WeakSet | ||
if (type === '[object Set]') { | ||
return 'set'; | ||
// Non-plain objects | ||
type = toString.call(val); | ||
switch (type) { | ||
case '[object Object]': return 'object'; | ||
// iterators | ||
case '[object Map Iterator]': return 'mapiterator'; | ||
case '[object Set Iterator]': return 'setiterator'; | ||
case '[object String Iterator]': return 'stringiterator'; | ||
case '[object Array Iterator]': return 'arrayiterator'; | ||
} | ||
if (type === '[object WeakSet]') { | ||
return 'weakset'; | ||
} | ||
if (type === '[object Map]') { | ||
return 'map'; | ||
} | ||
if (type === '[object WeakMap]') { | ||
return 'weakmap'; | ||
} | ||
if (type === '[object Symbol]') { | ||
return 'symbol'; | ||
} | ||
if (type === '[object Map Iterator]') { | ||
return 'mapiterator'; | ||
} | ||
if (type === '[object Set Iterator]') { | ||
return 'setiterator'; | ||
} | ||
if (type === '[object String Iterator]') { | ||
return 'stringiterator'; | ||
} | ||
if (type === '[object Array Iterator]') { | ||
return 'arrayiterator'; | ||
} | ||
// 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'; | ||
// other | ||
return type.slice(8, -1).toLowerCase().replace(/\s/g, ''); | ||
}; | ||
function ctorName(val) { | ||
return val.constructor ? val.constructor.name : null; | ||
} | ||
function isError(val) { | ||
return typeof val.message === 'string' && val.constructor | ||
&& typeof val.constructor.stackTraceLimit === 'number'; | ||
} | ||
function isDate(val) { | ||
if (val instanceof Date) return true; | ||
return typeof val.toDateString === 'function' | ||
&& typeof val.getDate === 'function' | ||
&& typeof val.setDate === 'function'; | ||
} | ||
function isRegexp(val) { | ||
if (val instanceof RegExp) return true; | ||
return typeof val.flags === 'string' | ||
&& typeof val.ignoreCase === 'boolean' | ||
&& typeof val.multiline === 'boolean' | ||
&& typeof val.global === 'boolean'; | ||
} | ||
function isGeneratorFn(name, val) { | ||
return ctorName(name) === 'GeneratorFunction'; | ||
} | ||
function isGeneratorObj(val) { | ||
return typeof val.throw === 'function' | ||
&& typeof val.return === 'function' | ||
&& typeof val.next === 'function'; | ||
} | ||
function isArguments(val) { | ||
try { | ||
if (typeof val.length === 'number' && typeof val.callee === 'function') { | ||
return true; | ||
} | ||
} catch (err) { | ||
if (err.message.indexOf('callee') !== -1) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -144,5 +126,6 @@ * If you need to support Safari 5-7 (8-10 yr-old browser), | ||
function isBuffer(val) { | ||
return val.constructor | ||
&& typeof val.constructor.isBuffer === 'function' | ||
&& val.constructor.isBuffer(val); | ||
if (val.constructor && typeof val.constructor.isBuffer === 'function') { | ||
return val.constructor.isBuffer(val); | ||
} | ||
return false; | ||
} |
{ | ||
"name": "kind-of", | ||
"description": "Get the native type of a value.", | ||
"version": "5.1.0", | ||
"version": "6.0.0", | ||
"homepage": "https://github.com/jonschlinkert/kind-of", | ||
@@ -34,10 +34,7 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"devDependencies": { | ||
"ansi-bold": "^0.1.1", | ||
"benchmarked": "^1.1.1", | ||
"benchmarked": "^2.0.0", | ||
"browserify": "^14.4.0", | ||
"gulp-format-md": "^0.1.12", | ||
"matched": "^0.4.4", | ||
"mocha": "^3.4.2", | ||
"type-of": "^2.0.1", | ||
"typeof": "^1.0.0" | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^4.0.1", | ||
"write": "^1.0.3" | ||
}, | ||
@@ -68,9 +65,2 @@ "keywords": [ | ||
"verb": { | ||
"related": { | ||
"list": [ | ||
"is-glob", | ||
"is-number", | ||
"is-primitive" | ||
] | ||
}, | ||
"toc": false, | ||
@@ -87,2 +77,9 @@ "layout": "default", | ||
}, | ||
"related": { | ||
"list": [ | ||
"is-glob", | ||
"is-number", | ||
"is-primitive" | ||
] | ||
}, | ||
"reflinks": [ | ||
@@ -89,0 +86,0 @@ "type-of", |
180
README.md
@@ -28,3 +28,3 @@ # kind-of [![NPM version](https://img.shields.io/npm/v/kind-of.svg?style=flat)](https://www.npmjs.com/package/kind-of) [![NPM monthly downloads](https://img.shields.io/npm/dm/kind-of.svg?style=flat)](https://npmjs.org/package/kind-of) [![NPM total downloads](https://img.shields.io/npm/dt/kind-of.svg?style=flat)](https://npmjs.org/package/kind-of) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/kind-of.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/kind-of) | ||
> es5, browser and es6 ready | ||
> es5, es6, and browser ready | ||
@@ -46,5 +46,2 @@ ```js | ||
kindOf(new Boolean(true)); | ||
//=> 'boolean' | ||
kindOf(new Buffer('')); | ||
@@ -56,11 +53,5 @@ //=> 'buffer' | ||
kindOf(new Number(42)); | ||
//=> 'number' | ||
kindOf('str'); | ||
//=> 'string' | ||
kindOf(new String('str')); | ||
//=> 'string' | ||
kindOf(arguments); | ||
@@ -81,11 +72,5 @@ //=> 'arguments' | ||
kindOf([]); | ||
//=> 'array' | ||
kindOf([1, 2, 3]); | ||
//=> 'array' | ||
kindOf(new Array()); | ||
//=> 'array' | ||
kindOf(/foo/); | ||
@@ -101,6 +86,6 @@ //=> 'regexp' | ||
kindOf(function * () {}); | ||
//=> 'function' | ||
//=> 'generatorfunction' | ||
kindOf(new Function()); | ||
//=> 'function' | ||
kindOf(Symbol('str')); | ||
//=> 'symbol' | ||
@@ -119,5 +104,2 @@ kindOf(new Map()); | ||
kindOf(Symbol('str')); | ||
//=> 'symbol' | ||
kindOf(new Int8Array()); | ||
@@ -151,76 +133,112 @@ //=> 'int8array' | ||
## Release history | ||
## Benchmarks | ||
### v4.0.0 | ||
Benchmarked against [typeof](http://github.com/CodingFu/typeof) and [type-of](https://github.com/ForbesLindesay/type-of). | ||
**Added** | ||
```bash | ||
# arguments (32 bytes) | ||
kind-of x 17,024,098 ops/sec ±1.90% (86 runs sampled) | ||
lib-type-of x 11,926,235 ops/sec ±1.34% (83 runs sampled) | ||
lib-typeof x 9,245,257 ops/sec ±1.22% (87 runs sampled) | ||
* `promise` support | ||
fastest is kind-of (by 161% avg) | ||
### v5.0.0 | ||
# array (22 bytes) | ||
kind-of x 17,196,492 ops/sec ±1.07% (88 runs sampled) | ||
lib-type-of x 8,838,283 ops/sec ±1.02% (87 runs sampled) | ||
lib-typeof x 8,677,848 ops/sec ±0.87% (87 runs sampled) | ||
**Added** | ||
fastest is kind-of (by 196% avg) | ||
* `Set Iterator` and `Map Iterator` support | ||
# boolean (24 bytes) | ||
kind-of x 16,841,600 ops/sec ±1.10% (86 runs sampled) | ||
lib-type-of x 8,096,787 ops/sec ±0.95% (87 runs sampled) | ||
lib-typeof x 8,423,345 ops/sec ±1.15% (86 runs sampled) | ||
**Fixed** | ||
fastest is kind-of (by 204% avg) | ||
* Now returns `generatorfunction` for generator functions | ||
# buffer (38 bytes) | ||
kind-of x 14,848,060 ops/sec ±1.05% (86 runs sampled) | ||
lib-type-of x 3,671,577 ops/sec ±1.49% (87 runs sampled) | ||
lib-typeof x 8,360,236 ops/sec ±1.24% (86 runs sampled) | ||
## Benchmarks | ||
fastest is kind-of (by 247% avg) | ||
Benchmarked against [typeof](http://github.com/CodingFu/typeof) and [type-of](https://github.com/ForbesLindesay/type-of). | ||
Note that performaces is slower for es6 features `Map`, `WeakMap`, `Set` and `WeakSet`. | ||
# date (30 bytes) | ||
kind-of x 16,067,761 ops/sec ±1.58% (86 runs sampled) | ||
lib-type-of x 8,954,436 ops/sec ±1.40% (87 runs sampled) | ||
lib-typeof x 8,488,307 ops/sec ±1.51% (84 runs sampled) | ||
```bash | ||
#1: array | ||
current x 23,329,397 ops/sec ±0.82% (94 runs sampled) | ||
lib-type-of x 4,170,273 ops/sec ±0.55% (94 runs sampled) | ||
lib-typeof x 9,686,935 ops/sec ±0.59% (98 runs sampled) | ||
fastest is kind-of (by 184% avg) | ||
#2: boolean | ||
current x 27,197,115 ops/sec ±0.85% (94 runs sampled) | ||
lib-type-of x 3,145,791 ops/sec ±0.73% (97 runs sampled) | ||
lib-typeof x 9,199,562 ops/sec ±0.44% (99 runs sampled) | ||
# error (36 bytes) | ||
kind-of x 9,634,090 ops/sec ±1.12% (89 runs sampled) | ||
lib-type-of x 7,735,624 ops/sec ±1.32% (86 runs sampled) | ||
lib-typeof x 7,442,160 ops/sec ±1.11% (90 runs sampled) | ||
#3: date | ||
current x 20,190,117 ops/sec ±0.86% (92 runs sampled) | ||
lib-type-of x 5,166,970 ops/sec ±0.74% (94 runs sampled) | ||
lib-typeof x 9,610,821 ops/sec ±0.50% (96 runs sampled) | ||
fastest is kind-of (by 127% avg) | ||
#4: function | ||
current x 23,855,460 ops/sec ±0.60% (97 runs sampled) | ||
lib-type-of x 5,667,740 ops/sec ±0.54% (100 runs sampled) | ||
lib-typeof x 10,010,644 ops/sec ±0.44% (100 runs sampled) | ||
# function (34 bytes) | ||
kind-of x 10,031,494 ops/sec ±1.27% (86 runs sampled) | ||
lib-type-of x 9,502,757 ops/sec ±1.17% (89 runs sampled) | ||
lib-typeof x 8,278,985 ops/sec ±1.08% (88 runs sampled) | ||
#5: null | ||
current x 27,061,047 ops/sec ±0.97% (96 runs sampled) | ||
lib-type-of x 13,965,573 ops/sec ±0.62% (97 runs sampled) | ||
lib-typeof x 8,460,194 ops/sec ±0.61% (97 runs sampled) | ||
fastest is kind-of (by 113% avg) | ||
#6: number | ||
current x 25,075,682 ops/sec ±0.53% (99 runs sampled) | ||
lib-type-of x 2,266,405 ops/sec ±0.41% (98 runs sampled) | ||
lib-typeof x 9,821,481 ops/sec ±0.45% (99 runs sampled) | ||
# null (24 bytes) | ||
kind-of x 18,159,808 ops/sec ±1.92% (86 runs sampled) | ||
lib-type-of x 12,927,635 ops/sec ±1.01% (88 runs sampled) | ||
lib-typeof x 7,958,234 ops/sec ±1.21% (89 runs sampled) | ||
#7: object | ||
current x 3,348,980 ops/sec ±0.49% (99 runs sampled) | ||
lib-type-of x 3,245,138 ops/sec ±0.60% (94 runs sampled) | ||
lib-typeof x 9,262,952 ops/sec ±0.59% (99 runs sampled) | ||
fastest is kind-of (by 174% avg) | ||
#8: regex | ||
current x 21,284,827 ops/sec ±0.72% (96 runs sampled) | ||
lib-type-of x 4,689,241 ops/sec ±0.43% (100 runs sampled) | ||
lib-typeof x 8,957,593 ops/sec ±0.62% (98 runs sampled) | ||
# number (22 bytes) | ||
kind-of x 17,846,779 ops/sec ±0.91% (85 runs sampled) | ||
lib-type-of x 3,316,636 ops/sec ±1.19% (86 runs sampled) | ||
lib-typeof x 2,329,477 ops/sec ±2.21% (85 runs sampled) | ||
#9: string | ||
current x 25,379,234 ops/sec ±0.58% (96 runs sampled) | ||
lib-type-of x 3,635,148 ops/sec ±0.76% (93 runs sampled) | ||
lib-typeof x 9,494,134 ops/sec ±0.49% (98 runs sampled) | ||
fastest is kind-of (by 632% avg) | ||
#10: undef | ||
current x 27,459,221 ops/sec ±1.01% (93 runs sampled) | ||
lib-type-of x 14,360,433 ops/sec ±0.52% (99 runs sampled) | ||
lib-typeof x 23,202,868 ops/sec ±0.59% (94 runs sampled) | ||
# object-plain (47 bytes) | ||
kind-of x 7,085,155 ops/sec ±1.05% (88 runs sampled) | ||
lib-type-of x 8,870,930 ops/sec ±1.06% (83 runs sampled) | ||
lib-typeof x 8,716,024 ops/sec ±1.05% (87 runs sampled) | ||
fastest is lib-type-of (by 112% avg) | ||
# regex (25 bytes) | ||
kind-of x 14,196,052 ops/sec ±1.65% (84 runs sampled) | ||
lib-type-of x 9,554,164 ops/sec ±1.25% (88 runs sampled) | ||
lib-typeof x 8,359,691 ops/sec ±1.07% (87 runs sampled) | ||
fastest is kind-of (by 158% avg) | ||
# string (33 bytes) | ||
kind-of x 16,131,428 ops/sec ±1.41% (85 runs sampled) | ||
lib-type-of x 7,273,172 ops/sec ±1.05% (87 runs sampled) | ||
lib-typeof x 7,382,635 ops/sec ±1.17% (85 runs sampled) | ||
fastest is kind-of (by 220% avg) | ||
# symbol (34 bytes) | ||
kind-of x 17,011,537 ops/sec ±1.24% (86 runs sampled) | ||
lib-type-of x 3,492,454 ops/sec ±1.23% (89 runs sampled) | ||
lib-typeof x 7,471,235 ops/sec ±2.48% (87 runs sampled) | ||
fastest is kind-of (by 310% avg) | ||
# template-strings (36 bytes) | ||
kind-of x 15,434,250 ops/sec ±1.46% (83 runs sampled) | ||
lib-type-of x 7,157,907 ops/sec ±0.97% (87 runs sampled) | ||
lib-typeof x 7,517,986 ops/sec ±0.92% (86 runs sampled) | ||
fastest is kind-of (by 210% avg) | ||
# undefined (29 bytes) | ||
kind-of x 19,167,115 ops/sec ±1.71% (87 runs sampled) | ||
lib-type-of x 15,477,740 ops/sec ±1.63% (85 runs sampled) | ||
lib-typeof x 19,075,495 ops/sec ±1.17% (83 runs sampled) | ||
fastest is lib-typeof,kind-of | ||
``` | ||
@@ -239,7 +257,7 @@ | ||
kind-of is more correct than other type checking libs I've looked at. For example, here are some differing results from other popular libs: | ||
kind-of seems to be more consistently "correct" than other type checking libs I've looked at. For example, here are some differing results from other popular libs: | ||
### [typeof](https://github.com/CodingFu/typeof) lib | ||
Incorrectly tests instances of custom constructors (pretty common): | ||
Incorrectly identifies instances of custom constructors (pretty common): | ||
@@ -289,3 +307,3 @@ ```js | ||
<details> | ||
</details> | ||
@@ -301,3 +319,3 @@ <details> | ||
<details> | ||
</details> | ||
@@ -315,3 +333,3 @@ <details> | ||
<details> | ||
</details> | ||
@@ -330,3 +348,3 @@ ### Related projects | ||
| --- | --- | | ||
| 82 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 84 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 3 | [aretecode](https://github.com/aretecode) | | ||
@@ -333,0 +351,0 @@ | 2 | [miguelmota](https://github.com/miguelmota) | |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22289
5
5
360
109
1