prepend and append array

This commit is contained in:
pompurin404 2024-08-30 17:01:33 +08:00
parent 344534e4fc
commit e503095bae
No known key found for this signature in database
3 changed files with 25 additions and 12 deletions

View File

@ -1,5 +1,7 @@
### Bug Fixes
### Break Changes
- 修复便携模式无法启动的问题
- 尝试修复Windows系统代理无法打开的问题
- 优化Windows程序拉起方式
- YAML覆写语法有所变动请更新后参考文档进行修改
### New Features
- YAML覆写功能支持对数组进行覆盖/前置/追加操作

View File

@ -35,12 +35,6 @@ async function overrideProfile(
break
case 'yaml': {
const patch = yaml.parse(content)
if (patch.rules) {
patch.rules = [...patch.rules, ...(profile.rules || [])]
}
if (patch.proxies) {
patch.proxies = [...patch.proxies, ...(profile.proxies || [])]
}
profile = deepMerge(profile, patch)
break
}

View File

@ -6,8 +6,25 @@ function isObject(item: any): boolean {
export function deepMerge<T extends object>(target: T, other: Partial<T>): T {
for (const key in other) {
if (isObject(other[key])) {
if (key.endsWith('!')) {
const k = key.slice(0, -1)
target[k] = other[key]
} else {
if (!target[key]) Object.assign(target, { [key]: {} })
deepMerge(target[key] as object, other[key] as object)
}
} else if (Array.isArray(other[key])) {
if (key.startsWith('+')) {
const k = key.slice(1)
if (!target[k]) Object.assign(target, { [k]: [] })
target[k] = [...other[key], ...(target[k] as never[])]
} else if (key.endsWith('+')) {
const k = key.slice(0, -1)
if (!target[k]) Object.assign(target, { [k]: [] })
target[k] = [...(target[k] as never[]), ...other[key]]
} else {
Object.assign(target, { [key]: other[key] })
}
} else {
Object.assign(target, { [key]: other[key] })
}