Collection predicates
Five predicates that ask “what shape of value is this, and is it empty?”. isObject, isPlainObject, Is.array, isIterable, and isEmpty.
| Predicate | Quick rule |
|---|---|
isObject(v) | Truthy AND typeof v === "object" |
isPlainObject(v) | Constructor name is exactly "Object" (or null-prototype) |
Is.array(v) | Array.isArray(v) |
isIterable(v) | Has a [Symbol.iterator] method |
isEmpty(v) | Smart emptiness (see below) |
isObject
Includes arrays, dates, regexes, class instances. Excludes null and functions.
isObject({}); // trueisObject([]); // trueisObject(new Date()); // trueisObject(/x/); // trueclass C {}isObject(new C()); // true
isObject(null); // falseisObject(undefined); // falseisObject(0); // falseisObject(""); // falseisObject("x"); // falseisObject(123); // falseisObject(() => {}); // falseTo check “object but not array”: isObject(x) && !Array.isArray(x). To check “object but also not date”: add && !(x instanceof Date). Or just use isPlainObject.
isPlainObject
Returns true ONLY for {} and new Object(). Class instances, dates, regexes, and arrays return false.
isPlainObject({}); // trueisPlainObject({ a: 1 }); // trueisPlainObject(new Object()); // true
isPlainObject([]); // falseisPlainObject(new Date()); // falseisPlainObject(/x/); // falseisPlainObject(new class {}); // falseisPlainObject("hello"); // falseisPlainObject(null); // falseisPlainObject(Object.create(null)); // true (null-prototype objects are plain)Is.array
Pure alias for Array.isArray. Same semantics: only literal arrays, not array-likes.
Is.array([]); // trueIs.array(new Array(3)); // true
Is.array(new Set()); // false (iterable, but not an Array)Is.array({ length: 0 }); // false (array-like, but not an Array)isIterable
Has a [Symbol.iterator] method.
isIterable([]); // trueisIterable("hello"); // trueisIterable(""); // true (empty string is still iterable)isIterable(new Set()); // trueisIterable(new Map()); // trueisIterable({ *[Symbol.iterator]() { yield 1; } }); // true
isIterable({}); // falseisIterable(123); // falseisIterable(null); // falseisIterable(undefined); // falseisEmpty
The most subtle predicate in the package. Branches:
| Input | Result | Why |
|---|---|---|
null, undefined, "" | true | Listed in the empty-set |
0, true, false | false | Listed in the “real value” set |
NaN | false | Treated as a numeric value, not absence |
Date instance | false | A constructed Date is a real value |
new Map() / new Set() | .size === 0 | Special cased |
Plain object (no Symbol.iterator) | Object.keys(v).length === 0 | Compared by own-key count |
| Iterable (array, string, …) | .length === 0 | Generic check |
Numeric (per isNumeric) | false | Numbers are real values |
| Everything else | true | Default fall-through |
isEmpty(null); // trueisEmpty(undefined); // trueisEmpty(""); // trueisEmpty([]); // trueisEmpty({}); // trueisEmpty(new Map()); // trueisEmpty(new Set()); // true
isEmpty(0); // falseisEmpty(false); // falseisEmpty("0"); // falseisEmpty(" "); // falseisEmpty([0]); // falseisEmpty(1); // falseisEmpty({ a: 1 }); // falseisEmpty(new Date()); // falseisEmpty(NaN); // false