Add Type tests
This commit is contained in:
parent
a04c656ec7
commit
b12fcef980
2 changed files with 46 additions and 2 deletions
|
|
@ -415,15 +415,18 @@ foo();
|
||||||
A `value` is tuple-like if it matches the provided `guards` array both in `.length` and in types.
|
A `value` is tuple-like if it matches the provided `guards` array both in `.length` and in types.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
is.tupleLike([1],[is.number]);
|
is.tupleLike([1], [is.number]);
|
||||||
//=> true
|
//=> true
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
function foo() {
|
function foo() {
|
||||||
const tuple = [1, '2', true]
|
const tuple = [1, '2', true];
|
||||||
if (is.tupleLike(tuple, [is.number, is.string, is.boolean])) {
|
if (is.tupleLike(tuple, [is.number, is.string, is.boolean])) {
|
||||||
tuple // [number, string, boolean]
|
tuple // [number, string, boolean]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foo();
|
foo();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
||||||
41
test/test.ts
41
test/test.ts
|
|
@ -1480,6 +1480,47 @@ test('is.tupleLike', t => {
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
assert.tupleLike(new Map(), [is.map]);
|
assert.tupleLike(new Map(), [is.map]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
const tuple = [[false, 'unicorn'], 'string', true];
|
||||||
|
const function_ = (value: string) => value;
|
||||||
|
|
||||||
|
if (is.tupleLike(tuple, [is.array, is.string, is.boolean])) {
|
||||||
|
if (is.tupleLike(tuple[0], [is.boolean, is.string])) { // eslint-disable-line unicorn/no-lonely-if
|
||||||
|
const value = tuple[0][1];
|
||||||
|
function_(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const tuple = [{isTest: true}, '1', true, null];
|
||||||
|
const function_ = (value: Record<string, unknown>) => value;
|
||||||
|
|
||||||
|
if (is.tupleLike(tuple, [is.nonEmptyObject, is.string, is.boolean, is.null_])) {
|
||||||
|
const value = tuple[0];
|
||||||
|
function_(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const tuple = [1, '1', true, null, undefined];
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
|
const function_ = (value: number | string | boolean | undefined | null) => value;
|
||||||
|
|
||||||
|
if (is.tupleLike(tuple, [is.number, is.string, is.boolean, is.undefined, is.null_])) {
|
||||||
|
const numericValue = tuple[0];
|
||||||
|
const stringValue = tuple[1];
|
||||||
|
const booleanValue = tuple[2];
|
||||||
|
const undefinedValue = tuple[3];
|
||||||
|
const nullValue = tuple[4];
|
||||||
|
function_(numericValue);
|
||||||
|
function_(stringValue);
|
||||||
|
function_(booleanValue);
|
||||||
|
function_(undefinedValue);
|
||||||
|
function_(nullValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.inRange', t => {
|
test('is.inRange', t => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue