Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is.TupleLike #189

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,18 @@ foo();
A `value` is tuple-like if it matches the provided `guards` array both in `.length` and in types.

```js
is.tupleLike([1],[is.number]);
is.tupleLike([1], [is.number]);
//=> true
```

```js
function foo() {
const tuple = [1, '2', true]
const tuple = [1, '2', true];
if (is.tupleLike(tuple, [is.number, is.string, is.boolean])) {
tuple // [number, string, boolean]
}
}

foo();
```

Expand Down
41 changes: 41 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,47 @@ test('is.tupleLike', t => {
t.throws(() => {
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);
Myfeetarefreezing marked this conversation as resolved.
Show resolved Hide resolved
function_(stringValue);
function_(booleanValue);
function_(undefinedValue);
function_(nullValue);
}
}
});

test('is.inRange', t => {
Expand Down