mirror of
https://github.com/SikongJueluo/cc-utils.git
synced 2025-11-29 12:57: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) {
|
||||
return target;
|
||||
}
|
||||
if (target instanceof Date) {
|
||||
return new Date(target.getTime()) as any;
|
||||
if (Array.isArray(target)) {
|
||||
return (target as unknown[]).map((v: unknown) => deepCopy(v)) as T;
|
||||
}
|
||||
if (target instanceof Array) {
|
||||
const cp = [] as any[];
|
||||
(target as any[]).forEach((v) => { cp.push(v); });
|
||||
return cp.map((n: any) => deepCopy<any>(n)) as any;
|
||||
}
|
||||
if (typeof target === 'object') {
|
||||
const cp = { ...(target as { [key: string]: any }) } as { [key: string]: any };
|
||||
Object.keys(cp).forEach(k => {
|
||||
cp[k] = deepCopy<any>(cp[k]);
|
||||
if (typeof target === "object") {
|
||||
const cp = { ...(target as Record<string, unknown>) } as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
Object.keys(cp).forEach((k) => {
|
||||
cp[k] = deepCopy<unknown>(cp[k]);
|
||||
});
|
||||
return cp as T;
|
||||
}
|
||||
return target;
|
||||
};
|
||||
@MohammadFakhreddin
|
||||
|
||||
Reference in New Issue
Block a user