diff --git a/src/util/util/FieldError.ts b/src/util/util/FieldError.ts index 810a9971..3bab92e7 100644 --- a/src/util/util/FieldError.ts +++ b/src/util/util/FieldError.ts @@ -23,7 +23,7 @@ export function FieldErrors( return new FieldError( 50035, "Invalid Form Body", - (fields as Object).map(({ message, code }) => ({ + fields.map(({ message, code }) => ({ _errors: [ { message, diff --git a/src/util/util/extensions/Array.ts b/src/util/util/extensions/Array.ts index d4b383ce..611a8437 100644 --- a/src/util/util/extensions/Array.ts +++ b/src/util/util/extensions/Array.ts @@ -27,6 +27,8 @@ declare global { last(): T | undefined; distinct(): T[]; distinctBy(key: (elem: T) => K): T[]; + intersect(other: T[]): T[]; + except(other: T[]): T[]; } } @@ -85,6 +87,14 @@ export function arrayDistinctBy(this: T[], key: (elem: T) => K): T[] { }); } +export function arrayIntersect(this: T[], other: T[]): T[] { + return this.filter((value) => other.includes(value)); +} + +export function arrayExcept(this: T[], other: T[]): T[] { + return this.filter((value) => !other.includes(value)); +} + // register extensions if (!Array.prototype.containsAll) Array.prototype.containsAll = function (this: T[], target: T[]) { @@ -121,4 +131,12 @@ if (!Array.prototype.distinct) if (!Array.prototype.distinctBy) Array.prototype.distinctBy = function (this: T[], key: (elem: T) => K) { return arrayDistinctBy.call(this, key as ((elem: unknown) => unknown)); + }; +if (!Array.prototype.intersect) + Array.prototype.intersect = function (this: T[], other: T[]) { + return arrayIntersect.call(this, other); + }; +if (!Array.prototype.except) + Array.prototype.except = function (this: T[], other: T[]) { + return arrayExcept.call(this, other); }; \ No newline at end of file diff --git a/src/util/util/extensions/Object.ts b/src/util/util/extensions/Object.ts new file mode 100644 index 00000000..46da5965 --- /dev/null +++ b/src/util/util/extensions/Object.ts @@ -0,0 +1,29 @@ +declare global { + interface Object { + forEach(callback: (value: any, key: string, object: any) => void): void; + map(callback: (value: any, key: string, object: any) => T): T[]; + } +} + +export function objectForEach(obj: any, callback: (value: any, key: string, object: any) => void): void { + Object.keys(obj).forEach((key) => { + callback(obj[key], key, obj); + }); +} + +export function objectMap(obj: any, callback: (value: any, key: string, object: any) => T): T[] { + return Object.keys(obj).map((key) => { + return callback(obj[key], key, obj); + }); +} + +if (!Object.prototype.forEach) + Object.defineProperty(Object.prototype, "forEach", { + value: objectForEach, + enumerable: false, + }); +if (!Object.prototype.map) + Object.defineProperty(Object.prototype, "map", { + value: objectMap, + enumerable: false, + }); \ No newline at end of file diff --git a/src/util/util/extensions/index.ts b/src/util/util/extensions/index.ts index 540c47dd..3bde4f7f 100644 --- a/src/util/util/extensions/index.ts +++ b/src/util/util/extensions/index.ts @@ -1,3 +1,4 @@ export * from "./Array"; export * from "./Math"; export * from "./Url"; +export * from "./Object"; \ No newline at end of file diff --git a/src/util/util/lambert-server/check.ts b/src/util/util/lambert-server/check.ts index efd8d20f..4dabc5ce 100644 --- a/src/util/util/lambert-server/check.ts +++ b/src/util/util/lambert-server/check.ts @@ -98,7 +98,7 @@ export function instanceOf( } if (typeof value !== "object") throw `${path} must be a object`; - const diff = Object.keys(value).missing( + const diff = Object.keys(value).except( Object.keys(type).map((x) => (x.startsWith(OPTIONAL_PREFIX) ? x.slice(OPTIONAL_PREFIX.length) : x)) );