cxc-szcx-uniapp/uni_modules/lime-shared/toArray/index.ts

21 lines
670 B
TypeScript
Raw Normal View History

2024-09-14 02:26:50 +00:00
// @ts-nocheck
/**
*
* @param item
* @returns
*/
2025-02-11 01:34:29 +00:00
// #ifndef UNI-APP-X && APP
2024-09-14 02:26:50 +00:00
export const toArray = <T>(item: T | T[]): T[] => Array.isArray(item) ? item : [item];
// #endif
2025-02-11 01:34:29 +00:00
// #ifdef UNI-APP-X && APP
2024-09-14 02:26:50 +00:00
export function toArray<T extends any>(item: any): T[] {
return Array.isArray(item) ? item as T[] : [item as T]// as T[]
};
// #endif
// 示例
// console.log(toArray(5)); // 输出: [5]
// console.log(toArray("hello")); // 输出: ["hello"]
// console.log(toArray([1, 2, 3])); // 输出: [1, 2, 3]
// console.log(toArray(["apple", "banana"])); // 输出: ["apple", "banana"]