Casts the provided value as an array if it's not one.
Array.isArray()
to determine if val
is an array and return it as-is or encapsulated in an array accordingly.const castArray = val => (Array.isArray(val) ? val : [val]);
castArray('foo'); // ['foo']
castArray([1]); // [1]
JavaScript, Type
Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.
JavaScript, Type
Checks if the provided argument is array-like (i.e. is iterable).
JavaScript, Type
Checks if the provided value is of the specified type.