mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-15 23:47:33 +08:00
code refactor
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user