Fill in missing stuff from missing-native-js-functions
This commit is contained in:
parent
625df4e314
commit
027392488b
@ -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,
|
||||
|
||||
@ -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);
|
||||
};
|
||||
29
src/util/util/extensions/Object.ts
Normal file
29
src/util/util/extensions/Object.ts
Normal 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,
|
||||
});
|
||||
@ -1,3 +1,4 @@
|
||||
export * from "./Array";
|
||||
export * from "./Math";
|
||||
export * from "./Url";
|
||||
export * from "./Object";
|
||||
@ -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))
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user