Skip to content

Commit

Permalink
Add compare array helper #3900
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktMehl committed Feb 3, 2025
1 parent f60569f commit b245a39
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
85 changes: 84 additions & 1 deletion visualization/app/codeCharta/util/arrayHelper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addItemToArray, removeItemFromArray } from "./arrayHelper"
import { addItemToArray, compareContent, removeItemFromArray } from "./arrayHelper"

function mutateObject(object: Record<string, number>) {
object.x = 10_000
Expand Down Expand Up @@ -40,4 +40,87 @@ describe("arrayHelper", () => {
])
})
})

describe("compareContent", () => {
it("should return true for arrays with the same contents", () => {
const array1 = [
{ x: 1, y: 2 },
{ x: 3, y: 4 }
]
const array2 = [
{ x: 1, y: 2 },
{ x: 3, y: 4 }
]

const result = compareContent(array1, array2)

expect(result).toBe(true)
})

it("should return false for arrays with different contents", () => {
const array1 = [{ x: 1, y: 2 }]
const array2 = [{ x: 3, y: 4 }]

const result = compareContent(array1, array2)

expect(result).toBe(false)
})

it("should return true for arrays with the same contents in different orders", () => {
const array1 = [
{ x: 3, y: 4 },
{ x: 1, y: 2 }
]
const array2 = [
{ x: 1, y: 2 },
{ x: 3, y: 4 }
]

const result = compareContent(array1, array2)

expect(result).toBe(true)
})

it("should return false for arrays with different length", () => {
const array1 = [{ x: 3, y: 4 }]
const array2 = [
{ x: 3, y: 4 },
{ x: 3, y: 4 }
]

const result1 = compareContent(array1, array2)
const result2 = compareContent(array2, array1)

expect(result1).toBe(false)
expect(result2).toBe(false)
})

it("should return true for empty arrays", () => {
const array1 = []
const array2 = []

const result = compareContent(array1, array2)

expect(result).toBe(true)
})

it("should return false for arrays containing duplicate elements", () => {
const array1 = [
{ x: 1, y: 2 },
{ x: 1, y: 2 },
{ x: 1, y: 3 }
]
const array2 = [
{ x: 1, y: 2 },
{ x: 1, y: 3 },
{ x: 1, y: 3 }
]

const result1 = compareContent(array1, array2)
const result2 = compareContent(array2, array1)

expect(result1).toBe(false)
expect(result2).toBe(false)
})
})
})
29 changes: 26 additions & 3 deletions visualization/app/codeCharta/util/arrayHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ export function removeItemFromArray<T>(array: T[], searchItem: T) {
return array.filter(entry => !dequal(entry, searchItem))
}

export function removeEntryAtIndexFromArray<T>(array: T[], index: number) {
export function removeEntryAtIndexFromArray<T>(array: T[], index: number): T[] {
return [...array.slice(0, index), ...array.slice(index + 1)]
}

export function addItemToArray<T>(array: T[], item: T) {
export function addItemToArray<T>(array: T[], item: T): T[] {
if (!arrayContainsItem(array, item)) {
return [...array, clone(item)]
}
return array
}

export function addItemsToArray<T>(array: T[], items: T[]) {
export function addItemsToArray<T>(array: T[], items: T[]): T[] {
const newArray = [...array]
for (const item of items) {
if (!arrayContainsItem(newArray, item)) {
Expand All @@ -26,6 +26,29 @@ export function addItemsToArray<T>(array: T[], items: T[]) {
return newArray
}

export function compareContent<T>(array1: T[], array2: T[]): boolean {
if (array1.length !== array2.length) {
return false
}

let clonedArray2 = [...array2]

return array1.every(item => {
const index = findIndexOfItemInArray(clonedArray2, item)

if (index >= 0) {
clonedArray2 = removeEntryAtIndexFromArray(clonedArray2, index)
return true
}

return false
})
}

function findIndexOfItemInArray<T>(array: T[], item: T): number {
return array.findIndex(x => dequal(x, item))
}

function arrayContainsItem<T>(array: T[], item: T) {
return array.some(x => dequal(x, item))
}

0 comments on commit b245a39

Please sign in to comment.