remove except (it's probally faster this way too)

This commit is contained in:
MathMan05
2025-11-25 23:28:02 -06:00
parent 685552579b
commit 10147dda08
3 changed files with 7 additions and 30 deletions

View File

@@ -74,14 +74,4 @@ describe("Array extensions", () => {
// @ts-expect-error
assert.deepEqual([].intersect(arr2), []);
});
it("except", () => {
const arr1 = [1, 2, 3, 4];
const arr2 = [3, 4, 5, 6];
assert.deepEqual(arr1.except(arr2), [1, 2]);
assert.deepEqual(arr1.except([]), [1, 2, 3, 4]);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
assert.deepEqual([].except(arr2), []);
});
});

View File

@@ -26,7 +26,6 @@ declare global {
distinct(): T[];
distinctBy<K>(key: (elem: T) => K): T[];
intersect(other: T[]): T[];
except(other: T[]): T[];
}
}
@@ -87,10 +86,6 @@ export function arrayIntersect<T>(this: T[], other: T[]): T[] {
return this.filter((value) => other.includes(value));
}
export function arrayExcept<T>(this: T[], other: T[]): T[] {
return this.filter((value) => !other.includes(value));
}
// register extensions
if (!Array.prototype.containsAll)
Array.prototype.containsAll = function <T>(this: T[], target: T[]) {
@@ -126,7 +121,3 @@ if (!Array.prototype.intersect)
Array.prototype.intersect = function <T>(this: T[], other: T[]) {
return arrayIntersect.call(this, other);
};
if (!Array.prototype.except)
Array.prototype.except = function <T>(this: T[], other: T[]) {
return arrayExcept.call(this, other);
};

View File

@@ -2,8 +2,7 @@ import { NextFunction, Request, Response } from "express";
import { HTTPError } from ".";
const OPTIONAL_PREFIX = "$";
const EMAIL_REGEX =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
const EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
export function check(schema: any) {
return (req: Request, res: Response, next: NextFunction) => {
@@ -31,11 +30,7 @@ export class Email {
}
}
export function instanceOf(
type: any,
value: any,
{ path = "", optional = false }: { path?: string; optional?: boolean } = {}
): Boolean {
export function instanceOf(type: any, value: any, { path = "", optional = false }: { path?: string; optional?: boolean } = {}): boolean {
if (!type) return true; // no type was specified
if (value == null) {
@@ -55,7 +50,9 @@ export function instanceOf(
try {
value = BigInt(value);
if (typeof value === "bigint") return true;
} catch (error) {}
} catch (error) {
//Ignore BigInt error
}
throw `${path} must be a bigint`;
case Boolean:
if (value == "true") value = true;
@@ -98,9 +95,8 @@ export function instanceOf(
}
if (typeof value !== "object") throw `${path} must be a object`;
const diff = Object.keys(value).except(
Object.keys(type).map((x) => (x.startsWith(OPTIONAL_PREFIX) ? x.slice(OPTIONAL_PREFIX.length) : x))
);
const filterset = new Set(Object.keys(type).map((x) => (x.startsWith(OPTIONAL_PREFIX) ? x.slice(OPTIONAL_PREFIX.length) : x)));
const diff = Object.keys(value).filter((_) => filterset.has(_));
if (diff.length) throw `Unknown key ${diff}`;