mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-11-04 19:27:50 +08:00
fix: deepcopy did't run as expected
This commit is contained in:
@@ -34,21 +34,18 @@ export const deepCopy = <T>(target: T): T => {
|
|||||||
if (target === null) {
|
if (target === null) {
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
if (target instanceof Date) {
|
if (Array.isArray(target)) {
|
||||||
return new Date(target.getTime()) as any;
|
return (target as unknown[]).map((v: unknown) => deepCopy(v)) as T;
|
||||||
}
|
}
|
||||||
if (target instanceof Array) {
|
if (typeof target === "object") {
|
||||||
const cp = [] as any[];
|
const cp = { ...(target as Record<string, unknown>) } as Record<
|
||||||
(target as any[]).forEach((v) => { cp.push(v); });
|
string,
|
||||||
return cp.map((n: any) => deepCopy<any>(n)) as any;
|
unknown
|
||||||
}
|
>;
|
||||||
if (typeof target === 'object') {
|
Object.keys(cp).forEach((k) => {
|
||||||
const cp = { ...(target as { [key: string]: any }) } as { [key: string]: any };
|
cp[k] = deepCopy<unknown>(cp[k]);
|
||||||
Object.keys(cp).forEach(k => {
|
|
||||||
cp[k] = deepCopy<any>(cp[k]);
|
|
||||||
});
|
});
|
||||||
return cp as T;
|
return cp as T;
|
||||||
}
|
}
|
||||||
return target;
|
return target;
|
||||||
};
|
};
|
||||||
@MohammadFakhreddin
|
|
||||||
|
|||||||
Reference in New Issue
Block a user