npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

isit

v1.1.0

Published

Tests a value’s type against a string like 'positive integer' or 'non-empty map'.

Downloads

60

Readme

isit

A Node.js module that tests a value’s type against a string like 'positive integer' or 'non-empty map'.

Installation

npm install isit --save

Usage

When calling isit(), the first argument is a space-separated string of type tests, and the second argument is the value to be tested. isit() returns true only if all tests pass.

const isit = require('isit')
isit('non-empty array', [1, 2, 3]) // true
isit('empty map', new Map()) // true
isit('positive integer', 1) // true

Available tests are listed in the “Type Tests” section below. Anything that does not match one of the available tests will be considered a class name (example: map in the above code block).

Negation

A test can be individually negated by prefixing it with non- or !, as in:

const isit = require('isit')
isit('non-empty array', [1, 2, 3]) // true
isit('array !empty', [1, 2, 3]) // true
isit('empty non-array', '') // true

Create Curried Functions

If you omit the second argument, a function is returned which runs the test provided in the first argument.

const isObject = require('isit')('non-array object')
isObject({}) // true
isObject([]) // false

Individual Test Functions

All tests are also available as member functions of isit, allowing you to run a single test like so:

const isit = require('isit')
isit.array([]) // true

const isString = require('isit').string
isString('test') // true

Type Tests

Here is the complete list:

  • arguments / args
  • array
  • blank
  • boolean / bool
  • boolish
  • buffer
  • collection
  • empty
  • false
  • falsey
  • float
  • finite
  • function
  • generator
  • infinity
  • integer / int
  • iterable
  • nan
  • negative
  • nil
  • null
  • number
  • numberish
  • numeric
  • object
  • objectbased
  • plain
  • positive
  • primitive
  • scalar
  • string
  • stringish
  • symbol
  • true
  • truthy
  • typedarray
  • undefined / undef

Undefined & Null

| Value: | undefinedundef | null | nil | | ----------- | :--------------------: | :----: | :---: | | undefined | ✔ | | ✔ | | null | | ✔ | ✔ |

Primitives & Scalars

| Value Type: | primitive | scalar | | ----------- | :---------: | :------: | | Undefined | ✔ | | | Null | ✔ | | | Boolean | ✔ | ✔ | | Number | ✔ | ✔ | | String | ✔ | ✔ | | Symbol | ✔ | ✔ | | Object | | | | Function | | ||

Booleans

| Value: | booleanbool | boolish | | -------------------- | :-----------------: | :-------: | | true | ✔ | ✔ | | false | ✔ | ✔ | | 'true' string | | ✔ | | 'false' string | | ✔ | | new Boolean(true) | | ✔ | | new Boolean(false) | | ✔ |

| Value: | true | truthy | | ------------------- | :----: | :------: | | true | ✔ | ✔ | | new Boolean(true) | ✔ | ✔ | | 'true' string | ✔ | ✔ | | 'false' string | | | | 1 | | ✔ | | 'test' | | ✔ | | [] | | ✔ |

| Value: | false | falsey | | -------------------- | :-----: | :------: | | false | ✔ | ✔ | | new Boolean(false) | ✔ | ✔ | | 'false' string | ✔ | ✔ | | 0 | | ✔ | | '' | | ✔ |

Empty Values

Every empty-checker out there assesses “emptiness” a bit differently. For our purposes, an empty value is one which contains no useful information except the absence of a value. Therefore, unlike many similar functions, the empty test does not consider 0, false, or zero-parameter functions to be “empty,” because these can often be intended as actual values.

| Value: | empty | | ------------- | :-----: | | undefined | ✔ | | null | ✔ | | NaN | ✔ | | 0 | | | false | | | '' | ✔ | | {} | ✔ | | [] | ✔ | | () => {} | | | new Error() | | | new Map() | ✔ | | new Set() | ✔ |

Blank Values

The blank test is the same as empty except it also returns true for strings that consist only of whitespace.

Functions

| Value: | function | generator | | ------------------- | :--------: | :---------: | | function () {} | ✔ | | | () => {} | ✔ | | | function* () {} | ✔ | ✔ |

Numbers

| Value: | number | numberish | numeric | | --------------- | :------: | :---------: | :-------: | | 0 | ✔ | ✔ | ✔ | | 1.23 | ✔ | ✔ | ✔ | | new Number(1) | | ✔ | ✔ | | '1' | | | ✔ | | '1e3' | | | ✔ | | NaN | | | ||

| Value: | positive | negative | | --------------- | :--------: | :--------: | | Infinity | ✔ | | | 123.45 | ✔ | | | 0 | ✔ | | | -0 | | ✔ | | -123.45 | | ✔ | | -Infinity | | ✔ |

JavaScript considers the number zero to be either positively or negatively signed; therefore, positive reports true for 0. If you want to exclude zero, consider using a simple x>0 test instead.

More Number Tests:

  • nan
  • integer / int
  • float
  • finite
  • infinity

Objects

Although functions are technically objects in JavaScript, they are often considered a separate category because the typeof operator gives them their own type. Use objectbased if you want to include functions.

| Value: | object | objectbased | | ------------ | :------: | :-----------: | | {} | ✔ | ✔ | | () => {} | | ✔ |

| Value: | object | plain | array | | ------------ | :------: | :-----: | :-----: | | new Date() | ✔ | | | | {} | ✔ | ✔ | | | [] | ✔ | | ✔ |

The object test returns true for arrays, because arrays are objects in JavaScript. If you want to exclude arrays, test against 'non-array object' instead.

Arguments

arguments or args returns true if the value is an Arguments object.

Buffers

The buffer test returns true if the value is a Node.js or Browserify Buffer.

Collections

The collection test returns true if the value is an instance of one of:

  • Map
  • Set
  • WeakMap
  • WeakSet

Iterables

The iterable test returns true if for...of can be used to iterate through the value.

Typed Arrays

The typedarray test returns true if the value is an instance of one of:

  • Int8Array
  • Uint8Array
  • Uint8ClampedArray
  • Int16Array
  • Uint16Array
  • Int32Array
  • Uint32Array
  • Float32Array
  • Float64Array

Strings

| Value: | string | stringish | | -------------------- | :------: | :---------: | | '' | ✔ | ✔ | | 'test' | ✔ | ✔ | | new String('test') | | ✔ |

Symbols

The symbol test returns true if the value is a symbol.

Class Testing

You can use isit.a or isit.an to see if an object is an instance of a given class.

You can provide the class itself or the class name as a case-insensitive string.

const isit = require('isit')

isit.a(Date, new Date()) // true
isit.a('date', new Date()) // true

isit.an(Error, new Error()) // true
isit.an('error', new Error()) // true

// Returns true because TypeError extends Error
isit.an(Error, new TypeError()) // true

You can also check if a given value is an instance of any one of a list of classes:

const isit = require('isit')

isit.a([TypeError, ReferenceError], new TypeError()) // true
isit.a('TypeError ReferenceError', new TypeError()) // true

Advanced Usage

Adding or Overriding Tests

You can add new tests, or override existing ones, by requiring isit/x and calling it as a function with the additional tests as an object argument. For example:

const isit = require('isit/x')({
  zero: value => value === 0
})
isit('negative zero', -0) // true
isit('non-zero integer', 1) // true