diff --git a/source/index.ts b/source/index.ts index 37b536f..ffb2ad2 100644 --- a/source/index.ts +++ b/source/index.ts @@ -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 diff --git a/test/test.ts b/test/test.ts index c5c98cd..30c1a86 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1529,6 +1529,27 @@ test('is.any', t => { t.throws(() => { assert.any(is.string); }); + + t.throws(() => { + assert.any(is.string, 1, 2, 3); + }, { + // Removes duplicates: + message: /received values of types `number`./ + }); + + t.throws(() => { + assert.any(is.string, 1, [4]); + }, { + // Lists all types: + message: /received values of types `number`, `Array`./ + }); + + t.throws(() => { + assert.any([is.string, is.nullOrUndefined], 1); + }, { + // Handles array as first argument: + message: /received values of types `number`./ + }); }); test('is.all', t => { @@ -1570,6 +1591,20 @@ test('is.all', t => { t.throws(() => { 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 => {