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

is.all(is.array, arr1, arr2) tries to use index 1 as a function #123

Closed
GuillaumeRochat opened this issue Aug 18, 2020 · 3 comments · Fixed by #125
Closed

is.all(is.array, arr1, arr2) tries to use index 1 as a function #123

GuillaumeRochat opened this issue Aug 18, 2020 · 3 comments · Fixed by #125

Comments

@GuillaumeRochat
Copy link

With the is.array changes to include an additional type check, the is.array function can no longer be used within a is.all. The behavior can also be misleading at first because it works when there is only one element being checked. It may be difficult to debug when used with the spread operator.

E.g.:

> const test1 = [['a']]
> is.all(is.array, ...test1)
true
> const test2 = [['a'], ['b']]
> is.all(is.array, ...test2);
Uncaught TypeError: 1 is not a function
    at Array.every (<anonymous>)
    at is.array (/home/user/project/node_modules/@sindresorhus/is/dist/index.js:137:18)
    at Array.every (<anonymous>)
    at predicateOnArray (/home/user/project/node_modules/@sindresorhus/is/dist/index.js:278:19)
    at Function.is.all (/home/user/project/node_modules/@sindresorhus/is/dist/index.js:284:36)

I can easily change the syntax to

is.all(v => is.array(v), ...test2)

and have it work like it used to, with the added benefit of using it with the new type check when needed

is.all(v => is.array(v, is.string), ...test2)

However, I think the array method could check for if (is.function_(assertion)) instead of just if (assertion), this way it would not try to call the index number as a function (which Array.prototype.every sets as the second argument on its callback).

@GuillaumeRochat GuillaumeRochat changed the title is.all(is.array, arr1, arr2) tries to use arr2 as a function is.all(is.array, arr1, arr2) tries to use index 1 as a function Aug 18, 2020
@sindresorhus
Copy link
Owner

// @Arnovsky

@gioragutt
Copy link
Collaborator

@GuillaumeRochat

However, I think the array method could check for if (is.function_(assertion)) instead of just if (assertion)

This would break if you have an array of functions.

@GuillaumeRochat
Copy link
Author

@gioragutt no, it would only check for the assertion callback, making sure it is one, unless there's something else I didn't understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants