From b80129760e3a040015e6f476eec1f4d5becba8ce Mon Sep 17 00:00:00 2001 From: Miodec Date: Wed, 18 Sep 2024 11:43:37 +0200 Subject: [PATCH] arrays --- backend/src/utils/misc.ts | 10 ---- backend/src/utils/validation.ts | 2 +- .../src/ts/test/funbox/funbox-validation.ts | 6 +-- frontend/src/ts/utils/arrays.ts | 16 ------- packages/util/__test__/arrays.spec.ts | 48 +++++++++++++++++++ packages/util/src/arrays.ts | 15 ++++++ packages/util/src/index.ts | 3 -- packages/util/tsconfig.json | 3 +- 8 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 packages/util/__test__/arrays.spec.ts create mode 100644 packages/util/src/arrays.ts delete mode 100644 packages/util/src/index.ts diff --git a/backend/src/utils/misc.ts b/backend/src/utils/misc.ts index 4ffac9d14..a709242ab 100644 --- a/backend/src/utils/misc.ts +++ b/backend/src/utils/misc.ts @@ -187,16 +187,6 @@ export function formatSeconds( return `${normalized} ${unit}${normalized > 1 ? "s" : ""}`; } -export function intersect(a: T[], b: T[], removeDuplicates = false): T[] { - let t: T[]; - // eslint-disable-next-line @typescript-eslint/no-unused-expressions - if (b.length > a.length) (t = b), (b = a), (a = t); // indexOf to loop over shorter - const filtered = a.filter(function (e) { - return b.includes(e); - }); - return removeDuplicates ? [...new Set(filtered)] : filtered; -} - export function isDevEnvironment(): boolean { return process.env["MODE"] === "dev"; } diff --git a/backend/src/utils/validation.ts b/backend/src/utils/validation.ts index 0cb56a2b7..a36ea508c 100644 --- a/backend/src/utils/validation.ts +++ b/backend/src/utils/validation.ts @@ -1,7 +1,7 @@ import _ from "lodash"; -import { intersect } from "./misc"; import { default as FunboxList } from "../constants/funbox-list"; import { CompletedEvent } from "@monkeytype/contracts/schemas/results"; +import { intersect } from "@monkeytype/util/arrays"; export function isTestTooShort(result: CompletedEvent): boolean { const { mode, mode2, customText, testDuration, bailedOut } = result; diff --git a/frontend/src/ts/test/funbox/funbox-validation.ts b/frontend/src/ts/test/funbox/funbox-validation.ts index 11dfac309..9a9f37033 100644 --- a/frontend/src/ts/test/funbox/funbox-validation.ts +++ b/frontend/src/ts/test/funbox/funbox-validation.ts @@ -1,8 +1,8 @@ import * as FunboxList from "./funbox-list"; import * as Notifications from "../../elements/notifications"; -import * as Arrays from "../../utils/arrays"; import * as Strings from "../../utils/strings"; import { Config, ConfigValue } from "@monkeytype/contracts/schemas/configs"; +import { intersect } from "@monkeytype/util/arrays"; export function checkFunboxForcedConfigs( key: string, @@ -43,7 +43,7 @@ export function checkFunboxForcedConfigs( if (forcedConfigs[key] === undefined) { forcedConfigs[key] = fb.forcedConfig[key] as ConfigValue[]; } else { - forcedConfigs[key] = Arrays.intersect( + forcedConfigs[key] = intersect( forcedConfigs[key], fb.forcedConfig[key] as ConfigValue[], true @@ -302,7 +302,7 @@ export function areFunboxesCompatible( for (const key in f.forcedConfig) { if (allowedConfig[key]) { if ( - Arrays.intersect( + intersect( allowedConfig[key], f.forcedConfig[key] as ConfigValue[], true diff --git a/frontend/src/ts/utils/arrays.ts b/frontend/src/ts/utils/arrays.ts index 65559b75d..f7129181d 100644 --- a/frontend/src/ts/utils/arrays.ts +++ b/frontend/src/ts/utils/arrays.ts @@ -99,19 +99,3 @@ export function nthElementFromArray( index = index < 0 ? array.length + index : index; return array[index]; } - -/** - * Returns the intersection of two arrays, i.e., the elements that are present in both arrays. - * @param a First array. - * @param b Second array. - * @returns An array containing the elements that are present in both input arrays. - */ -export function intersect(a: T[], b: T[], removeDuplicates = false): T[] { - let t; - // eslint-disable-next-line @typescript-eslint/no-unused-expressions - if (b.length > a.length) (t = b), (b = a), (a = t); // indexOf to loop over shorter - const filtered = a.filter(function (e) { - return b.includes(e); - }); - return removeDuplicates ? [...new Set(filtered)] : filtered; -} diff --git a/packages/util/__test__/arrays.spec.ts b/packages/util/__test__/arrays.spec.ts new file mode 100644 index 000000000..2fbb64d10 --- /dev/null +++ b/packages/util/__test__/arrays.spec.ts @@ -0,0 +1,48 @@ +import * as Arrays from "../src/arrays"; + +describe("arrays", () => { + it("intersect", () => { + const testCases = [ + { + a: [1], + b: [2], + removeDuplicates: false, + expected: [], + }, + { + a: [1], + b: [1], + removeDuplicates: false, + expected: [1], + }, + { + a: [1, 1], + b: [1], + removeDuplicates: true, + expected: [1], + }, + { + a: [1, 1], + b: [1], + removeDuplicates: false, + expected: [1, 1], + }, + { + a: [1], + b: [1, 2, 3], + removeDuplicates: false, + expected: [1], + }, + { + a: [1, 1], + b: [1, 2, 3], + removeDuplicates: true, + expected: [1], + }, + ]; + + testCases.forEach(({ a, b, removeDuplicates, expected }) => { + expect(Arrays.intersect(a, b, removeDuplicates)).toEqual(expected); + }); + }); +}); diff --git a/packages/util/src/arrays.ts b/packages/util/src/arrays.ts new file mode 100644 index 000000000..95349f66d --- /dev/null +++ b/packages/util/src/arrays.ts @@ -0,0 +1,15 @@ +/** + * Returns the intersection of two arrays, i.e., the elements that are present in both arrays. + * @param a First array. + * @param b Second array. + * @returns An array containing the elements that are present in both input arrays. + */ +export function intersect(a: T[], b: T[], removeDuplicates = false): T[] { + let t; + // eslint-disable-next-line @typescript-eslint/no-unused-expressions + if (b.length > a.length) (t = b), (b = a), (a = t); // indexOf to loop over shorter + const filtered = a.filter(function (e) { + return b.includes(e); + }); + return removeDuplicates ? [...new Set(filtered)] : filtered; +} diff --git a/packages/util/src/index.ts b/packages/util/src/index.ts deleted file mode 100644 index 2d2f6be09..000000000 --- a/packages/util/src/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function testIndex(): void { - console.log("testIndex"); -} diff --git a/packages/util/tsconfig.json b/packages/util/tsconfig.json index 4d12c2f9c..e632a1d38 100644 --- a/packages/util/tsconfig.json +++ b/packages/util/tsconfig.json @@ -7,7 +7,8 @@ "declarationMap": true, "moduleResolution": "Node", "module": "ES6", - "target": "ES2015" + "target": "ES2015", + "lib": ["es2016"] }, "include": ["src"], "exclude": ["node_modules", "dist"]