Fix assertion message for .all and .any (#132)
This commit is contained in:
parent
5ed7e9bb40
commit
b748ab72b6
2 changed files with 50 additions and 4 deletions
|
|
@ -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);
|
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) {
|
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),
|
inRange: (value: number, range: number | number[]): asserts value is number => assertType(is.inRange(value, range), AssertionTypeDescription.inRange, value),
|
||||||
|
|
||||||
// Variadic functions.
|
// Variadic functions.
|
||||||
any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never => assertType(is.any(predicate, ...values), AssertionTypeDescription.any, values),
|
any: (predicate: Predicate | Predicate[], ...values: unknown[]): void | never => {
|
||||||
all: (predicate: Predicate, ...values: unknown[]): void | never => assertType(is.all(predicate, ...values), AssertionTypeDescription.all, values)
|
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
|
// Some few keywords are reserved, but we'll populate them for Node.js users
|
||||||
|
|
|
||||||
35
test/test.ts
35
test/test.ts
|
|
@ -1529,6 +1529,27 @@ test('is.any', t => {
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
assert.any(is.string);
|
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 => {
|
test('is.all', t => {
|
||||||
|
|
@ -1570,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 => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue