Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
kind-of
Advanced tools
Get the native type of a value.
Weekly downloads
Package description
The kind-of npm package is a utility library that allows developers to check the type of a given value in JavaScript. It provides a simple API to determine whether a value is a string, number, array, object, function, and other JavaScript types. This can be particularly useful for type checking and validation in applications where type safety is important.
Type checking for primitives
This feature allows you to check the type of primitive values such as numbers, strings, and booleans.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(1)); // 'number' console.log(kindOf('hello')); // 'string' console.log(kindOf(true)); // 'boolean'
Type checking for objects
This feature allows you to check the type of object-based values such as plain objects, arrays, and regular expressions.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf({})); // 'object' console.log(kindOf([])); // 'array' console.log(kindOf(/foo/)); // 'regexp'
Type checking for functions and undefined
This feature allows you to check the type of functions and the undefined value.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(function () {})); // 'function' console.log(kindOf(undefined)); // 'undefined'
Type checking for null and dates
This feature allows you to check the type of null values and date objects.
"use strict"; const kindOf = require('kind-of'); console.log(kindOf(null)); // 'null' console.log(kindOf(new Date())); // 'date'
The 'type-check' package offers a similar functionality to 'kind-of' by allowing type assertions and checks. It provides a mini language to specify the types of values and can validate against those specifications.
The 'is' package is another utility library that provides type checking functions. It has a wider range of type checks available, including checks for specific JavaScript environments like the browser or Node.js.
The 'check-types' package provides a set of predicates for type checking. It has a fluent API and includes additional checks for min/max values, empty strings, and arrays, which are not directly available in 'kind-of'.
Readme
Get the native type of a value.
npm i kind-of --save
var kindOf = require('kind-of');
kindOf(undefined);
//=> 'undefined'
kindOf(null);
//=> 'null'
kindOf(true);
//=> 'boolean'
kindOf(false);
//=> 'boolean'
kindOf(new Boolean(true));
//=> 'boolean'
kindOf(new Buffer(''));
//=> 'buffer'
kindOf(42);
//=> 'number'
kindOf(new Number(42));
//=> 'number'
kindOf("string");
//=> 'string'
kindOf(arguments);
//=> 'arguments'
kindOf({});
//=> 'object'
kindOf(new Test());
//=> 'object'
kindOf(new Date());
//=> 'date'
kindOf([]);
//=> 'array'
kindOf([1, 2, 3]);
//=> 'array'
kindOf(new Array());
//=> 'array'
kindOf(/[\s\S]+/);
//=> 'regexp'
kindOf(new RegExp('^' + 'foo$'));
//=> 'regexp'
kindOf(function () {});
//=> 'function'
kindOf(new Function());
//=> 'function'
Install dev dependencies:
npm i -d && npm test
Benchmarked against typeof and type-of.
#1: array.js
kind-of x 21,578,944 ops/sec ±1.01% (97 runs sampled)
(lib) type-of x 4,593,840 ops/sec ±0.76% (92 runs sampled)
(lib) typeof x 5,786,776 ops/sec ±0.71% (97 runs sampled)
#2: boolean.js
kind-of x 25,189,600 ops/sec ±0.60% (97 runs sampled)
(lib) type-of x 2,751,076 ops/sec ±0.78% (100 runs sampled)
(lib) typeof x 4,390,312 ops/sec ±0.61% (99 runs sampled)
#3: date.js
kind-of x 8,862,303 ops/sec ±0.77% (99 runs sampled)
(lib) type-of x 6,239,662 ops/sec ±0.67% (94 runs sampled)
(lib) typeof x 6,180,922 ops/sec ±0.59% (97 runs sampled)
#4: function.js
kind-of x 19,685,336 ops/sec ±1.67% (95 runs sampled)
(lib) type-of x 6,648,551 ops/sec ±0.93% (95 runs sampled)
(lib) typeof x 6,631,967 ops/sec ±1.05% (92 runs sampled)
#5: null.js
kind-of x 24,155,010 ops/sec ±0.95% (91 runs sampled)
(lib) type-of x 12,854,583 ops/sec ±0.69% (94 runs sampled)
(lib) typeof x 8,182,952 ops/sec ±0.48% (99 runs sampled)
#6: number.js
kind-of x 20,993,521 ops/sec ±0.37% (98 runs sampled)
(lib) type-of x 2,112,716 ops/sec ±0.73% (96 runs sampled)
(lib) typeof x 4,492,943 ops/sec ±0.68% (96 runs sampled)
#7: object.js
kind-of x 3,686,169 ops/sec ±0.85% (96 runs sampled)
(lib) type-of x 3,661,833 ops/sec ±0.73% (98 runs sampled)
(lib) typeof x 6,159,847 ops/sec ±0.72% (98 runs sampled)
#8: regex.js
kind-of x 10,780,535 ops/sec ±0.75% (95 runs sampled)
(lib) type-of x 5,380,781 ops/sec ±0.83% (92 runs sampled)
(lib) typeof x 5,852,558 ops/sec ±0.67% (95 runs sampled)
#9: string.js
kind-of x 19,713,570 ops/sec ±0.69% (91 runs sampled)
(lib) type-of x 4,017,753 ops/sec ±0.85% (98 runs sampled)
(lib) typeof x 4,370,984 ops/sec ±0.62% (100 runs sampled)
#10: undef.js
kind-of x 23,250,387 ops/sec ±0.88% (91 runs sampled)
(lib) type-of x 13,725,183 ops/sec ±0.62% (91 runs sampled)
(lib) typeof x 20,549,334 ops/sec ±0.74% (97 runs sampled)
Jon Schlinkert
Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb on February 09, 2015.
FAQs
Get the native type of a value.
The npm package kind-of receives a total of 87,838,660 weekly downloads. As such, kind-of popularity was classified as popular.
We found that kind-of demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.