cxc-szcx-uniapp/uni_modules/lime-shared/toArray/index.ts
2024-09-14 10:26:50 +08:00

21 lines
682 B
TypeScript

// @ts-nocheck
/**
* 将一个或多个元素转换为数组
* @param item 要转换为数组的元素
* @returns 转换后的数组
*/
// #ifndef APP-IOS || APP-ANDROID
export const toArray = <T>(item: T | T[]): T[] => Array.isArray(item) ? item : [item];
// #endif
// #ifdef APP-IOS || APP-ANDROID
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"]