MCPcopy Create free account
hub / github.com/esengine/esengine / deepClone

Method deepClone

packages/core/src/ECS/Core/SoASerializer.ts:70–107  ·  view source on GitHub ↗

* 深拷贝对象

(obj: T)

Source from the content-addressed store, hash-verified

68 * 深拷贝对象
69 */
70 public static deepClone<T>(obj: T): T {
71 if (obj === null || typeof obj !== 'object') {
72 return obj;
73 }
74
75 if (obj instanceof Date) {
76 return new Date(obj.getTime()) as T;
77 }
78
79 if (Array.isArray(obj)) {
80 return obj.map((item) => this.deepClone(item)) as T;
81 }
82
83 if (obj instanceof Map) {
84 const cloned = new Map();
85 for (const [key, value] of obj.entries()) {
86 cloned.set(key, this.deepClone(value));
87 }
88 return cloned as T;
89 }
90
91 if (obj instanceof Set) {
92 const cloned = new Set();
93 for (const value of obj.values()) {
94 cloned.add(this.deepClone(value));
95 }
96 return cloned as T;
97 }
98
99 // 普通对象
100 const cloned = {} as Record<string, unknown>;
101 for (const key in obj) {
102 if (Object.prototype.hasOwnProperty.call(obj, key)) {
103 cloned[key] = this.deepClone((obj as Record<string, unknown>)[key]);
104 }
105 }
106 return cloned as T;
107 }
108}

Callers 2

Calls 6

isArrayMethod · 0.80
entriesMethod · 0.80
setMethod · 0.65
mapMethod · 0.45
valuesMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected