diginext-utils
Version:
README.md
29 lines • 706 B
JavaScript
import { isNull } from "./isNull.js";
/**
* Converts a value to an array.
* If the value is already an array, returns it as-is.
* If the value is null/undefined, returns an empty array.
* Otherwise, wraps the value in an array.
*
* @template T - The type of the value
* @param value - The value to convert
* @returns An array containing the value
*
* @example
* ```ts
* toArray(5); // [5]
* toArray([1, 2, 3]); // [1, 2, 3]
* toArray(null); // []
* toArray('hello'); // ['hello']
* ```
*/
export function toArray(value) {
if (isNull(value)) {
return [];
}
if (Array.isArray(value)) {
return value;
}
return [value];
}
//# sourceMappingURL=toArray.js.map