Fill in missing stuff from missing-native-js-functions

This commit is contained in:
Rory& 2025-10-03 23:27:40 +02:00
parent 625df4e314
commit 027392488b
5 changed files with 50 additions and 2 deletions

View File

@ -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,

View File

@ -27,6 +27,8 @@ declare global {
last(): T | undefined;
distinct(): T[];
distinctBy<K>(key: (elem: T) => K): T[];
intersect(other: T[]): T[];
except(other: T[]): T[];
}
}
@ -85,6 +87,14 @@ export function arrayDistinctBy<T, K>(this: T[], key: (elem: T) => K): T[] {
});
}
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[]) {
@ -121,4 +131,12 @@ if (!Array.prototype.distinct)
if (!Array.prototype.distinctBy)
Array.prototype.distinctBy = function <T, K>(this: T[], key: (elem: T) => K) {
return arrayDistinctBy.call(this, key as ((elem: unknown) => unknown));
};
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

@ -0,0 +1,29 @@
declare global {
interface Object {
forEach(callback: (value: any, key: string, object: any) => void): void;
map<T>(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<T>(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,
});

View File

@ -1,3 +1,4 @@
export * from "./Array";
export * from "./Math";
export * from "./Url";
export * from "./Object";

View File

@ -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))
);