What is is-primitive?
The is-primitive npm package is used to check if a given value is a JavaScript primitive. A primitive in JavaScript is data that is not an object and has no methods. In JavaScript, there are 7 primitive data types: string, number, bigint, boolean, undefined, symbol, and null. This package provides a simple and straightforward way to determine if a value belongs to one of these types.
What are is-primitive's main functionalities?
Check if a value is a primitive
This feature allows you to check if a value is a primitive type. The function returns true for primitive values and false for objects (including arrays and functions).
const isPrimitive = require('is-primitive');
console.log(isPrimitive(42)); // true
console.log(isPrimitive('hello')); // true
console.log(isPrimitive(false)); // true
console.log(isPrimitive({})); // false
console.log(isPrimitive([])); // false
console.log(isPrimitive(function() {})); // false
Other packages similar to is-primitive
is
The 'is' package provides a comprehensive set of type-check functions, including checks for primitives. It offers more functionality than is-primitive by including type checks for objects, arrays, regexes, functions, and more.
type-check
The 'type-check' package allows you to check the types of JavaScript values with an extended type syntax. It is more complex and feature-rich compared to is-primitive, as it supports custom type definitions and more intricate type checking.
is-type-of
The 'is-type-of' package has a similar purpose to is-primitive but includes additional checks for specific JavaScript types and structures, such as streams, buffers, and errors, making it more versatile for certain applications.
is-primitive
Returns true
if the value is a primitive.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install
Install with npm:
$ npm install --save is-primitive
Primitive values
The Mozilla docs for "Primitive values" defines specifies six data types that are primitives:
Boolean
Null
Undefined
Number
String
Symbol
(new in ECMAScript 6)
Usage
var isPrimitive = require('is-primitive');
true
All of the following return true
:
console.log(isPrimitive(false));
console.log(isPrimitive(true));
console.log(isPrimitive(null));
console.log(isPrimitive());
console.log(isPrimitive(42));
console.log(isPrimitive('abc'));
console.log(isPrimitive(Symbol()));
false
Everything else returns false
:
console.log(isPrimitive({}));
console.log(isPrimitive([]));
console.log(isPrimitive(function(){}));
console.log(isPrimitive(new Date()));
About
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:
$ 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:
$ npm install -g verbose/verb
Related projects
You might also be interested in these projects:
Contributors
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on November 23, 2017.