2.0.0 • Published 1 year ago

@slimio/is v2.0.0

Weekly downloads
335
License
MIT
Repository
github
Last release
1 year ago

SlimIO IS

version Maintenance MIT size build

Node.js JavaScript Type checker (Primitives, Objects, etc..)

Package heavily inspired by @sindresorhus/is. This package aims to work on Node.js (no browser support).

Why

  • Focus on type checking (no fancy feature).
  • Focus on Node.js support.
  • Come with a TypeScript definition (which works).
  • Is concerned about being stable.

Requirements

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/is
# or
$ yarn add @slimio/is

Usage example

const { strictEqual } = require("assert");
const is = require("@slimio/is");

strictEqual(is.bool(true), true);
strictEqual(is.string("hello"), true);
strictEqual(is.map(new Map()), true);
strictEqual(is.func(() => {}), true);

The is const namespace is a Plain JavaScript Object with a lot of exported methods (check the below API Documentation).

API

All methods can be called as follow: is.{methodName}. All methods return a boolean value.

Primitives

methodexample
stringis.string("hello")
numberis.number(10)
booleanis.boolean(true)
boolis.bool(false)
symbolis.symbol(Symbol("foo"))
undefinedis.undefined(undefined)
bigintis.bigint(50n)
nullValueis.nullValue(null)
nullOrUndefinedis.nullOrUndefined(null)
primitiveis.primitive("hello")

is.null is not available because of a name restriction.

Objects

methodexample
promiseis.promise(new Promise())
classObjectis.classObject(new Class{})
arrayis.array([])
objectis.object({})
plainObjectis.plainObject(Object.create(null))
setis.set(new Set())
mapis.map(new Map())
weakMapis.weakMap(new WeakMap())
weakSetis.weakSet(new WeakSet())
erroris.error(new Error("ooppss!"))
dateis.date(new Date())
regExpis.regExp(/^hello world$/)
bufferis.buffer(Buffer.from("hello"))

is.class is not available because of a name restriction.

Functions & Iterators

methodexample
funcis.func(new Function())
generatorFunctionN/A
asyncFunctionis.asyncFunction(async function() {})
boundFunctionis.boundFunction((function(){}).bind(null))
iterableis.iterable([1, 2])
asyncIterableN/A
generatorN/A

is.function has been reduced to is.func because of a name restriction.

Typed Arrays

methodexample
typedArrayis.typedArray(new int8Array())
int8Arrayis.int8Array(new int8Array())
uint8Arrayis.uint8Array(new uint8Array())
uint8ClampedArrayis.uint8ClampedArray(new uint8ClampedArray())
int16Arrayis.int16Array(new int16Array())
uint16Arrayis.uint16Array(new uint16Array())
int32Arrayis.int32Array(new int32Array())
uint32Arrayis.uint32Array(new uint32Array())
float32Arrayis.float32Array(new float32Array())
float64Arrayis.float64Array(new float64Array())
arrayBufferis.arrayBuffer(new ArrayBuffer())
sharedArrayBufferis.sharedArrayBuffer(new SharedArrayBuffer())
dataViewis.dataView(new DataView(new ArrayBuffer(8)))

Misc

methodexample
nanis.nan(Number("booom!"))
integeris.integer(5 / 10)
directInstanceOfis.directInstanceOf(Object, {})
truthyis.truthy(true)
falsyis.falsy("")
emptyStringis.emptyString("")

Dependencies

This project have no dependencies.

License

MIT