From b245a399f750968bc1d5c1e13e49b439d74c08d7 Mon Sep 17 00:00:00 2001 From: Benedikt Mehl Date: Thu, 30 Jan 2025 14:19:12 +0100 Subject: [PATCH] Add compare array helper #3900 --- .../app/codeCharta/util/arrayHelper.spec.ts | 85 ++++++++++++++++++- .../app/codeCharta/util/arrayHelper.ts | 29 ++++++- 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/visualization/app/codeCharta/util/arrayHelper.spec.ts b/visualization/app/codeCharta/util/arrayHelper.spec.ts index 7935883e67..eb7362488d 100644 --- a/visualization/app/codeCharta/util/arrayHelper.spec.ts +++ b/visualization/app/codeCharta/util/arrayHelper.spec.ts @@ -1,4 +1,4 @@ -import { addItemToArray, removeItemFromArray } from "./arrayHelper" +import { addItemToArray, compareContent, removeItemFromArray } from "./arrayHelper" function mutateObject(object: Record) { object.x = 10_000 @@ -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) + }) + }) }) diff --git a/visualization/app/codeCharta/util/arrayHelper.ts b/visualization/app/codeCharta/util/arrayHelper.ts index 25dbb452fd..1e92abe73b 100644 --- a/visualization/app/codeCharta/util/arrayHelper.ts +++ b/visualization/app/codeCharta/util/arrayHelper.ts @@ -5,18 +5,18 @@ export function removeItemFromArray(array: T[], searchItem: T) { return array.filter(entry => !dequal(entry, searchItem)) } -export function removeEntryAtIndexFromArray(array: T[], index: number) { +export function removeEntryAtIndexFromArray(array: T[], index: number): T[] { return [...array.slice(0, index), ...array.slice(index + 1)] } -export function addItemToArray(array: T[], item: T) { +export function addItemToArray(array: T[], item: T): T[] { if (!arrayContainsItem(array, item)) { return [...array, clone(item)] } return array } -export function addItemsToArray(array: T[], items: T[]) { +export function addItemsToArray(array: T[], items: T[]): T[] { const newArray = [...array] for (const item of items) { if (!arrayContainsItem(newArray, item)) { @@ -26,6 +26,29 @@ export function addItemsToArray(array: T[], items: T[]) { return newArray } +export function compareContent(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(array: T[], item: T): number { + return array.findIndex(x => dequal(x, item)) +} + function arrayContainsItem(array: T[], item: T) { return array.some(x => dequal(x, item)) }