safers
Version:
Safely convert to the desired data and error-free refine values.
25 lines (24 loc) • 939 B
TypeScript
/**
* Converts the input value into an array representation based on its type.
*
* @param value - The value to be converted into an array.
* @returns An array representation of the input value.
*
* @remarks
* - If the input `value` is already an array, it returns the same array.
* - If the input `value` is an object, it converts it to an array using `Object.entries()`.
* - If the input `value` is a Map or Set, it converts it to an array using `Array.from()`.
* - For other types, it creates an array containing the input value as its single element.
*
* @example
* ```ts
* toArray([1, 2, 3]); // [1, 2, 3]
* toArray({ name: "John", age: 23 }); // [["name", "John"], ["age", 23]]
* toArray(new Map([["name", "John"], ["age", 23]])); // [["name", "John"], ["age", 23]]
*
* toArray(1); // [1]
* toArray("John"); // ["John"]
* toArray(null); // [null]
* ```
*/
export declare function toArray(value: unknown): any[];