index.ts 670 B

123456789101112131415161718192021
  1. // @ts-nocheck
  2. /**
  3. * 将一个或多个元素转换为数组
  4. * @param item 要转换为数组的元素
  5. * @returns 转换后的数组
  6. */
  7. // #ifndef UNI-APP-X && APP
  8. export const toArray = <T>(item: T | T[]): T[] => Array.isArray(item) ? item : [item];
  9. // #endif
  10. // #ifdef UNI-APP-X && APP
  11. export function toArray<T extends any>(item: any): T[] {
  12. return Array.isArray(item) ? item as T[] : [item as T]// as T[]
  13. };
  14. // #endif
  15. // 示例
  16. // console.log(toArray(5)); // 输出: [5]
  17. // console.log(toArray("hello")); // 输出: ["hello"]
  18. // console.log(toArray([1, 2, 3])); // 输出: [1, 2, 3]
  19. // console.log(toArray(["apple", "banana"])); // 输出: ["apple", "banana"]