import { cloneDeepWith } from 'funtool';
// 在克隆过程中将日期转换为ISO字符串
const obj = { created: new Date(), data: { value: 1 } };
const result = cloneDeepWith(obj, (val) => {
if (val instanceof Date) return val.toISOString();
});
// result => { created: "2023-07-20T12:34:56.789Z", data: { value: 1 } }
// 跳过特定属性的深度克隆
const config = { deep: { nested: true }, skip: { complex: true } };
const cloned = cloneDeepWith(config, (val, key) => {
if (key === 'skip') return val; // 返回原值而不进行深度克隆
});
// cloned.skip === config.skip (true)
// cloned.deep !== config.deep (true)
// 将数字转换为字符串
const data = { id: 123, name: "Test" };
const transformed = cloneDeepWith(data, (val) => {
if (typeof val === 'number') return String(val);
});
// transformed => { id: "123", name: "Test" }