Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(form): 修复 formitem 的值如果是对象会自动重置为空对象的问题 #2952

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 83 additions & 23 deletions src/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,89 @@
export function merge(...objects: any[]) {
const result: any = Array.isArray(objects[0]) ? [] : {}

function mergeHelper(obj: any, path: string[] = []) {
for (const [key, value] of Object.entries(obj)) {
const newPath = [...path, key]

if (Array.isArray(value)) {
// Arrays are always overridden
result[key] = [...value]
} else if (typeof value === 'object' && value !== null) {
// Check for circular references
export default main

export function main(clone: boolean, ...items: any[]): any
export function main(...items: any[]): any
export function main(...items: any[]) {
return merge(...items)
}

oasis-cloud marked this conversation as resolved.
Show resolved Hide resolved
main.clone = clone
main.isPlainObject = isPlainObject
main.recursive = recursive

export function merge(clone: boolean, ...items: any[]): any
export function merge(...items: any[]): any
export function merge(...items: any[]) {
return _merge(items[0] === true, false, items)
}

export function recursive(clone: boolean, ...items: any[]): any
export function recursive(...items: any[]): any
export function recursive(...items: any[]) {
return _merge(items[0] === true, true, items)
}

export function clone<T>(input: T): T {
if (Array.isArray(input)) {
const output = []

for (let index = 0; index < input.length; ++index)
output.push(clone(input[index]))

return output as any
}
if (isPlainObject(input)) {
const output: any = {}

// eslint-disable-next-line guard-for-in
for (const index in input) output[index] = clone((input as any)[index])

return output as any
}
return input
}

export function isPlainObject(input: unknown): input is NonNullable<any> {
if (input === null || typeof input !== 'object') return false
if (Object.getPrototypeOf(input) === null) return true
let ref = input
while (Object.getPrototypeOf(ref) !== null) ref = Object.getPrototypeOf(ref)
return Object.getPrototypeOf(input) === ref
}

function _recursiveMerge(base: any, extend: any) {
if (!isPlainObject(base) || !isPlainObject(extend)) return extend
for (const key in extend) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype')
// eslint-disable-next-line no-continue
continue
base[key] =
isPlainObject(base[key]) && isPlainObject(extend[key])
? _recursiveMerge(base[key], extend[key])
: extend[key]
}

return base
}

function _merge(isClone: boolean, isRecursive: boolean, items: any[]) {
let result

if (isClone || !isPlainObject((result = items.shift()))) result = {}

oasis-cloud marked this conversation as resolved.
Show resolved Hide resolved
for (let index = 0; index < items.length; ++index) {
const item = items[index]

// eslint-disable-next-line no-continue
if (!isPlainObject(item)) continue

for (const key in item) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype')
// eslint-disable-next-line no-continue
if (path.some((p: any) => p === value)) continue

// Recursively merge objects
if (!result[key]) result[key] = {}
mergeHelper(value, newPath)
} else {
// Set primitive values
result[key] = value
}
continue
const value = isClone ? clone(item[key]) : item[key]
result[key] = isRecursive ? _recursiveMerge(result[key], value) : value
}
}

objects.filter((obj) => !!obj).forEach((obj) => mergeHelper(obj))

return result
}
Loading