Gathering detailed insights and metrics for kind-of
Gathering detailed insights and metrics for kind-of
Gathering detailed insights and metrics for kind-of
Gathering detailed insights and metrics for kind-of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
npm install kind-of
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
351 Stars
121 Commits
40 Forks
11 Watching
1 Branches
11 Contributors
Updated on 25 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-11.4%
28,434,502
Compared to previous day
Last week
3%
187,600,769
Compared to previous week
Last month
55.6%
664,401,041
Compared to previous month
Last year
-22.2%
5,747,859,810
Compared to previous year
5
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:
1$ npm install --save kind-of
es5, es6, and browser ready
1const kindOf = require('kind-of'); 2 3kindOf(undefined); 4//=> 'undefined' 5 6kindOf(null); 7//=> 'null' 8 9kindOf(true); 10//=> 'boolean' 11 12kindOf(false); 13//=> 'boolean' 14 15kindOf(new Buffer('')); 16//=> 'buffer' 17 18kindOf(42); 19//=> 'number' 20 21kindOf('str'); 22//=> 'string' 23 24kindOf(arguments); 25//=> 'arguments' 26 27kindOf({}); 28//=> 'object' 29 30kindOf(Object.create(null)); 31//=> 'object' 32 33kindOf(new Test()); 34//=> 'object' 35 36kindOf(new Date()); 37//=> 'date' 38 39kindOf([1, 2, 3]); 40//=> 'array' 41 42kindOf(/foo/); 43//=> 'regexp' 44 45kindOf(new RegExp('foo')); 46//=> 'regexp' 47 48kindOf(new Error('error')); 49//=> 'error' 50 51kindOf(function () {}); 52//=> 'function' 53 54kindOf(function * () {}); 55//=> 'generatorfunction' 56 57kindOf(Symbol('str')); 58//=> 'symbol' 59 60kindOf(new Map()); 61//=> 'map' 62 63kindOf(new WeakMap()); 64//=> 'weakmap' 65 66kindOf(new Set()); 67//=> 'set' 68 69kindOf(new WeakSet()); 70//=> 'weakset' 71 72kindOf(new Int8Array()); 73//=> 'int8array' 74 75kindOf(new Uint8Array()); 76//=> 'uint8array' 77 78kindOf(new Uint8ClampedArray()); 79//=> 'uint8clampedarray' 80 81kindOf(new Int16Array()); 82//=> 'int16array' 83 84kindOf(new Uint16Array()); 85//=> 'uint16array' 86 87kindOf(new Int32Array()); 88//=> 'int32array' 89 90kindOf(new Uint32Array()); 91//=> 'uint32array' 92 93kindOf(new Float32Array()); 94//=> 'float32array' 95 96kindOf(new Float64Array()); 97//=> 'float64array'
Benchmarked against typeof and type-of.
1# arguments (32 bytes) 2 kind-of x 17,024,098 ops/sec ±1.90% (86 runs sampled) 3 lib-type-of x 11,926,235 ops/sec ±1.34% (83 runs sampled) 4 lib-typeof x 9,245,257 ops/sec ±1.22% (87 runs sampled) 5 6 fastest is kind-of (by 161% avg) 7 8# array (22 bytes) 9 kind-of x 17,196,492 ops/sec ±1.07% (88 runs sampled) 10 lib-type-of x 8,838,283 ops/sec ±1.02% (87 runs sampled) 11 lib-typeof x 8,677,848 ops/sec ±0.87% (87 runs sampled) 12 13 fastest is kind-of (by 196% avg) 14 15# boolean (24 bytes) 16 kind-of x 16,841,600 ops/sec ±1.10% (86 runs sampled) 17 lib-type-of x 8,096,787 ops/sec ±0.95% (87 runs sampled) 18 lib-typeof x 8,423,345 ops/sec ±1.15% (86 runs sampled) 19 20 fastest is kind-of (by 204% avg) 21 22# buffer (38 bytes) 23 kind-of x 14,848,060 ops/sec ±1.05% (86 runs sampled) 24 lib-type-of x 3,671,577 ops/sec ±1.49% (87 runs sampled) 25 lib-typeof x 8,360,236 ops/sec ±1.24% (86 runs sampled) 26 27 fastest is kind-of (by 247% avg) 28 29# date (30 bytes) 30 kind-of x 16,067,761 ops/sec ±1.58% (86 runs sampled) 31 lib-type-of x 8,954,436 ops/sec ±1.40% (87 runs sampled) 32 lib-typeof x 8,488,307 ops/sec ±1.51% (84 runs sampled) 33 34 fastest is kind-of (by 184% avg) 35 36# error (36 bytes) 37 kind-of x 9,634,090 ops/sec ±1.12% (89 runs sampled) 38 lib-type-of x 7,735,624 ops/sec ±1.32% (86 runs sampled) 39 lib-typeof x 7,442,160 ops/sec ±1.11% (90 runs sampled) 40 41 fastest is kind-of (by 127% avg) 42 43# function (34 bytes) 44 kind-of x 10,031,494 ops/sec ±1.27% (86 runs sampled) 45 lib-type-of x 9,502,757 ops/sec ±1.17% (89 runs sampled) 46 lib-typeof x 8,278,985 ops/sec ±1.08% (88 runs sampled) 47 48 fastest is kind-of (by 113% avg) 49 50# null (24 bytes) 51 kind-of x 18,159,808 ops/sec ±1.92% (86 runs sampled) 52 lib-type-of x 12,927,635 ops/sec ±1.01% (88 runs sampled) 53 lib-typeof x 7,958,234 ops/sec ±1.21% (89 runs sampled) 54 55 fastest is kind-of (by 174% avg) 56 57# number (22 bytes) 58 kind-of x 17,846,779 ops/sec ±0.91% (85 runs sampled) 59 lib-type-of x 3,316,636 ops/sec ±1.19% (86 runs sampled) 60 lib-typeof x 2,329,477 ops/sec ±2.21% (85 runs sampled) 61 62 fastest is kind-of (by 632% avg) 63 64# object-plain (47 bytes) 65 kind-of x 7,085,155 ops/sec ±1.05% (88 runs sampled) 66 lib-type-of x 8,870,930 ops/sec ±1.06% (83 runs sampled) 67 lib-typeof x 8,716,024 ops/sec ±1.05% (87 runs sampled) 68 69 fastest is lib-type-of (by 112% avg) 70 71# regex (25 bytes) 72 kind-of x 14,196,052 ops/sec ±1.65% (84 runs sampled) 73 lib-type-of x 9,554,164 ops/sec ±1.25% (88 runs sampled) 74 lib-typeof x 8,359,691 ops/sec ±1.07% (87 runs sampled) 75 76 fastest is kind-of (by 158% avg) 77 78# string (33 bytes) 79 kind-of x 16,131,428 ops/sec ±1.41% (85 runs sampled) 80 lib-type-of x 7,273,172 ops/sec ±1.05% (87 runs sampled) 81 lib-typeof x 7,382,635 ops/sec ±1.17% (85 runs sampled) 82 83 fastest is kind-of (by 220% avg) 84 85# symbol (34 bytes) 86 kind-of x 17,011,537 ops/sec ±1.24% (86 runs sampled) 87 lib-type-of x 3,492,454 ops/sec ±1.23% (89 runs sampled) 88 lib-typeof x 7,471,235 ops/sec ±2.48% (87 runs sampled) 89 90 fastest is kind-of (by 310% avg) 91 92# template-strings (36 bytes) 93 kind-of x 15,434,250 ops/sec ±1.46% (83 runs sampled) 94 lib-type-of x 7,157,907 ops/sec ±0.97% (87 runs sampled) 95 lib-typeof x 7,517,986 ops/sec ±0.92% (86 runs sampled) 96 97 fastest is kind-of (by 210% avg) 98 99# undefined (29 bytes) 100 kind-of x 19,167,115 ops/sec ±1.71% (87 runs sampled) 101 lib-type-of x 15,477,740 ops/sec ±1.63% (85 runs sampled) 102 lib-typeof x 19,075,495 ops/sec ±1.17% (83 runs sampled) 103 104 fastest is lib-typeof,kind-of 105
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:
typeof
checks were being used in my own libraries and other libraries I use a lot.Object
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();
just to get the word regex
? It's much faster to do if (type === '[object RegExp]') return 'regex'
require()
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):
1var typeOf = require('typeof'); 2function Test() {} 3console.log(typeOf(new Test())); 4//=> 'test'
Returns object
instead of arguments
:
1function foo() { 2 console.log(typeOf(arguments)) //=> 'object' 3} 4foo();
Incorrectly returns object
for generator functions, buffers, Map
, Set
, WeakMap
and WeakSet
:
1function * foo() {} 2console.log(typeOf(foo)); 3//=> 'object' 4console.log(typeOf(new Buffer(''))); 5//=> 'object' 6console.log(typeOf(new Map())); 7//=> 'object' 8console.log(typeOf(new Set())); 9//=> 'object' 10console.log(typeOf(new WeakMap())); 11//=> 'object' 12console.log(typeOf(new WeakSet())); 13//=> 'object'
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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:
1$ npm install && npm test
(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:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
true
if the given string looks like a glob pattern or an extglob pattern… more | homepagetrue
if the value is a primitive. | homepageCommits | Contributor |
---|---|
102 | jonschlinkert |
4 | doowb |
3 | aretecode |
2 | miguelmota |
1 | dtothefp |
1 | ianstormtaylor |
1 | ksheedlo |
1 | pdehaan |
1 | laggingreflex |
1 | tunnckoCore |
1 | struct78 |
Jon Schlinkert
Copyright © 2023, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on July 12, 2023.
The latest stable version of the package.
Stable Version
1
7.5/10
Summary
Validation Bypass in kind-of
Affected Versions
>= 6.0.0, < 6.0.3
Patched Versions
6.0.3
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More