@deebeetech/is-helper
v3.0.0
Published
A collection of "is"-style helpers to help solidify if something "is" something else. Has no dependencies and uses only raw JavaScript methodology.
Maintainers
Readme
is-helper
Zero-dependency type-checking utilities for JavaScript and TypeScript. Every check works with unknown values and returns proper type guards where possible.
Install
npm install @deebeetech/is-helperUsage
import is from "@deebeetech/is-helper";Null, Undefined, and Friends
is.null(null); // true
is.null(undefined); // false
is.undefined(undefined); // true
is.undefined(null); // false
is.nil(null); // true — null OR undefined
is.nil(undefined); // true
is.nil(0); // false
is.defined(0); // true — anything that isn't null/undefined
is.defined(null); // false
is.nothing(null); // true — null, undefined, empty string, or whitespace
is.nothing(""); // true
is.nothing(" "); // true
is.nothing("hello"); // falseStrings
is.string("hello"); // true
is.string(42); // false
is.string.empty(""); // true
is.string.empty("hi"); // false
is.string.whitespace(" "); // true — non-empty, all whitespace
is.string.whitespace(""); // false
is.string.blank(""); // true — empty OR whitespace-only
is.string.blank(" "); // true
is.string.blank("hi"); // falseNumbers
Numeric strings like "42" and "3.14" are treated as numbers too (up to two decimal places).
is.number(42); // true
is.number("3.14"); // true
is.number(""); // false
is.number(null); // false
is.number.positive(5); // true
is.number.positive(-1); // false
is.number.positive("10"); // true
is.number.integer(7); // true
is.number.integer(3.5); // false
is.number.positiveInteger(3); // true
is.number.positiveInteger(-2); // false
is.number.positiveInteger(1.5); // falseBooleans
Goes beyond typeof — recognizes "true", "false", "y", "n", "yes", "no", "1", "0", and the numbers 1 and 0.
is.boolean(true); // true
is.boolean("yes"); // true
is.boolean("n"); // true
is.boolean(1); // true
is.boolean("maybe"); // falseUse is.boolean.value() to extract the actual boolean:
is.boolean.value("yes"); // true
is.boolean.value("n"); // false
is.boolean.value(0); // false
is.boolean.value("true"); // trueArrays
Covers regular arrays and all typed arrays (Float64Array, Uint8Array, etc.).
is.array([1, 2, 3]); // true
is.array(new Uint8Array(4)); // true
is.array("not an array"); // false
is.array.empty([]); // true
is.array.empty([1]); // false
is.array.nonEmpty([1, 2]); // true
is.array.nonEmpty([]); // falseObjects
Only plain {} objects — arrays, null, and functions don't count.
is.object({ a: 1 }); // true
is.object([1, 2]); // false
is.object(null); // false
is.object.empty({}); // true
is.object.empty({ a: 1 }); // false
is.object.plain({}); // true
is.object.plain(Object.create(null)); // true
is.object.plain(new (class Foo {})); // falseFunctions
is.fn(() => {}); // true
is.fn(console.log); // true
is.fn("not a function"); // falseDates
is.date(new Date()); // true
is.date("2024-01-01"); // falseIPv4
is.ipv4("192.168.1.1"); // true
is.ipv4("999.999.999.999"); // false
is.ipv4("not an ip"); // falseCombinators
is.any() and is.all() let you compose checks into reusable validators.
const isStringOrNumber = is.any(is.string, is.number);
isStringOrNumber("hello"); // true
isStringOrNumber(42); // true
isStringOrNumber(null); // false
const isPositiveInt = is.all(is.number.positive, is.number.integer);
isPositiveInt(5); // true
isPositiveInt(-3); // false
isPositiveInt(2.5); // falseTypeScript
Most checks double as type guards, so the compiler narrows types automatically:
function greet(name: unknown) {
if (is.string(name)) {
console.log(name.toUpperCase()); // name is narrowed to string
}
}License
MIT
