diff --git a/README.md b/README.md index e1864745a..5092802d3 100644 --- a/README.md +++ b/README.md @@ -4088,24 +4088,22 @@ isArray([1]); // true Checks if the provided argument is array-like (i.e. is iterable). -Use `Array.from()` and a `try... catch` block to check if the provided argument is array-like. +Check that the object is not a function or `null` and that its `length` property is a non-negative integer below `Number.MAX_SAFE_INTEGER`. ```js -const isArrayLike = arr => { - try { - Array.from(arr); - return true; - } catch (e) { - return false; - } -}; +const isArrayLike = val => + val != null && + typeof val != 'function' && + val.length > -1 && + val.length % 1 == 0 && + val.length <= Number.MAX_SAFE_INTEGER; ```
Examples ```js -isArrayLike(document.querySelector('.className')); // true +isArrayLike(document.querySelectorAll('.className')); // true isArrayLike('abc'); // true isArrayLike(null); // false ``` diff --git a/docs/index.html b/docs/index.html index c6dcd331c..fd4c1c019 100644 --- a/docs/index.html +++ b/docs/index.html @@ -853,15 +853,13 @@ hexToRGB('#fff'); // 'rgb(255, 255, 255)'

isArray

Checks if the given argument is an array.

Use Array.isArray() to check if a value is classified as an array.

const isArray = val => !!val && Array.isArray(val);
 
isArray(null); // false
 isArray([1]); // true
-

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use Array.from() and a try... catch block to check if the provided argument is array-like.

const isArrayLike = arr => {
-  try {
-    Array.from(arr);
-    return true;
-  } catch (e) {
-    return false;
-  }
-};
-
isArrayLike(document.querySelector('.className')); // true
+

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Check that the object is not a function or null and that its length property is a non-negative integer below Number.MAX_SAFE_INTEGER.

const isArrayLike = val =>
+  val != null &&
+  typeof val != 'function' &&
+  val.length > -1 &&
+  val.length % 1 == 0 &&
+  val.length <= Number.MAX_SAFE_INTEGER;
+
isArrayLike(document.querySelectorAll('.className')); // true
 isArrayLike('abc'); // true
 isArrayLike(null); // false
 

isBoolean

Checks if the given argument is a native boolean element.

Use typeof to check if a value is classified as a boolean primitive.

const isBoolean = val => typeof val === 'boolean';
diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md
index 051399caf..8d846e68f 100644
--- a/snippets/isArrayLike.md
+++ b/snippets/isArrayLike.md
@@ -5,8 +5,12 @@ Checks if the provided argument is array-like (i.e. is iterable).
 Check that the object is not a function or `null` and that its `length` property is a non-negative integer below `Number.MAX_SAFE_INTEGER`.
 
 ```js
-const isArrayLike = val => 
-  val != null && typeof val != 'function' && val.length > -1 && val.length % 1 == 0 && val.length <= Number.MAX_SAFE_INTEGER;
+const isArrayLike = val =>
+  val != null &&
+  typeof val != 'function' &&
+  val.length > -1 &&
+  val.length % 1 == 0 &&
+  val.length <= Number.MAX_SAFE_INTEGER;
 ```
 
 ```js