Get the native type of a value.
npm install kind-of> Get the native type of a value.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
``sh`
$ npm install --save kind-of
Install with bower
`sh`
$ bower install kind-of --save
1. it's fast | optimizations
2. better type checking
> es5, es6, and browser ready
`js
var kindOf = require('kind-of');
kindOf(undefined);
//=> 'undefined'
kindOf(null);
//=> 'null'
kindOf(true);
//=> 'boolean'
kindOf(false);
//=> 'boolean'
kindOf(new Buffer(''));
//=> 'buffer'
kindOf(42);
//=> 'number'
kindOf('str');
//=> 'string'
kindOf(arguments);
//=> 'arguments'
kindOf({});
//=> 'object'
kindOf(Object.create(null));
//=> 'object'
kindOf(new Test());
//=> 'object'
kindOf(new Date());
//=> 'date'
kindOf([1, 2, 3]);
//=> 'array'
kindOf(/foo/);
//=> 'regexp'
kindOf(new RegExp('foo'));
//=> 'regexp'
kindOf(new Error('error'));
//=> 'error'
kindOf(function () {});
//=> 'function'
kindOf(function * () {});
//=> 'generatorfunction'
kindOf(Symbol('str'));
//=> 'symbol'
kindOf(new Map());
//=> 'map'
kindOf(new WeakMap());
//=> 'weakmap'
kindOf(new Set());
//=> 'set'
kindOf(new WeakSet());
//=> 'weakset'
kindOf(new Int8Array());
//=> 'int8array'
kindOf(new Uint8Array());
//=> 'uint8array'
kindOf(new Uint8ClampedArray());
//=> 'uint8clampedarray'
kindOf(new Int16Array());
//=> 'int16array'
kindOf(new Uint16Array());
//=> 'uint16array'
kindOf(new Int32Array());
//=> 'int32array'
kindOf(new Uint32Array());
//=> 'uint32array'
kindOf(new Float32Array());
//=> 'float32array'
kindOf(new Float64Array());
//=> 'float64array'
`
Benchmarked against typeof and type-of.
`basharguments (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)
fastest is kind-of (by 161% avg)
fastest is kind-of (by 196% avg)
fastest is kind-of (by 204% avg)
fastest is kind-of (by 247% avg)
fastest is kind-of (by 184% avg)
fastest is kind-of (by 127% avg)
fastest is kind-of (by 113% avg)
fastest is kind-of (by 174% avg)
fastest is kind-of (by 632% avg)
fastest is lib-type-of (by 112% avg)
fastest is kind-of (by 158% avg)
fastest is kind-of (by 220% avg)
fastest is kind-of (by 310% avg)
fastest is kind-of (by 210% avg)
fastest is lib-typeof,kind-of
`
In 7 out of 8 cases, this library is 2x-10x faster than other top libraries included in the benchmarks. There are a few things that lead to this performance advantage, none of them hard and fast rules, but all of them simple and repeatable in almost any code library:
1. Optimize around the fastest and most common use cases first. Of course, this will change from project-to-project, but I took some time to understand how and why typeof checks were being used in my own libraries and other libraries I use a lot.Object
2. Optimize around bottlenecks - In other words, the order in which conditionals are implemented is significant, because each check is only as fast as the failing checks that came before it. Here, the biggest bottleneck by far is checking for plain objects (an object that was created by the constructor). I opted to make this check happen by process of elimination rather than brute force up front (e.g. by using something like val.constructor.name), so that every other type check would not be penalized it..slice(8, -1).toLowerCase();
3. Don't do uneccessary processing - why do just to get the word regex? It's much faster to do if (type === '[object RegExp]') return 'regex'require()
4. There is no reason to make the code in a microlib as terse as possible, just to win points for making it shorter. It's always better to favor performant code over terse code. You will always only be using a single statement to use the library anyway, regardless of how the code is written.
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:
Incorrectly identifies instances of custom constructors (pretty common):
`js`
var typeOf = require('typeof');
function Test() {}
console.log(typeOf(new Test()));
//=> 'test'
Returns object instead of arguments:
`js`
function foo() {
console.log(typeOf(arguments)) //=> 'object'
}
foo();
Incorrectly returns object for generator functions, buffers, Map, Set, WeakMap and WeakSet:
`js`
function * foo() {}
console.log(typeOf(foo));
//=> 'object'
console.log(typeOf(new Buffer('')));
//=> 'object'
console.log(typeOf(new Map()));
//=> 'object'
console.log(typeOf(new Set()));
//=> 'object'
console.log(typeOf(new WeakMap()));
//=> 'object'
console.log(typeOf(new WeakSet()));
//=> 'object'
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
`sh`
$ npm install && npm test
Building docs
_(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)_
To generate the readme, run the following command:
`sh`
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
* is-glob: Returns true if the given string looks like a glob pattern or an extglob pattern… more | truetrue if the given string looks like a glob pattern or an extglob pattern. This makes it easy to create code that only uses external modules like node-glob when necessary, resulting in much faster code execution and initialization time, and a bet"" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">homepage
* is-number: Returns true if a number or string value is a finite number. Useful for regex… more | homepage
* is-primitive: Returns if the value is a primitive. | true` if the value is a primitive. "" class="text-primary hover:underline" target="_blank" rel="noopener noreferrer">homepage
| Commits | Contributor |
| --- | --- |
| 102 | jonschlinkert |
| 3 | aretecode |
| 2 | miguelmota |
| 1 | doowb |
| 1 | dtothefp |
| 1 | ianstormtaylor |
| 1 | ksheedlo |
| 1 | pdehaan |
| 1 | laggingreflex |
| 1 | tunnckoCore |
| 1 | xiaofen9 |
Jon Schlinkert
* GitHub Profile
* Twitter Profile
* LinkedIn Profile
Copyright © 2020, Jon Schlinkert.
Released under the MIT License.
*
_This file was generated by verb-generate-readme, v0.8.0, on January 16, 2020._