Skip to content

Commit

Permalink
refactor: move array related function to the arrays file
Browse files Browse the repository at this point in the history
  • Loading branch information
Miodec committed Apr 3, 2024
1 parent 39e611d commit 5701f94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions frontend/src/ts/test/funbox/funbox-validation.ts
@@ -1,6 +1,6 @@
import * as FunboxList from "./funbox-list";
import * as Notifications from "../../elements/notifications";
import * as Misc from "../../utils/misc";
import * as Arrays from "../../utils/arrays";
import * as Strings from "../../utils/strings";

export function checkFunboxForcedConfigs(
Expand Down Expand Up @@ -44,7 +44,7 @@ export function checkFunboxForcedConfigs(
key
] as SharedTypes.ConfigValue[];
} else {
forcedConfigs[key] = Misc.intersect(
forcedConfigs[key] = Arrays.intersect(
forcedConfigs[key] as SharedTypes.ConfigValue[],
fb.forcedConfig[key] as SharedTypes.ConfigValue[],
true
Expand Down Expand Up @@ -305,7 +305,7 @@ export function areFunboxesCompatible(
for (const key in f.forcedConfig) {
if (allowedConfig[key]) {
if (
Misc.intersect(
Arrays.intersect(
allowedConfig[key] as SharedTypes.ConfigValue[],
f.forcedConfig[key] as SharedTypes.ConfigValue[],
true
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/ts/utils/arrays.ts
Expand Up @@ -99,3 +99,18 @@ export function nthElementFromArray<T>(
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<T>(a: T[], b: T[], removeDuplicates = false): T[] {
let t;
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;
}
9 changes: 0 additions & 9 deletions frontend/src/ts/utils/misc.ts
Expand Up @@ -555,15 +555,6 @@ export function isPasswordStrong(password: string): boolean {
return hasCapital && hasNumber && hasSpecial && isLong && isShort;
}

export function intersect<T>(a: T[], b: T[], removeDuplicates = false): T[] {
let t;
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 htmlToText(html: string): string {
const el = document.createElement("div");
el.innerHTML = html;
Expand Down

0 comments on commit 5701f94

Please sign in to comment.