isArrayLike
lang
Checks if value is array-like.
Installation
Import
import { isArrayLike } from '@tulx/utils';Source Code
Implementation
/**
* Checks if value is array-like.
*
* @param value - The value to check.
* @returns Returns true if value is array-like, else false.
*
* @example
* ```ts
* isArrayLike([1, 2, 3]); // true
* isArrayLike(document.body.children); // true
* isArrayLike('abc'); // true
* isArrayLike(() => {}); // false
* ```
*/
export function isArrayLike(value: unknown): value is ArrayLike<unknown> {
return (
value !== null &&
typeof value !== 'function' &&
typeof (value as { length?: unknown }).length === 'number' &&
(value as { length: number }).length >= 0 &&
(value as { length: number }).length % 1 === 0 &&
(value as { length: number }).length <= Number.MAX_SAFE_INTEGER
);
}
Example
import { isArrayLike } from '@tulx/utils';
isArrayLike([1, 2, 3]); // true
isArrayLike(document.body.children); // true
isArrayLike('abc'); // true
isArrayLike(() => {}); // falseRelated Functions
castArray
Casts value as an array if it's not one.
clone
Creates a shallow clone of value.
cloneDeep
This method is like clone except that it recursively clones value.
cloneDeepWith
This method is like cloneDeep except that it accepts customizer which is invoked to produce the cloned value.
cloneWith
This method is like clone except that it accepts customizer which is invoked to produce the cloned value.
conformsTo
Checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object.