gitmyhub

is.js

JavaScript ★ 9.1k updated 4y ago

Micro check library

A tiny JavaScript utility library that replaces scattered typeof checks and regex tests with one clean API for validating value types, formats, and environment details.

JavaScriptsetup: easycomplexity 2/5

is.js is a small JavaScript library for checking what kind of value a variable holds. It has no dependencies and works in browsers, in Node.js, and with AMD-style module loaders, so it drops into almost any JavaScript project without friction.

The library covers a wide range of checks in one consistent style. You can ask whether something is an array, a string, a number, a date, a function, a regular expression, null, undefined, NaN, a DOM element, or the browser window object. There are also checks for whether a value is empty, whether a number falls within a certain range, whether a string matches a particular format (email, URL, credit card number, IP address, and more), and whether the code is running on a particular browser, operating system, or device type.

Every check comes with three extra interfaces built in. The plain call tests a single value. The not version inverts the result, so is.not.array('foo') returns true because 'foo' is not an array. The all version tests multiple values and returns true only if every one of them passes. The any version returns true if at least one of them passes. Both all and any can accept either a list of arguments or a single array.

The goal is to replace scattered typeof checks, length comparisons, and regular expression tests spread across a codebase with a single readable library call. Rather than writing multi-step conditional logic to confirm a value's type or format, a developer writes one descriptive line. The checks themselves handle the edge cases, such as the fact that NaN has type 'number' in plain JavaScript, or that arrays report as 'object' with typeof.

The README is a long catalog of available checks with short code examples for each one. The full README is longer than what was shown.

Where it fits