From ee7912eb78593af6243e6aef2bb3ac9d2d00329a Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Wed, 18 Nov 2026 12:53:33 +0100 Subject: [PATCH] refactor: move array equality utils into @monkeytype/util --- .../ts/components/modals/CommandlineModal.tsx | 3 +-- .../components/modals/EditResultTagsModal.tsx | 2 +- .../modals/MobileTestConfigModal.tsx | 2 +- .../custom-setting/CustomLayoutfluid.tsx | 2 +- .../custom-setting/CustomPolyglot.tsx | 2 +- .../ts/components/pages/test/TestConfig.tsx | 2 +- frontend/src/ts/components/ui/SlimSelect.tsx | 3 +-- frontend/src/ts/config/metadata.tsx | 2 +- .../ts/controllers/challenge-controller.ts | 2 +- .../src/ts/controllers/theme-controller.ts | 3 ++- frontend/src/ts/utils/arrays.ts | 23 ------------------- packages/util/src/arrays.ts | 23 +++++++++++++++++++ 12 files changed, 34 insertions(+), 35 deletions(-) diff --git a/frontend/src/ts/components/modals/CommandlineModal.tsx b/frontend/src/ts/components/modals/CommandlineModal.tsx index a5f85c284753..4fbea4c16c93 100644 --- a/frontend/src/ts/components/modals/CommandlineModal.tsx +++ b/frontend/src/ts/components/modals/CommandlineModal.tsx @@ -1,4 +1,4 @@ -import { intersect } from "@monkeytype/util/arrays"; +import { areUnsortedArraysEqual, intersect } from "@monkeytype/util/arrays"; import { For, JSXElement, @@ -42,7 +42,6 @@ import { } from "../../states/test"; import { FaObject } from "../../types/font-awesome"; import { ValidationResult } from "../../types/validation"; -import { areUnsortedArraysEqual } from "../../utils/arrays"; import { cn } from "../../utils/cn"; import { AnimatedModal } from "../common/AnimatedModal"; import { Fa } from "../common/Fa"; diff --git a/frontend/src/ts/components/modals/EditResultTagsModal.tsx b/frontend/src/ts/components/modals/EditResultTagsModal.tsx index 8f8021e221fe..00e87982cdfc 100644 --- a/frontend/src/ts/components/modals/EditResultTagsModal.tsx +++ b/frontend/src/ts/components/modals/EditResultTagsModal.tsx @@ -1,3 +1,4 @@ +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import { createEffect, createSignal, For } from "solid-js"; import { updateTags } from "../../collections/results"; @@ -10,7 +11,6 @@ import { showSuccessNotification, } from "../../states/notifications"; import { setResultTagsAfterEdit } from "../../states/result"; -import { areUnsortedArraysEqual } from "../../utils/arrays"; import { createErrorMessage } from "../../utils/error"; import { AnimatedModal } from "../common/AnimatedModal"; import { Button } from "../common/Button"; diff --git a/frontend/src/ts/components/modals/MobileTestConfigModal.tsx b/frontend/src/ts/components/modals/MobileTestConfigModal.tsx index 1b2a5eb7a742..5de39632a8d8 100644 --- a/frontend/src/ts/components/modals/MobileTestConfigModal.tsx +++ b/frontend/src/ts/components/modals/MobileTestConfigModal.tsx @@ -4,6 +4,7 @@ import type { } from "@monkeytype/schemas/configs"; import type { Mode } from "@monkeytype/schemas/shared"; +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import { For, JSXElement, Show } from "solid-js"; import { setConfig, setQuoteLengthAll } from "../../config/setters"; @@ -11,7 +12,6 @@ import { getConfig } from "../../config/store"; import { isAuthenticated } from "../../states/core"; import { showModal } from "../../states/modals"; import { restartTest } from "../../test/test-logic"; -import { areUnsortedArraysEqual } from "../../utils/arrays"; import { AnimatedModal } from "../common/AnimatedModal"; import { Button } from "../common/Button"; import { Separator } from "../common/Separator"; diff --git a/frontend/src/ts/components/pages/settings/custom-setting/CustomLayoutfluid.tsx b/frontend/src/ts/components/pages/settings/custom-setting/CustomLayoutfluid.tsx index 30a13f93e9fb..51ec9d3954be 100644 --- a/frontend/src/ts/components/pages/settings/custom-setting/CustomLayoutfluid.tsx +++ b/frontend/src/ts/components/pages/settings/custom-setting/CustomLayoutfluid.tsx @@ -1,11 +1,11 @@ import { CustomLayoutFluid } from "@monkeytype/schemas/configs"; +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import { JSXElement } from "solid-js"; import { configMetadata } from "../../../../config/metadata"; import { setConfig } from "../../../../config/setters"; import { getConfig } from "../../../../config/store"; import { LayoutsList } from "../../../../constants/layouts"; -import { areUnsortedArraysEqual } from "../../../../utils/arrays"; import SlimSelect from "../../../ui/SlimSelect"; import { SearchableSetting } from "../SearchableSetting"; diff --git a/frontend/src/ts/components/pages/settings/custom-setting/CustomPolyglot.tsx b/frontend/src/ts/components/pages/settings/custom-setting/CustomPolyglot.tsx index 2affd7aaeb0b..466a0920c395 100644 --- a/frontend/src/ts/components/pages/settings/custom-setting/CustomPolyglot.tsx +++ b/frontend/src/ts/components/pages/settings/custom-setting/CustomPolyglot.tsx @@ -1,4 +1,5 @@ import { CustomPolyglot as CustomPolyglotType } from "@monkeytype/schemas/configs"; +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import { Optgroup } from "slim-select/store"; import { JSXElement } from "solid-js"; @@ -9,7 +10,6 @@ import { LanguageGroupNames, LanguageGroups, } from "../../../../constants/languages"; -import { areUnsortedArraysEqual } from "../../../../utils/arrays"; import { getLanguageDisplayString } from "../../../../utils/strings"; import SlimSelect from "../../../ui/SlimSelect"; import { SearchableSetting } from "../SearchableSetting"; diff --git a/frontend/src/ts/components/pages/test/TestConfig.tsx b/frontend/src/ts/components/pages/test/TestConfig.tsx index c6a133b10524..a8f728572b5c 100644 --- a/frontend/src/ts/components/pages/test/TestConfig.tsx +++ b/frontend/src/ts/components/pages/test/TestConfig.tsx @@ -1,3 +1,4 @@ +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import { ComponentProps, For, JSXElement, Show } from "solid-js"; import { configMetadata } from "../../../config/metadata"; @@ -14,7 +15,6 @@ import { } from "../../../states/test"; import { restartTest } from "../../../test/test-logic"; import { FaObject } from "../../../types/font-awesome"; -import { areUnsortedArraysEqual } from "../../../utils/arrays"; import { cn } from "../../../utils/cn"; import { Anime } from "../../common/anime"; import { Button } from "../../common/Button"; diff --git a/frontend/src/ts/components/ui/SlimSelect.tsx b/frontend/src/ts/components/ui/SlimSelect.tsx index 41fc23668264..dac7a74bd246 100644 --- a/frontend/src/ts/components/ui/SlimSelect.tsx +++ b/frontend/src/ts/components/ui/SlimSelect.tsx @@ -1,11 +1,10 @@ import type { JSX, JSXElement } from "solid-js"; +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import SlimSelectCore, { Config } from "slim-select"; import { Optgroup, Option } from "slim-select/store"; import { onMount, onCleanup, createEffect, createSignal } from "solid-js"; -import { areUnsortedArraysEqual } from "../../utils/arrays"; - // Helper: Update SlimSelect data and trigger renders function updateSlimSelectData( slimSelect: SlimSelectCore, diff --git a/frontend/src/ts/config/metadata.tsx b/frontend/src/ts/config/metadata.tsx index ba7003e0b2c4..7e6faf38ee77 100644 --- a/frontend/src/ts/config/metadata.tsx +++ b/frontend/src/ts/config/metadata.tsx @@ -1,6 +1,7 @@ import { checkCompatibility } from "@monkeytype/funbox"; import * as ConfigSchemas from "@monkeytype/schemas/configs"; import { KnownFontName } from "@monkeytype/schemas/fonts"; +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; import { roundTo1 } from "@monkeytype/util/numbers"; import { typedKeys } from "@monkeytype/util/objects"; import { JSXElement } from "solid-js"; @@ -13,7 +14,6 @@ import { showNoticeNotification } from "../states/notifications"; import { setRandomTheme } from "../states/theme"; import { FaObject } from "../types/font-awesome"; import { Validation } from "../types/validation"; -import { areUnsortedArraysEqual } from "../utils/arrays"; import { isDevEnvironment } from "../utils/env"; import { reloadAfter } from "../utils/misc"; import { capitalizeFirstLetter } from "../utils/strings"; diff --git a/frontend/src/ts/controllers/challenge-controller.ts b/frontend/src/ts/controllers/challenge-controller.ts index c0bc00f52762..de2e64a29139 100644 --- a/frontend/src/ts/controllers/challenge-controller.ts +++ b/frontend/src/ts/controllers/challenge-controller.ts @@ -21,7 +21,7 @@ import { getLoadedChallenge, setLoadedChallenge, } from "../states/test"; -import { areUnsortedArraysEqual } from "../utils/arrays"; +import { areUnsortedArraysEqual } from "@monkeytype/util/arrays"; let challengeLoading = false; diff --git a/frontend/src/ts/controllers/theme-controller.ts b/frontend/src/ts/controllers/theme-controller.ts index e1ef978fecfe..3c1d091555a7 100644 --- a/frontend/src/ts/controllers/theme-controller.ts +++ b/frontend/src/ts/controllers/theme-controller.ts @@ -9,6 +9,7 @@ import { getCustomTheme, getCustomThemes } from "../queries/custom-themes"; import { showNoticeNotification } from "../states/notifications"; import { debounce } from "throttle-debounce"; import { CustomThemeColors, ThemeName } from "@monkeytype/schemas/configs"; +import { areSortedArraysEqual } from "@monkeytype/util/arrays"; import { Theme, themes, ThemesList } from "../constants/themes"; import { setThemeIndicator } from "../states/core"; import { @@ -90,7 +91,7 @@ function updateThemeIndicator(nameOverride?: string): void { if (Config.customTheme && nameOverride === undefined) { // Match current custom theme by colors since Config does not store custom theme IDs const matchedTheme = getCustomThemes().find((ct) => - Arrays.areSortedArraysEqual(ct.colors, Config.customThemeColors), + areSortedArraysEqual(ct.colors, Config.customThemeColors), ); if (matchedTheme) { diff --git a/frontend/src/ts/utils/arrays.ts b/frontend/src/ts/utils/arrays.ts index dbf4129f7d8d..ae8309640905 100644 --- a/frontend/src/ts/utils/arrays.ts +++ b/frontend/src/ts/utils/arrays.ts @@ -101,29 +101,6 @@ export function lastElementFromArray(array: T[]): T | undefined { return array[array.length - 1]; } -/** - * Checks if two unsorted arrays are equal, i.e., they have the same elements regardless of order. - * @param a The first array. - * @param b The second array. - * @returns True if the arrays are equal, false otherwise. - */ -export function areUnsortedArraysEqual(a: T[], b: T[]): boolean { - return a.length === b.length && a.every((v) => b.includes(v)); -} - -/** - * Checks if two sorted arrays are equal, i.e., they have the same elements in the same order. - * @param a The first array. - * @param b The second array. - * @returns True if the arrays are equal, false otherwise. - */ -export function areSortedArraysEqual( - a: readonly T[], - b: readonly T[], -): boolean { - return a.length === b.length && a.every((v, i) => v === b[i]); -} - /** * Returns a random element from an array. * @param array The input array. diff --git a/packages/util/src/arrays.ts b/packages/util/src/arrays.ts index 10c6c62320c5..4685a2118c4d 100644 --- a/packages/util/src/arrays.ts +++ b/packages/util/src/arrays.ts @@ -13,3 +13,26 @@ export function intersect(a: T[], b: T[], removeDuplicates = false): T[] { }); return removeDuplicates ? [...new Set(filtered)] : filtered; } + +/** + * Checks if two unsorted arrays are equal, i.e., they have the same elements regardless of order. + * @param a The first array. + * @param b The second array. + * @returns True if the arrays are equal, false otherwise. + */ +export function areUnsortedArraysEqual(a: T[], b: T[]): boolean { + return a.length === b.length && a.every((v) => b.includes(v)); +} + +/** + * Checks if two sorted arrays are equal, i.e., they have the same elements in the same order. + * @param a The first array. + * @param b The second array. + * @returns True if the arrays are equal, false otherwise. + */ +export function areSortedArraysEqual( + a: readonly T[], + b: readonly T[], +): boolean { + return a.length === b.length && a.every((v, i) => v === b[i]); +}