From a19cc81391d85c96780cf936519358c3f0f9a358 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 5 Jan 2025 13:03:50 +0800 Subject: [PATCH] code refactor --- src/lib/common.ts | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/lib/common.ts b/src/lib/common.ts index 8a6bd473..5ddd11a5 100644 --- a/src/lib/common.ts +++ b/src/lib/common.ts @@ -243,9 +243,9 @@ export function stringToArrayBuffer(str: string): ArrayBuffer { return Uint8Array.from(str, c => c.charCodeAt(0)).buffer; } -export function getFirstVisibleItem(items: Record[] | Record>, hiddenField: string): unknown { - if (isArray(items) && (items as Record[]).length > 0) { - const arr = items as Record[]; +export function getFirstVisibleItem(items: Record[] | Record>, hiddenField: string): Record | null { + if (isArray(items) && (items as Record[]).length > 0) { + const arr = items as Record[]; for (let i = 0; i < arr.length; i++) { if (hiddenField && arr[i][hiddenField]) { @@ -255,7 +255,7 @@ export function getFirstVisibleItem(items: Record[] | Record>; + const obj = items as Record>; for (const field in obj) { if (!Object.prototype.hasOwnProperty.call(obj, field)) { @@ -273,9 +273,9 @@ export function getFirstVisibleItem(items: Record[] | Record[] | Record>, value: unknown, keyField: string) { +export function getItemByKeyValue (src: Record[] | Record>, value: T, keyField: string): Record | null { if (isArray(src)) { - const arr = src as Record[]; + const arr = src as Record[]; for (let i = 0; i < arr.length; i++) { const item = arr[i]; @@ -285,7 +285,7 @@ export function getItemByKeyValue(src: Record[] | Record>; + const obj = src as Record>; for (const field in obj) { if (!Object.prototype.hasOwnProperty.call(src, field)) { @@ -303,9 +303,9 @@ export function getItemByKeyValue(src: Record[] | Record[] | Record>, value: unknown, keyField: string, nameField: string, defaultName: string): unknown { +export function getNameByKeyValue(src: Record[] | Record>, value: T, keyField: string, nameField: string, defaultName: T): T { if (isArray(src)) { - const arr = src as Record[]; + const arr = src as Record[]; if (keyField) { for (let i = 0; i < arr.length; i++) { @@ -325,7 +325,7 @@ export function getNameByKeyValue(src: Record[] | Record>; + const obj = src as Record>; if (keyField) { for (const key in obj) { @@ -353,9 +353,9 @@ export function getNameByKeyValue(src: Record[] | Record, toObject: Record): object { +export function copyObjectTo(fromObject: Record | undefined, toObject: Record | undefined): Record { if (!isObject(fromObject)) { - return toObject; + return toObject as Record; } if (!isObject(toObject)) { @@ -368,23 +368,23 @@ export function copyObjectTo(fromObject: Record, toObject: Reco } const fromValue = fromObject[key]; - const toValue = toObject[key]; + const toValue = (toObject as Record)[key]; if (isArray(fromValue)) { - toObject[key] = copyArrayTo(fromValue as unknown[], toValue as unknown[]); + (toObject as Record)[key] = copyArrayTo(fromValue as unknown[], toValue as unknown[]); } else if (isObject(fromValue)) { - toObject[key] = copyObjectTo(fromValue as Record, toValue as Record); + (toObject as Record)[key] = copyObjectTo(fromValue as Record, toValue as Record); } else { if (fromValue !== toValue) { - toObject[key] = fromValue; + (toObject as Record)[key] = fromValue; } } } - return toObject; + return (toObject as Record); } -export function copyArrayTo(fromArray: unknown[], toArray: unknown[]) { +export function copyArrayTo(fromArray: unknown[], toArray: unknown[]): unknown[] { if (!isArray(fromArray)) { return toArray; } @@ -422,7 +422,7 @@ export function copyArrayTo(fromArray: unknown[], toArray: unknown[]) { return toArray; } -export function arrayContainsFieldValue(array: Record[], fieldName: string, value: unknown): boolean { +export function arrayContainsFieldValue(array: Record[], fieldName: string, value: T): boolean { if (!value || !array || !array.length) { return false; } @@ -450,8 +450,8 @@ export function objectFieldToArrayItem(object: object): string[] { return ret; } -export function arrayItemToObjectField(array: string[], value: unknown): Record { - const ret: Record = {}; +export function arrayItemToObjectField(array: string[], value: T): Record { + const ret: Record = {}; for (let i = 0; i < array.length; i++) { ret[array[i]] = value; @@ -460,8 +460,8 @@ export function arrayItemToObjectField(array: string[], value: unknown): Record< return ret; } -export function categorizedArrayToPlainArray(object: Record): unknown[] { - const ret: unknown[] = []; +export function categorizedArrayToPlainArray(object: Record): T[] { + const ret: T[] = []; for (const field in object) { if (!Object.prototype.hasOwnProperty.call(object, field)) { @@ -538,10 +538,10 @@ export function isPrimaryItemHasSecondaryValue(primaryItem: Record[]>[] | Record[]>>, primarySubItemsField: string, primaryValueField: string, primaryHiddenField: string, secondaryValueField: string, secondaryHiddenField: string, secondaryValue: unknown): unknown { +export function getPrimaryValueBySecondaryValue(items: Record[]>[] | Record[]>>, primarySubItemsField: string, primaryValueField: string, primaryHiddenField: string, secondaryValueField: string, secondaryHiddenField: string, secondaryValue: T): Record[] | Record[]> | null { if (primarySubItemsField) { if (isArray(items)) { - const arr = items as Record[]>[]; + const arr = items as Record[]>[]; for (let i = 0; i < arr.length; i++) { const primaryItem = arr[i]; @@ -559,7 +559,7 @@ export function getPrimaryValueBySecondaryValue(items: Record[]>>; + const obj = items as Record[]>>; for (const field in obj) { if (!Object.prototype.hasOwnProperty.call(obj, field)) { @@ -586,12 +586,12 @@ export function getPrimaryValueBySecondaryValue(items: Record(array: T[], startIndex: number): T[] { if (startIndex <= 0 || startIndex >= array.length) { return array; } - const newArray = []; + const newArray: T[] = []; for (let i = startIndex; i < array.length; i++) { newArray.push(array[i]);