From 931c33eca49972ba1d4012682db12ce47fb89c1e Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 2 Aug 2024 07:30:16 -0300 Subject: [PATCH] Simplify array intersection --- src/components/EventsCompare.tsx | 33 ++++++++++++++++------------- src/models/in-common.ts | 4 ++-- src/utils/array.ts | 36 +++++++++++++++++--------------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/components/EventsCompare.tsx b/src/components/EventsCompare.tsx index 0efc4c6..ed44f2c 100644 --- a/src/components/EventsCompare.tsx +++ b/src/components/EventsCompare.tsx @@ -40,17 +40,16 @@ function EventsCompare({ () => eventIds.length < 2 ? {} : Object.fromEntries( - intersection( - // @ts-ignore - ...eventIds.map((eventId) => inCommon[eventId]) - ) - .map( - (address) => [ - address, - getColorForSeed(address), - ] - ) - ), + intersection( + ...eventIds.map((eventId) => inCommon[eventId]) + ) + .map( + (address) => [ + address, + getColorForSeed(address), + ] + ) + ), [eventIds, inCommon] ) @@ -75,9 +74,11 @@ function EventsCompare({ ) function onOwnerEnter(ownerEventId: number, owner: string): void { - if (owner in adressesColors && + if ( + owner in adressesColors && settings && - settings.autoScrollCollectors) { + settings.autoScrollCollectors + ) { for (const eventId of eventIds) { if (eventId !== ownerEventId && eventId && liRefs && @@ -89,9 +90,11 @@ function EventsCompare({ }) } } - if (ownerEventId in liRefs && + if ( + ownerEventId in liRefs && owner in liRefs[ownerEventId] && - liRefs[ownerEventId][owner].current) { + liRefs[ownerEventId][owner].current + ) { liRefs[ownerEventId][owner].current.scrollIntoView({ behavior: 'smooth', block: 'center', diff --git a/src/models/in-common.ts b/src/models/in-common.ts index 4e52b14..7fdee0d 100644 --- a/src/models/in-common.ts +++ b/src/models/in-common.ts @@ -72,7 +72,7 @@ export function mergeAllInCommon( * Merges in-common collectors that belong to all events. */ export function mergeAddressesInCommon(inCommon: InCommon): string[] { - let mergedAddresses = null + let mergedAddresses: string[] | null = null for (const [, addresses] of Object.entries(inCommon)) { if (mergedAddresses == null) { mergedAddresses = addresses @@ -91,7 +91,7 @@ export function getAddressInCommonEventIds( inCommon: InCommon, address: string, ): number[] { - const eventIds = [] + const eventIds: number[] = [] for (const [rawEventId, addresses] of Object.entries(inCommon)) { if (addresses.indexOf(address) !== -1) { eventIds.push(parseInt(rawEventId)) diff --git a/src/utils/array.ts b/src/utils/array.ts index 02fcfb8..d23bcfa 100644 --- a/src/utils/array.ts +++ b/src/utils/array.ts @@ -1,35 +1,35 @@ /** * Returns elements found in all array arguments. */ -export function intersection(array: T[], ...args: T[][]): T[] { - if (array == null) { +export function intersection(...args: T[][]): T[] { + if (args.length === 0 || args[0].length === 0) { return [] } - const result = [] - for (var i = 0, length = array.length; i < length; i++) { - const item = array[i] - if (result.indexOf(item) !== -1) { - continue - } - let j: number - for (j = 0; j < args.length; j++) { - if (args[j].indexOf(item) === -1) { - break - } - } - if (j === args.length - 1) { - result.push(item) + + const all = new Set() + const tail = args.slice(1) + + for (const item of args[0]) { + if (tail.every((other) => other.includes(item))) { + all.add(item) } } - return result + + return [...all] } +/** + * Returns elements in all array arguments. + */ export function union(...args: T[][]): T[] { const all = new Set() + for (let i = 0; i < args.length; i++) { const arg = args[i] + if (Array.isArray(arg)) { const array: T[] = [].concat(...arg) + for (const item of array) { all.add(item) } @@ -37,6 +37,7 @@ export function union(...args: T[][]): T[] { all.add(arg) } } + return [...all] } @@ -69,6 +70,7 @@ export function equals(a: unknown[], b: unknown[]): boolean { return false } } + return true }