Array Like
March 5, 2020 ยท View on GitHub
Array-like value (any value with length property)
array-like/is
Restricted array-like confirmation. Returns true for every value that meets following contraints
- is an object (or with
allowStringoption, a string) - is not a function
- Exposes
lengththat meetsarray-lengthconstraints
const isArrayLike = require("type/array-like/is");
isArrayLike([]); // true
isArrayLike({}); // false
isArrayLike({ length: 0 }); // true
isArrayLike("foo"); // false
isArrayLike("foo", { allowString: true }); // true
array-like/ensure
If given argument is an array-like, it is returned back. Otherwise TypeError is thrown.
const ensureArrayLike = require("type/array-like/ensure");
ensureArrayLike({ length: 0 }); // { length: 0 }
ensureArrayLike("foo", { allowString: true }); // "foo"
ensureArrayLike({}); // Thrown TypeError: null is not an iterable