uvue.uts 808 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @ts-nocheck
  2. /**
  3. * 深度克隆一个对象或数组
  4. * @param obj 要克隆的对象或数组
  5. * @returns 克隆后的对象或数组
  6. */
  7. export function cloneDeep<T>(obj : any) : T {
  8. if (obj instanceof Set) {
  9. const set = new Set<any>();
  10. obj.forEach((item : any) => {
  11. set.add(item)
  12. })
  13. return set as T;
  14. }
  15. if (obj instanceof Map) {
  16. const map = new Map<any, any>();
  17. obj.forEach((value : any, key : any) => {
  18. map.set(key, value)
  19. })
  20. return map as T;
  21. }
  22. if (obj instanceof RegExp) {
  23. return new RegExp(obj) as T;
  24. }
  25. if (Array.isArray(obj)) {
  26. return (obj as any[]).map((item : any):any => item) as T;
  27. }
  28. if (obj instanceof Date) {
  29. return new Date(obj.getTime()) as T;
  30. }
  31. if (typeof obj == 'object') {
  32. return UTSJSONObject.assign<T>({}, toRaw(obj))!
  33. }
  34. return obj as T
  35. }