feat: lots more improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
27
src/models/clone-plain.ts
Normal file
27
src/models/clone-plain.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { isProxy, toRaw } from 'vue';
|
||||
|
||||
function normalizeCloneable<T>(value: T): T {
|
||||
if (value === null || value === undefined) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (typeof value !== 'object') {
|
||||
return value;
|
||||
}
|
||||
|
||||
const rawValue = isProxy(value) ? toRaw(value) : value;
|
||||
|
||||
if (Array.isArray(rawValue)) {
|
||||
return rawValue.map(item => normalizeCloneable(item)) as T;
|
||||
}
|
||||
|
||||
const plainObject = Object.fromEntries(
|
||||
Object.entries(rawValue).map(([key, nestedValue]) => [key, normalizeCloneable(nestedValue)])
|
||||
);
|
||||
|
||||
return plainObject as T;
|
||||
}
|
||||
|
||||
export function clonePlain<T>(value: T): T {
|
||||
return structuredClone(normalizeCloneable(value));
|
||||
}
|
||||
Reference in New Issue
Block a user