Add support for multiple predicates to is.any (#104)
This commit is contained in:
parent
f97029fd73
commit
d1929ad47c
3 changed files with 23 additions and 5 deletions
|
|
@ -388,7 +388,13 @@ const predicateOnArray = (method: ArrayMethod, predicate: Predicate, values: unk
|
|||
return method.call(values, predicate);
|
||||
};
|
||||
|
||||
is.any = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.some, predicate, values);
|
||||
is.any = (predicate: Predicate | Predicate[], ...values: unknown[]): boolean => {
|
||||
const predicates = is.array(predicate) ? predicate : [predicate];
|
||||
return predicates.some(singlePredicate =>
|
||||
predicateOnArray(Array.prototype.some, singlePredicate, values)
|
||||
);
|
||||
};
|
||||
|
||||
is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.every, predicate, values);
|
||||
|
||||
const assertType = (condition: boolean, description: string, value: unknown): asserts condition => {
|
||||
|
|
@ -526,7 +532,7 @@ interface Assert {
|
|||
inRange: (value: number, range: number | number[]) => asserts value is number;
|
||||
|
||||
// Variadic functions.
|
||||
any: (predicate: Predicate, ...values: unknown[]) => void | never;
|
||||
any: (predicate: Predicate | Predicate[], ...values: unknown[]) => void | never;
|
||||
all: (predicate: Predicate, ...values: unknown[]) => void | never;
|
||||
}
|
||||
|
||||
|
|
@ -616,7 +622,7 @@ export const assert: Assert = {
|
|||
inRange: (value: number, range: number | number[]): asserts value is number => assertType(is.inRange(value, range), AssertionTypeDescription.inRange, value),
|
||||
|
||||
// Variadic functions.
|
||||
any: (predicate: Predicate, ...values: unknown[]): void | never => assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values),
|
||||
any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never => assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values),
|
||||
all: (predicate: Predicate, ...values: unknown[]): void | never => assertType(is.all(predicate, ...values), AssertionTypeDescription.all, values)
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue