From 9bc97b851ad89db8eda3ceb0af06158c879fb8d2 Mon Sep 17 00:00:00 2001 From: Sean Hamilton Date: Mon, 3 Dec 2018 09:58:21 +0000 Subject: [PATCH] fix: check if iterator is a function --- src/each.js | 10 +++------- src/every.js | 2 +- src/filter.js | 2 +- src/find.js | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/each.js b/src/each.js index 110ec83b..fcc77514 100644 --- a/src/each.js +++ b/src/each.js @@ -13,7 +13,7 @@ import deflate from './deflate'; function each(object, iterator, follow = false) { // check if the object is an object and isn't empty // if it is it would be pointless running the forEach - if (is(object) && !empty(object)) { + if (is(object) && !empty(object) && typeof iterator === 'function') { // if follow is true flatten the object keys so // its easy to get the path and values if follow // is false it will just be the base object @@ -27,12 +27,8 @@ function each(object, iterator, follow = false) { // get the value of the current key const value = flattenedObject[key]; - // check if the iterator is a function - if (typeof iterator === 'function') { - // if it is run the iterator with the - // key and value - iterator(key, value); - } + // run the iterator with the key and value + iterator(key, value); }); } } diff --git a/src/every.js b/src/every.js index 4a04f8ef..2ce5f8d0 100644 --- a/src/every.js +++ b/src/every.js @@ -14,7 +14,7 @@ import each from './each'; */ function every(object, iterator, follow = false) { // if the object is an object and is not empty - if (is(object) && !empty(object)) { + if (is(object) && !empty(object) && typeof iterator === 'function') { // set the result to true so we can change it // to false if the iterator fails let result = true; diff --git a/src/filter.js b/src/filter.js index 149ac388..5a600fec 100644 --- a/src/filter.js +++ b/src/filter.js @@ -16,7 +16,7 @@ import del from './del'; */ function filter(object, iterator, follow = false) { // if the object is an object and is not empty - if (is(object) && !empty(object)) { + if (is(object) && !empty(object) && typeof iterator === 'function') { // create a clone of the object to manipulate let result = clone(object); diff --git a/src/find.js b/src/find.js index cdc75346..1b13c147 100644 --- a/src/find.js +++ b/src/find.js @@ -14,7 +14,7 @@ import each from './each'; */ function find(object, iterator, follow) { // if the object is an object and is not empty - if (is(object) && !empty(object)) { + if (is(object) && !empty(object) && typeof iterator === 'function') { // create an result variable as undefined let found = false; let result = '';