assertType handles multipleValues rather than caller

This commit is contained in:
Dave Cohen 2021-04-21 12:05:50 -05:00
parent 5b13eeedda
commit 773a1cfb74
2 changed files with 26 additions and 6 deletions

View file

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

View file

@ -1591,6 +1591,20 @@ test('is.all', t => {
t.throws(() => { t.throws(() => {
assert.all(is.string); assert.all(is.string);
}); });
t.throws(() => {
assert.all(is.string, 1, 2, 3);
}, {
// Removes duplicates:
message: /received values of types `number`./
});
t.throws(() => {
assert.all(is.string, 1, [4]);
}, {
// Lists all types:
message: /received values of types `number`, `Array`./
});
}); });
test('assert', t => { test('assert', t => {