Socket
Socket
Sign inDemoInstall

kind-of

Package Overview
Dependencies
0
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

kind-of

Get the native type of a value.


Version published
Maintainers
1
Weekly downloads
106,942,073
decreased by-11.03%

Weekly downloads

Package description

What is kind-of?

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.

What are kind-of's main functionalities?

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'

Other packages similar to kind-of

Readme

Source

kind-of NPM version Build Status

Get the native type of a value.

Install with npm

npm i kind-of --save

Usage

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'

Run tests

Install dev dependencies:

npm i -d && npm test

Benchmarks

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)

Author

Jon Schlinkert

License

Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb on February 09, 2015.

Keywords

FAQs

Last updated on 09 Feb 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc