From a9ac0b40ce51b4d2403cd3040de23c0d68a5917e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 17:18:19 +0200 Subject: [PATCH] Update isArrayLike.md --- snippets/isArrayLike.md | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index 36eae12a9..ee2fc1718 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -2,17 +2,11 @@ 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; ``` ```js