Meta tweaks
This commit is contained in:
parent
0c3f110386
commit
11003b925e
4 changed files with 386 additions and 146 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
"description": "Type check values",
|
"description": "Type check values",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "sindresorhus/is",
|
"repository": "sindresorhus/is",
|
||||||
|
"funding": "https://github.com/sindresorhus/is?sponsor=1",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Sindre Sorhus",
|
"name": "Sindre Sorhus",
|
||||||
"email": "sindresorhus@gmail.com",
|
"email": "sindresorhus@gmail.com",
|
||||||
|
|
|
||||||
35
readme.md
35
readme.md
|
|
@ -6,7 +6,6 @@ For example, `is.string('🦄') //=> true`
|
||||||
|
|
||||||
<img src="header.gif" width="182" align="right">
|
<img src="header.gif" width="182" align="right">
|
||||||
|
|
||||||
|
|
||||||
## Highlights
|
## Highlights
|
||||||
|
|
||||||
- Written in TypeScript
|
- Written in TypeScript
|
||||||
|
|
@ -15,14 +14,12 @@ For example, `is.string('🦄') //=> true`
|
||||||
- Actively maintained
|
- Actively maintained
|
||||||
- 2 million weekly downloads
|
- 2 million weekly downloads
|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```
|
```
|
||||||
$ npm install @sindresorhus/is
|
$ npm install @sindresorhus/is
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|
@ -38,15 +35,23 @@ is.number(6);
|
||||||
//=> true
|
//=> true
|
||||||
```
|
```
|
||||||
|
|
||||||
[Assertions](#type-assertions) perform the same type checks, but throw errors if the type does not match.
|
[Assertions](#type-assertions) perform the same type checks, but throw an error if the type does not match.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const {assert} = require('@sindresorhus/is');
|
const {assert} = require('@sindresorhus/is');
|
||||||
|
|
||||||
assert.string(foo);
|
assert.string(2);
|
||||||
// foo is known to be a string here
|
//=> Error: Expected value which is `string`, received value of type `number`.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
And with TypeScript:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import {assert} from '@sindresorhus/is';
|
||||||
|
|
||||||
|
assert.string(foo);
|
||||||
|
// `foo` is now typed as a `string`.
|
||||||
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
|
@ -406,7 +411,6 @@ is.all(is.string, '🦄', [], 'unicorns');
|
||||||
//=> false
|
//=> false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Type guards
|
## Type guards
|
||||||
|
|
||||||
When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used extensively to infer the correct type inside if-else statements.
|
When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used extensively to infer the correct type inside if-else statements.
|
||||||
|
|
@ -437,18 +441,20 @@ padLeft('🦄', '🌈');
|
||||||
|
|
||||||
## Type assertions
|
## Type assertions
|
||||||
|
|
||||||
The type guards are also available as [typescript assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions), which throw an error for unexpected types. It is a convenient one-line version of the often repetitive "if-not-expected-type-throw" pattern.
|
The type guards are also available as [type assertions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions), which throw an error for unexpected types. It is a convenient one-line version of the often repetitive "if-not-expected-type-throw" pattern.
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
import {assert} from '@sindresorhus/is';
|
import {assert} from '@sindresorhus/is';
|
||||||
|
|
||||||
const handleMovieRatingApiResponse = (response: unknown) => {
|
const handleMovieRatingApiResponse = (response: unknown) => {
|
||||||
assert.plainObject(response);
|
assert.plainObject(response);
|
||||||
// `response` is typed as a plain `object` with `unknown` properties
|
// `response` is now typed as a plain `object` with `unknown` properties.
|
||||||
|
|
||||||
assert.number(response.rating);
|
assert.number(response.rating);
|
||||||
// `response.rating` is typed as a `number`
|
// `response.rating` is now typed as a `number`.
|
||||||
|
|
||||||
assert.string(response.title);
|
assert.string(response.title);
|
||||||
// `response.title` is typed as a `string`
|
// `response.title` is now typed as a `string`.
|
||||||
|
|
||||||
return `${response.title} (${response.rating * 10})`;
|
return `${response.title} (${response.rating * 10})`;
|
||||||
};
|
};
|
||||||
|
|
@ -456,8 +462,8 @@ const handleMovieRatingApiResponse = (response: unknown) => {
|
||||||
handleMovieRatingApiResponse({rating: 0.87, title: 'The Matrix'});
|
handleMovieRatingApiResponse({rating: 0.87, title: 'The Matrix'});
|
||||||
//=> 'The Matrix (8.7)'
|
//=> 'The Matrix (8.7)'
|
||||||
|
|
||||||
handleMovieRatingApiResponse({status: 'system maintenance'});
|
// This throws an error.
|
||||||
//=> throws error
|
handleMovieRatingApiResponse({rating: '🦄'});
|
||||||
```
|
```
|
||||||
|
|
||||||
## FAQ
|
## FAQ
|
||||||
|
|
@ -477,14 +483,12 @@ For the ones I found, pick 3 of these.
|
||||||
|
|
||||||
The most common mistakes I noticed in these modules was using `instanceof` for type checking, forgetting that functions are objects, and omitting `symbol` as a primitive.
|
The most common mistakes I noticed in these modules was using `instanceof` for type checking, forgetting that functions are objects, and omitting `symbol` as a primitive.
|
||||||
|
|
||||||
|
|
||||||
## For enterprise
|
## For enterprise
|
||||||
|
|
||||||
Available as part of the Tidelift Subscription.
|
Available as part of the Tidelift Subscription.
|
||||||
|
|
||||||
The maintainers of @sindresorhus/is and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-sindresorhus-is?utm_source=npm-sindresorhus-is&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
The maintainers of @sindresorhus/is and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-sindresorhus-is?utm_source=npm-sindresorhus-is&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
||||||
|
|
||||||
|
|
||||||
## Related
|
## Related
|
||||||
|
|
||||||
- [ow](https://github.com/sindresorhus/ow) - Function argument validation for humans
|
- [ow](https://github.com/sindresorhus/ow) - Function argument validation for humans
|
||||||
|
|
@ -498,7 +502,6 @@ The maintainers of @sindresorhus/is and thousands of other packages are working
|
||||||
- [is-blob](https://github.com/sindresorhus/is-blob) - Check if a value is a Blob - File-like object of immutable, raw data
|
- [is-blob](https://github.com/sindresorhus/is-blob) - Check if a value is a Blob - File-like object of immutable, raw data
|
||||||
- [has-emoji](https://github.com/sindresorhus/has-emoji) - Check whether a string has any emoji
|
- [has-emoji](https://github.com/sindresorhus/has-emoji) - Check whether a string has any emoji
|
||||||
|
|
||||||
|
|
||||||
## Maintainers
|
## Maintainers
|
||||||
|
|
||||||
- [Sindre Sorhus](https://github.com/sindresorhus)
|
- [Sindre Sorhus](https://github.com/sindresorhus)
|
||||||
|
|
|
||||||
|
|
@ -387,7 +387,7 @@ is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArr
|
||||||
|
|
||||||
const assertType = (condition: boolean, description: string, value: unknown): asserts condition => {
|
const assertType = (condition: boolean, description: string, value: unknown): asserts condition => {
|
||||||
if (!condition) {
|
if (!condition) {
|
||||||
throw new TypeError(`Expected value which is "${description}", received value of type ${is(value)}.`);
|
throw new TypeError(`Expected value which is \`${description}\`, received value of type \`${is(value)}\`.`);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
494
test/test.ts
494
test/test.ts
|
|
@ -1,14 +1,13 @@
|
||||||
|
import fs = require('fs');
|
||||||
|
import net = require('net');
|
||||||
|
import Stream = require('stream');
|
||||||
import {inspect} from 'util';
|
import {inspect} from 'util';
|
||||||
import test, {ExecutionContext} from 'ava';
|
import test, {ExecutionContext} from 'ava';
|
||||||
import {JSDOM} from 'jsdom';
|
import {JSDOM} from 'jsdom';
|
||||||
import {Subject, Observable} from 'rxjs';
|
import {Subject, Observable} from 'rxjs';
|
||||||
import is, {assert, AssertionTypeDescription, TypeName} from '../source';
|
|
||||||
|
|
||||||
import fs = require('fs');
|
|
||||||
import net = require('net');
|
|
||||||
import Stream = require('stream');
|
|
||||||
import tempy = require('tempy');
|
import tempy = require('tempy');
|
||||||
import ZenObservable = require('zen-observable');
|
import ZenObservable = require('zen-observable');
|
||||||
|
import is, {assert, AssertionTypeDescription, TypeName} from '../source';
|
||||||
|
|
||||||
class PromiseSubclassFixture<T> extends Promise<T> {}
|
class PromiseSubclassFixture<T> extends Promise<T> {}
|
||||||
class ErrorSubclassFixture extends Error {}
|
class ErrorSubclassFixture extends Error {}
|
||||||
|
|
@ -26,7 +25,7 @@ interface Test {
|
||||||
}
|
}
|
||||||
|
|
||||||
const invertAssertThrow = (description: string, fn: () => void | never, value: unknown): void | never => {
|
const invertAssertThrow = (description: string, fn: () => void | never, value: unknown): void | never => {
|
||||||
const expectedAssertErrorMessage = `Expected value which is "${description}", received value of type ${is(value)}.`;
|
const expectedAssertErrorMessage = `Expected value which is \`${description}\`, received value of type \`${is(value)}\`.`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fn();
|
fn();
|
||||||
|
|
@ -555,8 +554,8 @@ const testType = (t: ExecutionContext, type: string, exclude?: string[]) => {
|
||||||
|
|
||||||
for (const fixture of fixtures) {
|
for (const fixture of fixtures) {
|
||||||
assertIs(testIs(fixture), `Value: ${inspect(fixture)}`);
|
assertIs(testIs(fixture), `Value: ${inspect(fixture)}`);
|
||||||
const valueType = JSON.stringify(typeDescription ? typeDescription : typename);
|
const valueType = typeDescription ? typeDescription : typename;
|
||||||
assertAsserts(() => testAssert(fixture), `Expected value which is ${valueType}, received value of type ${is(fixture)}.`);
|
assertAsserts(() => testAssert(fixture), `Expected value which is \`${valueType!}\`, received value of type \`${is(fixture)}\`.`);
|
||||||
|
|
||||||
if (isTypeUnderTest && typename) {
|
if (isTypeUnderTest && typename) {
|
||||||
t.is(is(fixture), typename);
|
t.is(is(fixture), typename);
|
||||||
|
|
@ -597,8 +596,12 @@ test('is.numericString', t => {
|
||||||
testType(t, 'numericString');
|
testType(t, 'numericString');
|
||||||
t.false(is.numericString(''));
|
t.false(is.numericString(''));
|
||||||
t.false(is.numericString(1));
|
t.false(is.numericString(1));
|
||||||
t.throws(() => assert.numericString(''));
|
t.throws(() => {
|
||||||
t.throws(() => assert.numericString(1));
|
assert.numericString('');
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.numericString(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.array', t => {
|
test('is.array', t => {
|
||||||
|
|
@ -611,7 +614,10 @@ test('is.function', t => {
|
||||||
|
|
||||||
test('is.boundFunction', t => {
|
test('is.boundFunction', t => {
|
||||||
t.false(is.boundFunction(function () {})); // eslint-disable-line prefer-arrow-callback
|
t.false(is.boundFunction(function () {})); // eslint-disable-line prefer-arrow-callback
|
||||||
t.throws(() => assert.boundFunction(function () {})); // eslint-disable-line prefer-arrow-callback
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.boundFunction(function () {}); // eslint-disable-line prefer-arrow-callback
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.buffer', t => {
|
test('is.buffer', t => {
|
||||||
|
|
@ -660,8 +666,11 @@ test('is.asyncFunction', t => {
|
||||||
if (is.asyncFunction(fixture)) {
|
if (is.asyncFunction(fixture)) {
|
||||||
// eslint-disable-next-line promise/prefer-await-to-then
|
// eslint-disable-next-line promise/prefer-await-to-then
|
||||||
t.true(is.function_(fixture().then));
|
t.true(is.function_(fixture().then));
|
||||||
// eslint-disable-next-line promise/prefer-await-to-then
|
|
||||||
t.notThrows(() => assert.function_(fixture().then));
|
t.notThrows(() => {
|
||||||
|
// eslint-disable-next-line promise/prefer-await-to-then
|
||||||
|
assert.function_(fixture().then);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -747,13 +756,21 @@ test('is.directInstanceOf', t => {
|
||||||
|
|
||||||
t.true(is.directInstanceOf(error, Error));
|
t.true(is.directInstanceOf(error, Error));
|
||||||
t.true(is.directInstanceOf(errorSubclass, ErrorSubclassFixture));
|
t.true(is.directInstanceOf(errorSubclass, ErrorSubclassFixture));
|
||||||
t.notThrows(() => assert.directInstanceOf(error, Error));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.directInstanceOf(errorSubclass, ErrorSubclassFixture));
|
assert.directInstanceOf(error, Error);
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.directInstanceOf(errorSubclass, ErrorSubclassFixture);
|
||||||
|
});
|
||||||
|
|
||||||
t.false(is.directInstanceOf(error, ErrorSubclassFixture));
|
t.false(is.directInstanceOf(error, ErrorSubclassFixture));
|
||||||
t.false(is.directInstanceOf(errorSubclass, Error));
|
t.false(is.directInstanceOf(errorSubclass, Error));
|
||||||
t.throws(() => assert.directInstanceOf(error, ErrorSubclassFixture));
|
t.throws(() => {
|
||||||
t.throws(() => assert.directInstanceOf(errorSubclass, Error));
|
assert.directInstanceOf(error, ErrorSubclassFixture);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.directInstanceOf(errorSubclass, Error);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.urlInstance', t => {
|
test('is.urlInstance', t => {
|
||||||
|
|
@ -763,10 +780,18 @@ test('is.urlInstance', t => {
|
||||||
t.false(is.urlInstance(undefined));
|
t.false(is.urlInstance(undefined));
|
||||||
t.false(is.urlInstance(null));
|
t.false(is.urlInstance(null));
|
||||||
|
|
||||||
t.notThrows(() => assert.urlInstance(url));
|
t.notThrows(() => {
|
||||||
t.throws(() => assert.urlInstance({}));
|
assert.urlInstance(url);
|
||||||
t.throws(() => assert.urlInstance(undefined));
|
});
|
||||||
t.throws(() => assert.urlInstance(null));
|
t.throws(() => {
|
||||||
|
assert.urlInstance({});
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.urlInstance(undefined);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.urlInstance(null);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.urlString', t => {
|
test('is.urlString', t => {
|
||||||
|
|
@ -777,11 +802,21 @@ test('is.urlString', t => {
|
||||||
t.false(is.urlString(undefined));
|
t.false(is.urlString(undefined));
|
||||||
t.false(is.urlString(null));
|
t.false(is.urlString(null));
|
||||||
|
|
||||||
t.notThrows(() => assert.urlString(url));
|
t.notThrows(() => {
|
||||||
t.throws(() => assert.urlString(new URL(url)));
|
assert.urlString(url);
|
||||||
t.throws(() => assert.urlString({}));
|
});
|
||||||
t.throws(() => assert.urlString(undefined));
|
t.throws(() => {
|
||||||
t.throws(() => assert.urlString(null));
|
assert.urlString(new URL(url));
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.urlString({});
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.urlString(undefined);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.urlString(null);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.truthy', t => {
|
test('is.truthy', t => {
|
||||||
|
|
@ -795,16 +830,30 @@ test('is.truthy', t => {
|
||||||
// t.true(is.truthy(1n));
|
// t.true(is.truthy(1n));
|
||||||
t.true(is.truthy(BigInt(1)));
|
t.true(is.truthy(BigInt(1)));
|
||||||
|
|
||||||
t.notThrows(() => assert.truthy('unicorn'));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.truthy('🦄'));
|
assert.truthy('unicorn');
|
||||||
t.notThrows(() => assert.truthy(new Set()));
|
});
|
||||||
t.notThrows(() => assert.truthy(Symbol('🦄')));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.truthy(true));
|
assert.truthy('🦄');
|
||||||
t.notThrows(() => assert.truthy(1));
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.truthy(new Set());
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.truthy(Symbol('🦄'));
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.truthy(true);
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.truthy(1);
|
||||||
|
});
|
||||||
|
|
||||||
// Disabled until TS supports it for an ESnnnn target.
|
// TODO: Disabled until TS supports it for an ESnnnn target.
|
||||||
// t.notThrows(() => assert.truthy(1n));
|
// t.notThrows(() => assert.truthy(1n));
|
||||||
t.notThrows(() => assert.truthy(BigInt(1)));
|
t.notThrows(() => {
|
||||||
|
assert.truthy(BigInt(1));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.falsy', t => {
|
test('is.falsy', t => {
|
||||||
|
|
@ -814,19 +863,33 @@ test('is.falsy', t => {
|
||||||
t.true(is.falsy(null));
|
t.true(is.falsy(null));
|
||||||
t.true(is.falsy(undefined));
|
t.true(is.falsy(undefined));
|
||||||
t.true(is.falsy(NaN));
|
t.true(is.falsy(NaN));
|
||||||
// Disabled until TS supports it for an ESnnnn target.
|
// TODO: Disabled until TS supports it for an ESnnnn target.
|
||||||
// t.true(is.falsy(0n));
|
// t.true(is.falsy(0n));
|
||||||
t.true(is.falsy(BigInt(0)));
|
t.true(is.falsy(BigInt(0)));
|
||||||
|
|
||||||
t.notThrows(() => assert.falsy(false));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.falsy(0));
|
assert.falsy(false);
|
||||||
t.notThrows(() => assert.falsy(''));
|
});
|
||||||
t.notThrows(() => assert.falsy(null));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.falsy(undefined));
|
assert.falsy(0);
|
||||||
t.notThrows(() => assert.falsy(NaN));
|
});
|
||||||
// Disabled until TS supports it for an ESnnnn target.
|
t.notThrows(() => {
|
||||||
|
assert.falsy('');
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.falsy(null);
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.falsy(undefined);
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.falsy(NaN);
|
||||||
|
});
|
||||||
|
// TODO: Disabled until TS supports it for an ESnnnn target.
|
||||||
// t.notThrows(() => assert.falsy(0n));
|
// t.notThrows(() => assert.falsy(0n));
|
||||||
t.notThrows(() => assert.falsy(BigInt(0)));
|
t.notThrows(() => {
|
||||||
|
assert.falsy(BigInt(0));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.nan', t => {
|
test('is.nan', t => {
|
||||||
|
|
@ -852,24 +915,32 @@ test('is.primitive', t => {
|
||||||
// 6n
|
// 6n
|
||||||
];
|
];
|
||||||
|
|
||||||
for (const el of primitives) {
|
for (const element of primitives) {
|
||||||
t.true(is.primitive(el));
|
t.true(is.primitive(element));
|
||||||
t.notThrows(() => assert.primitive(el));
|
t.notThrows(() => {
|
||||||
|
assert.primitive(element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.integer', t => {
|
test('is.integer', t => {
|
||||||
testType(t, 'integer', ['number', 'safeInteger']);
|
testType(t, 'integer', ['number', 'safeInteger']);
|
||||||
t.false(is.integer(1.4));
|
t.false(is.integer(1.4));
|
||||||
t.throws(() => assert.integer(1.4));
|
t.throws(() => {
|
||||||
|
assert.integer(1.4);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.safeInteger', t => {
|
test('is.safeInteger', t => {
|
||||||
testType(t, 'safeInteger', ['number', 'integer']);
|
testType(t, 'safeInteger', ['number', 'integer']);
|
||||||
t.false(is.safeInteger(2 ** 53));
|
t.false(is.safeInteger(2 ** 53));
|
||||||
t.false(is.safeInteger(-(2 ** 53)));
|
t.false(is.safeInteger(-(2 ** 53)));
|
||||||
t.throws(() => assert.safeInteger(2 ** 53));
|
t.throws(() => {
|
||||||
t.throws(() => assert.safeInteger(-(2 ** 53)));
|
assert.safeInteger(2 ** 53);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.safeInteger(-(2 ** 53));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.plainObject', t => {
|
test('is.plainObject', t => {
|
||||||
|
|
@ -887,15 +958,33 @@ test('is.iterable', t => {
|
||||||
t.false(is.iterable(Infinity));
|
t.false(is.iterable(Infinity));
|
||||||
t.false(is.iterable({}));
|
t.false(is.iterable({}));
|
||||||
|
|
||||||
t.notThrows(() => assert.iterable(''));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.iterable([]));
|
assert.iterable('');
|
||||||
t.notThrows(() => assert.iterable(new Map()));
|
});
|
||||||
t.throws(() => assert.iterable(null));
|
t.notThrows(() => {
|
||||||
t.throws(() => assert.iterable(undefined));
|
assert.iterable([]);
|
||||||
t.throws(() => assert.iterable(0));
|
});
|
||||||
t.throws(() => assert.iterable(NaN));
|
t.notThrows(() => {
|
||||||
t.throws(() => assert.iterable(Infinity));
|
assert.iterable(new Map());
|
||||||
t.throws(() => assert.iterable({}));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.iterable(null);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.iterable(undefined);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.iterable(0);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.iterable(NaN);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.iterable(Infinity);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.iterable({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.asyncIterable', t => {
|
test('is.asyncIterable', t => {
|
||||||
|
|
@ -910,16 +999,30 @@ test('is.asyncIterable', t => {
|
||||||
t.false(is.asyncIterable(Infinity));
|
t.false(is.asyncIterable(Infinity));
|
||||||
t.false(is.asyncIterable({}));
|
t.false(is.asyncIterable({}));
|
||||||
|
|
||||||
t.notThrows(() => assert.asyncIterable({
|
t.notThrows(() => {
|
||||||
[Symbol.asyncIterator]: () => { }
|
assert.asyncIterable({
|
||||||
}));
|
[Symbol.asyncIterator]: () => { }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
t.throws(() => assert.asyncIterable(null));
|
t.throws(() => {
|
||||||
t.throws(() => assert.asyncIterable(undefined));
|
assert.asyncIterable(null);
|
||||||
t.throws(() => assert.asyncIterable(0));
|
});
|
||||||
t.throws(() => assert.asyncIterable(NaN));
|
t.throws(() => {
|
||||||
t.throws(() => assert.asyncIterable(Infinity));
|
assert.asyncIterable(undefined);
|
||||||
t.throws(() => assert.asyncIterable({}));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.asyncIterable(0);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.asyncIterable(NaN);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.asyncIterable(Infinity);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.asyncIterable({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.class', t => {
|
test('is.class', t => {
|
||||||
|
|
@ -932,7 +1035,10 @@ test('is.class', t => {
|
||||||
|
|
||||||
for (const classDeclaration of classDeclarations) {
|
for (const classDeclaration of classDeclarations) {
|
||||||
t.true(is.class_(classDeclaration));
|
t.true(is.class_(classDeclaration));
|
||||||
t.notThrows(() => assert.class_(classDeclaration));
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.class_(classDeclaration);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -952,16 +1058,25 @@ test('is.typedArray', t => {
|
||||||
|
|
||||||
for (const item of typedArrays) {
|
for (const item of typedArrays) {
|
||||||
t.true(is.typedArray(item));
|
t.true(is.typedArray(item));
|
||||||
t.notThrows(() => assert.typedArray(item));
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.typedArray(item);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
t.false(is.typedArray(new ArrayBuffer(1)));
|
t.false(is.typedArray(new ArrayBuffer(1)));
|
||||||
t.false(is.typedArray([]));
|
t.false(is.typedArray([]));
|
||||||
t.false(is.typedArray({}));
|
t.false(is.typedArray({}));
|
||||||
|
|
||||||
t.throws(() => assert.typedArray(new ArrayBuffer(1)));
|
t.throws(() => {
|
||||||
t.throws(() => assert.typedArray([]));
|
assert.typedArray(new ArrayBuffer(1));
|
||||||
t.throws(() => assert.typedArray({}));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.typedArray([]);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.typedArray({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.arrayLike', t => {
|
test('is.arrayLike', t => {
|
||||||
|
|
@ -977,15 +1092,27 @@ test('is.arrayLike', t => {
|
||||||
t.false(is.arrayLike(new Map()));
|
t.false(is.arrayLike(new Map()));
|
||||||
|
|
||||||
(function () {
|
(function () {
|
||||||
t.notThrows(() => assert.arrayLike(arguments)); // eslint-disable-line prefer-rest-params
|
t.notThrows(() => {
|
||||||
|
assert.arrayLike(arguments); // eslint-disable-line prefer-rest-params
|
||||||
|
});
|
||||||
})();
|
})();
|
||||||
|
|
||||||
t.notThrows(() => assert.arrayLike([]));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.arrayLike('unicorn'));
|
assert.arrayLike([]);
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.arrayLike('unicorn');
|
||||||
|
});
|
||||||
|
|
||||||
t.throws(() => assert.arrayLike({}));
|
t.throws(() => {
|
||||||
t.throws(() => assert.arrayLike(() => { }));
|
assert.arrayLike({});
|
||||||
t.throws(() => assert.arrayLike(new Map()));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.arrayLike(() => {});
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.arrayLike(new Map());
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.inRange', t => {
|
test('is.inRange', t => {
|
||||||
|
|
@ -1018,20 +1145,57 @@ test('is.inRange', t => {
|
||||||
is.inRange(0, [1, 2, 3]);
|
is.inRange(0, [1, 2, 3]);
|
||||||
});
|
});
|
||||||
|
|
||||||
t.notThrows(() => assert.inRange(x, [0, 5]));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.inRange(x, [5, 0]));
|
assert.inRange(x, [0, 5]);
|
||||||
t.notThrows(() => assert.inRange(x, [-5, 5]));
|
});
|
||||||
t.notThrows(() => assert.inRange(x, [5, -5]));
|
|
||||||
t.throws(() => assert.inRange(x, [4, 8]));
|
|
||||||
t.notThrows(() => assert.inRange(-7, [-5, -10]));
|
|
||||||
t.notThrows(() => assert.inRange(-5, [-5, -10]));
|
|
||||||
t.notThrows(() => assert.inRange(-10, [-5, -10]));
|
|
||||||
|
|
||||||
t.notThrows(() => assert.inRange(x, 10));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.inRange(0, 0));
|
assert.inRange(x, [5, 0]);
|
||||||
t.notThrows(() => assert.inRange(-2, -3));
|
});
|
||||||
t.throws(() => assert.inRange(x, 2));
|
|
||||||
t.throws(() => assert.inRange(-3, -2));
|
t.notThrows(() => {
|
||||||
|
assert.inRange(x, [-5, 5]);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(x, [5, -5]);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.inRange(x, [4, 8]);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(-7, [-5, -10]);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(-5, [-5, -10]);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(-10, [-5, -10]);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(x, 10);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(0, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.inRange(-2, -3);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.inRange(x, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.inRange(-3, -2);
|
||||||
|
});
|
||||||
|
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
assert.inRange(0, []);
|
assert.inRange(0, []);
|
||||||
|
|
@ -1049,7 +1213,9 @@ test('is.inRange', t => {
|
||||||
test('is.domElement', t => {
|
test('is.domElement', t => {
|
||||||
testType(t, 'domElement');
|
testType(t, 'domElement');
|
||||||
t.false(is.domElement({nodeType: 1, nodeName: 'div'}));
|
t.false(is.domElement({nodeType: 1, nodeName: 'div'}));
|
||||||
t.throws(() => assert.domElement({nodeType: 1, nodeName: 'div'}));
|
t.throws(() => {
|
||||||
|
assert.domElement({nodeType: 1, nodeName: 'div'});
|
||||||
|
});
|
||||||
|
|
||||||
const htmlTagNameToTypeName = {
|
const htmlTagNameToTypeName = {
|
||||||
div: 'HTMLDivElement',
|
div: 'HTMLDivElement',
|
||||||
|
|
@ -1081,24 +1247,32 @@ test('is.infinite', t => {
|
||||||
test('is.evenInteger', t => {
|
test('is.evenInteger', t => {
|
||||||
for (const el of [-6, 2, 4]) {
|
for (const el of [-6, 2, 4]) {
|
||||||
t.true(is.evenInteger(el));
|
t.true(is.evenInteger(el));
|
||||||
t.notThrows(() => assert.evenInteger(el));
|
t.notThrows(() => {
|
||||||
|
assert.evenInteger(el);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const el of [-3, 1, 5]) {
|
for (const el of [-3, 1, 5]) {
|
||||||
t.false(is.evenInteger(el));
|
t.false(is.evenInteger(el));
|
||||||
t.throws(() => assert.evenInteger(el));
|
t.throws(() => {
|
||||||
|
assert.evenInteger(el);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.oddInteger', t => {
|
test('is.oddInteger', t => {
|
||||||
for (const el of [-5, 7, 13]) {
|
for (const el of [-5, 7, 13]) {
|
||||||
t.true(is.oddInteger(el));
|
t.true(is.oddInteger(el));
|
||||||
t.notThrows(() => assert.oddInteger(el));
|
t.notThrows(() => {
|
||||||
|
assert.oddInteger(el);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const el of [-8, 8, 10]) {
|
for (const el of [-8, 8, 10]) {
|
||||||
t.false(is.oddInteger(el));
|
t.false(is.oddInteger(el));
|
||||||
t.throws(() => assert.oddInteger(el));
|
t.throws(() => {
|
||||||
|
assert.oddInteger(el);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -1111,15 +1285,23 @@ test('is.nonEmptyArray', t => {
|
||||||
t.false(is.nonEmptyArray([]));
|
t.false(is.nonEmptyArray([]));
|
||||||
t.false(is.nonEmptyArray(new Array())); // eslint-disable-line @typescript-eslint/no-array-constructor
|
t.false(is.nonEmptyArray(new Array())); // eslint-disable-line @typescript-eslint/no-array-constructor
|
||||||
|
|
||||||
t.notThrows(() => assert.nonEmptyArray([1, 2, 3]));
|
t.notThrows(() => {
|
||||||
t.throws(() => assert.nonEmptyArray([]));
|
assert.nonEmptyArray([1, 2, 3]);
|
||||||
t.throws(() => assert.nonEmptyArray(new Array())); // eslint-disable-line @typescript-eslint/no-array-constructor
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.nonEmptyArray([]);
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.nonEmptyArray(new Array()); // eslint-disable-line @typescript-eslint/no-array-constructor
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.emptyString', t => {
|
test('is.emptyString', t => {
|
||||||
testType(t, 'emptyString', ['string']);
|
testType(t, 'emptyString', ['string']);
|
||||||
t.false(is.emptyString('🦄'));
|
t.false(is.emptyString('🦄'));
|
||||||
t.throws(() => assert.emptyString('🦄'));
|
t.throws(() => {
|
||||||
|
assert.emptyString('🦄');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.nonEmptyString', t => {
|
test('is.nonEmptyString', t => {
|
||||||
|
|
@ -1127,9 +1309,15 @@ test('is.nonEmptyString', t => {
|
||||||
t.false(is.nonEmptyString(String()));
|
t.false(is.nonEmptyString(String()));
|
||||||
t.true(is.nonEmptyString('🦄'));
|
t.true(is.nonEmptyString('🦄'));
|
||||||
|
|
||||||
t.throws(() => assert.nonEmptyString(''));
|
t.throws(() => {
|
||||||
t.throws(() => assert.nonEmptyString(String()));
|
assert.nonEmptyString('');
|
||||||
t.notThrows(() => assert.nonEmptyString('🦄'));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.nonEmptyString(String());
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.nonEmptyString('🦄');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.emptyStringOrWhitespace', t => {
|
test('is.emptyStringOrWhitespace', t => {
|
||||||
|
|
@ -1138,9 +1326,15 @@ test('is.emptyStringOrWhitespace', t => {
|
||||||
t.false(is.emptyStringOrWhitespace('🦄'));
|
t.false(is.emptyStringOrWhitespace('🦄'));
|
||||||
t.false(is.emptyStringOrWhitespace('unicorn'));
|
t.false(is.emptyStringOrWhitespace('unicorn'));
|
||||||
|
|
||||||
t.notThrows(() => assert.emptyStringOrWhitespace(' '));
|
t.notThrows(() => {
|
||||||
t.throws(() => assert.emptyStringOrWhitespace('🦄'));
|
assert.emptyStringOrWhitespace(' ');
|
||||||
t.throws(() => assert.emptyStringOrWhitespace('unicorn'));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.emptyStringOrWhitespace('🦄');
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.emptyStringOrWhitespace('unicorn');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.emptyObject', t => {
|
test('is.emptyObject', t => {
|
||||||
|
|
@ -1148,9 +1342,15 @@ test('is.emptyObject', t => {
|
||||||
t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object
|
t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object
|
||||||
t.false(is.emptyObject({unicorn: '🦄'}));
|
t.false(is.emptyObject({unicorn: '🦄'}));
|
||||||
|
|
||||||
t.notThrows(() => assert.emptyObject({}));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.emptyObject(new Object())); // eslint-disable-line no-new-object
|
assert.emptyObject({});
|
||||||
t.throws(() => assert.emptyObject({unicorn: '🦄'}));
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.emptyObject(new Object()); // eslint-disable-line no-new-object
|
||||||
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.emptyObject({unicorn: '🦄'});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.nonEmptyObject', t => {
|
test('is.nonEmptyObject', t => {
|
||||||
|
|
@ -1161,9 +1361,15 @@ test('is.nonEmptyObject', t => {
|
||||||
t.false(is.nonEmptyObject(new Object())); // eslint-disable-line no-new-object
|
t.false(is.nonEmptyObject(new Object())); // eslint-disable-line no-new-object
|
||||||
t.true(is.nonEmptyObject({unicorn: '🦄'}));
|
t.true(is.nonEmptyObject({unicorn: '🦄'}));
|
||||||
|
|
||||||
t.throws(() => assert.nonEmptyObject({}));
|
t.throws(() => {
|
||||||
t.throws(() => assert.nonEmptyObject(new Object())); // eslint-disable-line no-new-object
|
assert.nonEmptyObject({});
|
||||||
t.notThrows(() => assert.nonEmptyObject({unicorn: '🦄'}));
|
});
|
||||||
|
t.throws(() => {
|
||||||
|
assert.nonEmptyObject(new Object()); // eslint-disable-line no-new-object
|
||||||
|
});
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.nonEmptyObject({unicorn: '🦄'});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.emptySet', t => {
|
test('is.emptySet', t => {
|
||||||
|
|
@ -1173,11 +1379,15 @@ test('is.emptySet', t => {
|
||||||
test('is.nonEmptySet', t => {
|
test('is.nonEmptySet', t => {
|
||||||
const tempSet = new Set();
|
const tempSet = new Set();
|
||||||
t.false(is.nonEmptySet(tempSet));
|
t.false(is.nonEmptySet(tempSet));
|
||||||
t.throws(() => assert.nonEmptySet(tempSet));
|
t.throws(() => {
|
||||||
|
assert.nonEmptySet(tempSet);
|
||||||
|
});
|
||||||
|
|
||||||
tempSet.add(1);
|
tempSet.add(1);
|
||||||
t.true(is.nonEmptySet(tempSet));
|
t.true(is.nonEmptySet(tempSet));
|
||||||
t.notThrows(() => assert.nonEmptySet(tempSet));
|
t.notThrows(() => {
|
||||||
|
assert.nonEmptySet(tempSet);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.emptyMap', t => {
|
test('is.emptyMap', t => {
|
||||||
|
|
@ -1187,11 +1397,15 @@ test('is.emptyMap', t => {
|
||||||
test('is.nonEmptyMap', t => {
|
test('is.nonEmptyMap', t => {
|
||||||
const tempMap = new Map();
|
const tempMap = new Map();
|
||||||
t.false(is.nonEmptyMap(tempMap));
|
t.false(is.nonEmptyMap(tempMap));
|
||||||
t.throws(() => assert.nonEmptyMap(tempMap));
|
t.throws(() => {
|
||||||
|
assert.nonEmptyMap(tempMap);
|
||||||
|
});
|
||||||
|
|
||||||
tempMap.set('unicorn', '🦄');
|
tempMap.set('unicorn', '🦄');
|
||||||
t.true(is.nonEmptyMap(tempMap));
|
t.true(is.nonEmptyMap(tempMap));
|
||||||
t.notThrows(() => assert.nonEmptyMap(tempMap));
|
t.notThrows(() => {
|
||||||
|
assert.nonEmptyMap(tempMap);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.any', t => {
|
test('is.any', t => {
|
||||||
|
|
@ -1208,10 +1422,21 @@ test('is.any', t => {
|
||||||
is.any(is.string);
|
is.any(is.string);
|
||||||
});
|
});
|
||||||
|
|
||||||
t.notThrows(() => assert.any(is.string, {}, true, '🦄'));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.any(is.object, false, {}, 'unicorns'));
|
assert.any(is.string, {}, true, '🦄');
|
||||||
t.throws(() => assert.any(is.boolean, '🦄', [], 3));
|
});
|
||||||
t.throws(() => assert.any(is.integer, true, 'lol', {}));
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.any(is.object, false, {}, 'unicorns');
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.any(is.boolean, '🦄', [], 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.any(is.integer, true, 'lol', {});
|
||||||
|
});
|
||||||
|
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
assert.any(null as any, true);
|
assert.any(null as any, true);
|
||||||
|
|
@ -1236,10 +1461,21 @@ test('is.all', t => {
|
||||||
is.all(is.string);
|
is.all(is.string);
|
||||||
});
|
});
|
||||||
|
|
||||||
t.notThrows(() => assert.all(is.object, {}, new Set(), new Map()));
|
t.notThrows(() => {
|
||||||
t.notThrows(() => assert.all(is.boolean, true, false));
|
assert.all(is.object, {}, new Set(), new Map());
|
||||||
t.throws(() => assert.all(is.string, '🦄', []));
|
});
|
||||||
t.throws(() => assert.all(is.set, new Map(), {}));
|
|
||||||
|
t.notThrows(() => {
|
||||||
|
assert.all(is.boolean, true, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.all(is.string, '🦄', []);
|
||||||
|
});
|
||||||
|
|
||||||
|
t.throws(() => {
|
||||||
|
assert.all(is.set, new Map(), {});
|
||||||
|
});
|
||||||
|
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
assert.all(null as any, true);
|
assert.all(null as any, true);
|
||||||
|
|
@ -1251,18 +1487,18 @@ test('is.all', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('assert', t => {
|
test('assert', t => {
|
||||||
// Contrived test showing that typescript acknowledges the type assertion in assert.number().
|
// Contrived test showing that TypeScript acknowledges the type assertion in `assert.number()`.
|
||||||
// Real world usage includes asserting user input, but here we use a random number/string generator.
|
// Real--world usage includes asserting user input, but here we use a random number/string generator.
|
||||||
t.plan(2);
|
t.plan(2);
|
||||||
|
|
||||||
const getNumberOrStringRandomly = (): number | string => {
|
const getNumberOrStringRandomly = (): number | string => {
|
||||||
const rnd = Math.random();
|
const random = Math.random();
|
||||||
|
|
||||||
if (rnd < 0.5) {
|
if (random < 0.5) {
|
||||||
return 'sometimes this function returns text';
|
return 'sometimes this function returns text';
|
||||||
}
|
}
|
||||||
|
|
||||||
return rnd;
|
return random;
|
||||||
};
|
};
|
||||||
|
|
||||||
const canUseOnlyNumber = (badlyTypedArgument: any): number => {
|
const canUseOnlyNumber = (badlyTypedArgument: any): number => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue