Fix assertion message for .all and .any (#132)

This commit is contained in:
Dave Cohen 2021-04-22 04:00:08 -05:00 committed by GitHub
parent 5ed7e9bb40
commit b748ab72b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 4 deletions

View file

@ -388,9 +388,18 @@ is.any = (predicate: Predicate | Predicate[], ...values: unknown[]): boolean =>
is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.every, predicate, values);
const assertType = (condition: boolean, description: string, value: unknown): asserts condition => {
const assertType = (condition: boolean, description: string, value: unknown, options: {multipleValues?: boolean} = {}): asserts condition => {
if (!condition) {
throw new TypeError(`Expected value which is \`${description}\`, received value of type \`${is(value)}\`.`);
const {multipleValues} = options;
const valuesMessage = multipleValues ?
`received values of types ${[
...new Set(
(value as any[]).map(singleValue => `\`${is(singleValue)}\``)
)
].join(', ')}` :
`received value of type \`${is(value)}\``;
throw new TypeError(`Expected value which is \`${description}\`, ${valuesMessage}.`);
}
};
@ -620,8 +629,10 @@ 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 | 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)
any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never => {
return assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values, {multipleValues: true});
},
all: (predicate: Predicate, ...values: unknown[]): void | never => assertType(is.all(predicate, ...values), AssertionTypeDescription.all, values, {multipleValues: true})
};
// Some few keywords are reserved, but we'll populate them for Node.js users