Simplified isArrayLike
This commit is contained in:
@ -2,16 +2,8 @@
|
|||||||
|
|
||||||
Checks if the provided argument is array-like (i.e. is iterable).
|
Checks if the provided argument is array-like (i.e. is iterable).
|
||||||
|
|
||||||
Use the spread operator (`...`) to check if the provided argument is iterable inside a `try... catch` block and the comma operator (`,`) to return the appropriate value.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const isArrayLike = val => {
|
const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function';
|
||||||
try {
|
|
||||||
return [...val], true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -1,8 +1,2 @@
|
|||||||
const isArrayLike = val => {
|
const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function';
|
||||||
try {
|
|
||||||
return [...val], true;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
module.exports = isArrayLike;
|
module.exports = isArrayLike;
|
||||||
|
|||||||
Reference in New Issue
Block a user