Skip to content

Commit

Permalink
fix: check if iterator is a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hamilton committed Dec 3, 2018
1 parent bbed4d3 commit 9bc97b8
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
10 changes: 3 additions & 7 deletions src/each.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/every.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down

0 comments on commit 9bc97b8

Please # to comment.