更新记录
1.0.0(2025-05-28)
下载此版本
平台兼容性
uni-app x
Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
√ |
√ |
5.0 |
12 |
√ |
√ |
其他
kux-deepmerge
参考 deepmerge 设计的深度合并工具库,专为 UTS 跨平台开发设计。
功能特色
- ✅ 深度对象合并
- ✅ 智能数组合并策略
- ✅ 循环引用检测
- ✅ 自定义合并规则
- ✅ 类型安全校验
快速开始
基础用法
merge
import { merge } from '@/uni_modules/kux-deepmerge';
const x = {
foo: { bar: 3 },
array: [{
does: 'work',
too: [ 1, 2, 3 ]
}]
}
const y = {
foo: { baz: 4 },
quux: 5,
array: [{
does: 'work',
too: [ 4, 5, 6 ]
}, {
really: 'yes'
}]
}
const output = {
foo: {
bar: 3,
baz: 4
},
array: [{
does: 'work',
too: [ 1, 2, 3 ]
}, {
does: 'work',
too: [ 4, 5, 6 ]
}, {
really: 'yes'
}],
quux: 5
}
merge(x, y) // => output
mergeAll
mergeAll([x, y]) // => output
数组合并策略
// 默认追加模式
merge([1, 2], [3, 4]) // => [1, 2, 3, 4]
// 自定义覆盖模式
const options = {
arrayMerge: (target, source) => [...source]
} as MergeOptions
merge([1, 2], [3, 4], options) // => [3, 4]
配置选项
MergeOptions
参数说明
参数 |
类型 |
默认值 |
描述 |
clone |
(boolean | null)? |
true |
是否克隆原始对象 |
arrayMerge |
(target[], source[], options) => any[] |
合并后追加 |
自定义数组合并策略 |
customMerge |
(key: string) => MergeFunction? |
- |
按属性名自定义合并逻辑 |
isMergeableObject |
(obj) => boolean? |
非数组对象 |
判断是否可合并的对象 |
高级示例
// 自定义属性合并规则
const mergeOptions = {
customMerge: (key: string): MergeFunction | null => {
if (key == 'specialKey') {
// 特殊合并逻辑演示
return ((x: any, y: any, options: MergeOptions): any => {
return {
version: 4
}
}) as MergeFunction | null
}
return null;
}
} as MergeOptions;
const baseConfig = {
version: 1,
features: {
analytics: true,
logging: false
},
endpoints: [
"https://api.example.com/v1"
],
specialKey: {
mode: "default",
retries: 3
}
}
const updateConfig = {
version: 2,
features: {
logging: true
},
endpoints: [
"https://api.example.com/v2"
],
specialKey: {
retries: 5,
timeout: 5000
}
}
const merged = merge(baseConfig, updateConfig, mergeOptions);
console.log(merged);
结语
友情推荐