castArray
lang
Casts value as an array if it's not one.
Installation
Import
import { castArray } from '@tulx/utils';Source Code
Implementation
/**
* Casts value as an array if it's not one.
*
* @param value - The value to inspect.
* @returns Returns the cast array.
*
* @example
* ```ts
* castArray(1); // [1]
* castArray({ 'a': 1 }); // [{ 'a': 1 }]
* castArray('abc'); // ['abc']
* castArray(null); // [null]
* castArray(undefined); // [undefined]
* castArray(); // []
* ```
*/
export function castArray<T>(value?: T): T[] {
if (arguments.length === 0) {
return [];
}
return Array.isArray(value) ? value : [value as T];
}
Example
import { castArray } from '@tulx/utils';
castArray(1); // [1]
castArray({ 'a': 1 }); // [{ 'a': 1 }]
castArray('abc'); // ['abc']
castArray(null); // [null]
castArray(undefined); // [undefined]
castArray(); // []Related Functions
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.
eq
Performs a SameValueZero comparison between two values to determine if they are equivalent.