27 lines
686 B
TypeScript
27 lines
686 B
TypeScript
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));
|
|
} |