RenaiApp/src/renderer/services/util/compare.ts

53 lines
1.5 KiB
TypeScript

/**
* @return true if elements in the arrays are the same (strict comparison), including their order
*/
export function equalArray(arr1: unknown[], arr2: unknown[]): boolean {
return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
}
/**
* @return true if the entities have the same ids
*/
export function equalEntity(
e1: IdentifiableInterface<number | string> | null,
e2: IdentifiableInterface<number | string> | null,
): boolean {
if (e1 && e2) {
return e1.id === e2.id;
}
return !e1 && !e2;
}
/**
* @return true if the entities in the arrays have the same ids (and same order)
*/
export function equalEntities(
arr1: Array<IdentifiableInterface<number | string>>,
arr2: Array<IdentifiableInterface<number | string>>,
): boolean {
return equalArray(
arr1.map((e) => e.id),
arr2.map((e) => e.id),
);
}
/**
* @return true if the promised entities have the same ids
*/
export async function equalEntityPromise(
e1: Promise<IdentifiableInterface<number | string>> | null,
e2: Promise<IdentifiableInterface<number | string>> | null,
): Promise<boolean> {
return equalEntity(await e1, await e2);
}
/**
* @return true if the promised entities in the arrays have the same ids (and same order)
*/
export async function equalEntitiesPromise(
arr1: Promise<Array<IdentifiableInterface<number | string>>>,
arr2: Promise<Array<IdentifiableInterface<number | string>>>,
): Promise<boolean> {
return equalEntities(await arr1, await arr2);
}