Skip to content

Commit

Permalink
Fix ow.number message when NaN is encountered (#232)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
panva and sindresorhus committed Feb 21, 2022
1 parent 96207f0 commit 8b6a208
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/predicates/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ export class Predicate<T = unknown> implements BasePredicate<T> {
// We do not include type in this label as we do for other messages, because it would be redundant.
const label_ = label?.slice(this.type.length + 1);

// TODO: The NaN check can be removed when `@sindresorhus/is` is fixed: https://github.com/sindresorhus/ow/issues/231#issuecomment-1047100612
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${is(value)}\``;
return `Expected ${label_ || 'argument'} to be of type \`${this.type}\` but received type \`${Number.isNaN(value) ? 'NaN' : is(value)}\``;
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
validator: value => (is as any)[typeString](value),
Expand Down
4 changes: 4 additions & 0 deletions test/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ test('number', t => {
ow('12' as any, ow.number);
}, {message: 'Expected argument to be of type `number` but received type `string`'});

t.throws(() => {
ow(Number.NaN as any, ow.number);
}, {message: 'Expected argument to be of type `number` but received type `NaN`'});

t.throws(() => {
ow('12' as any, 'foo', ow.number);
}, {message: 'Expected `foo` to be of type `number` but received type `string`'});
Expand Down

0 comments on commit 8b6a208

Please sign in to comment.