mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-28 03:34:26 +03:00
7016baf301
Closes #597
1.0 KiB
1.0 KiB
castArray
::: info
This function is only available in es-toolkit/compat
for compatibility reasons. It either has alternative native JavaScript APIs or isn’t fully optimized yet.
When imported from es-toolkit/compat
, it behaves exactly like lodash and provides the same functionalities, as detailed here.
:::
Casts value as an array if it's not one.
Signature
function castArray<T>(value?: T | readonly T[]): T[];
Parameters
value
(T | readonly T[]
): The value to be cast to an array.
Returns
(T[]
): An array containing the input value if it wasn't an array, or the original array if it was.
Examples
import { castArray } from 'es-toolkit/compat';
const arr1 = castArray(1);
// Returns: [1]
const arr2 = castArray([1]);
// Returns: [1]
const arr3 = castArray({ a: 1 });
// Returns: [{'a': 1}]
const arr4 = castArray(null);
// Returns: [null]
const arr5 = castArray(undefined);
// Returns: [undefined]
const arr6 = castArray();
// Returns: []