Skip to content

Commit

Permalink
Merge pull request #397 from poap-xyz/array-intersection
Browse files Browse the repository at this point in the history
Simplify array intersection
  • Loading branch information
jm42 authored Aug 2, 2024
2 parents ba9c9b7 + 931c33e commit b6d1f23
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
33 changes: 18 additions & 15 deletions src/components/EventsCompare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)

Expand All @@ -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 &&
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/models/in-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
36 changes: 19 additions & 17 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
/**
* Returns elements found in all array arguments.
*/
export function intersection<T>(array: T[], ...args: T[][]): T[] {
if (array == null) {
export function intersection<T>(...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<T>()
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<T>(...args: T[][]): T[] {
const all = new Set<T>()

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)
}
} else {
all.add(arg)
}
}

return [...all]
}

Expand Down Expand Up @@ -69,6 +70,7 @@ export function equals(a: unknown[], b: unknown[]): boolean {
return false
}
}

return true
}

Expand Down

0 comments on commit b6d1f23

Please sign in to comment.