Skip to content

Commit

Permalink
Fix assertion message for .all and .any (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
scraggo committed Apr 22, 2021
1 parent 5ed7e9b commit b748ab7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
19 changes: 15 additions & 4 deletions source/index.ts
Expand Up @@ -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}.`);
}
};

Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions test/test.ts
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit b748ab7

Please sign in to comment.