code refactor

This commit is contained in:
MaysWind
2025-02-12 23:26:03 +08:00
parent 30575d15d0
commit 0b5721671d
6 changed files with 11 additions and 85 deletions
-69
View File
@@ -402,75 +402,6 @@ export function getNameByKeyValue<V, N>(src: Record<string, N | V>[] | Record<st
return defaultName;
}
export function copyObjectTo(fromObject: Record<string, unknown> | undefined, toObject: Record<string, unknown> | undefined): Record<string, unknown> {
if (!isObject(fromObject)) {
return toObject as Record<string, unknown>;
}
if (!isObject(toObject)) {
toObject = {};
}
for (const key in fromObject) {
if (!Object.prototype.hasOwnProperty.call(fromObject, key)) {
continue;
}
const fromValue = fromObject[key];
const toValue = toObject[key];
if (isArray(fromValue)) {
toObject[key] = copyArrayTo(fromValue, toValue as unknown[]);
} else if (isObject(fromValue)) {
toObject[key] = copyObjectTo(fromValue as Record<string, unknown>, toValue as Record<string, unknown>);
} else {
if (fromValue !== toValue) {
toObject[key] = fromValue;
}
}
}
return toObject;
}
export function copyArrayTo<T>(fromArray: T[], toArray: T[]): T[] {
if (!isArray(fromArray)) {
return toArray;
}
if (!isArray(toArray)) {
toArray = [];
}
for (let i = 0; i < fromArray.length; i++) {
const fromValue = fromArray[i];
if (toArray.length > i) {
const toValue = toArray[i];
if (isArray(fromValue)) {
toArray[i] = copyArrayTo(fromValue as unknown[], toValue as unknown[]) as T;
} else if (isObject(fromValue)) {
toArray[i] = copyObjectTo(fromValue as Record<string, unknown>, toValue as Record<string, unknown>) as T;
} else {
if (fromValue !== toValue) {
toArray[i] = fromValue;
}
}
} else {
if (isArray(fromValue)) {
toArray.push(copyArrayTo(fromValue as unknown[], []) as T);
} else if (isObject(fromValue)) {
toArray.push(copyObjectTo(fromValue as Record<string, unknown>, {}) as T);
} else {
toArray.push(fromValue);
}
}
}
return toArray;
}
export function arrayContainsFieldValue<T>(array: Record<string, T>[], fieldName: string, value: T): boolean {
if (!value || !array || !array.length) {
return false;