From 6857bfcedc56ddcf580f2d72fbe345a28d24a189 Mon Sep 17 00:00:00 2001 From: Bruce Feldman Date: Mon, 13 Aug 2018 15:43:39 -0400 Subject: [PATCH 1/2] Simplified isArrayLike --- snippets/isArrayLike.md | 10 +--------- test/isArrayLike/isArrayLike.js | 8 +------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index 5417f1974..5e44bb473 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -2,16 +2,8 @@ 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 -const isArrayLike = val => { - try { - return [...val], true; - } catch (e) { - return false; - } -}; +const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function'; ``` ```js diff --git a/test/isArrayLike/isArrayLike.js b/test/isArrayLike/isArrayLike.js index c603d5bc5..144b181d0 100644 --- a/test/isArrayLike/isArrayLike.js +++ b/test/isArrayLike/isArrayLike.js @@ -1,8 +1,2 @@ -const isArrayLike = val => { - try { - return [...val], true; - } catch (e) { - return false; - } -}; +const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function'; module.exports = isArrayLike; From 26c3724d55f44b077d28e38e8c2de5738879fca3 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 14 Aug 2018 20:37:58 +0300 Subject: [PATCH 2/2] Updated description --- snippets/isArrayLike.md | 2 ++ test/initializeArrayWithRange/initializeArrayWithRange.js | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index 5e44bb473..088ab4e52 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -2,6 +2,8 @@ Checks if the provided argument is array-like (i.e. is iterable). +Check if the provided argument is not `null` and that its `Symbol.iterator` property is a function. + ```js const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function'; ``` diff --git a/test/initializeArrayWithRange/initializeArrayWithRange.js b/test/initializeArrayWithRange/initializeArrayWithRange.js index e29b1c842..a096edd8d 100644 --- a/test/initializeArrayWithRange/initializeArrayWithRange.js +++ b/test/initializeArrayWithRange/initializeArrayWithRange.js @@ -1,3 +1,3 @@ const initializeArrayWithRange = (end, start = 0, step = 1) => - Array.from({length: Math.ceil((end - start + 1) / step)}, (v, i) => i * step + start); + Array.from({ length: Math.ceil((end - start + 1) / step) }, (v, i) => i * step + start); module.exports = initializeArrayWithRange;