Skip to content

Commit

Permalink
feat(every): add every function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hamilton committed Nov 30, 2018
1 parent e9a34e9 commit 7777204
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/every.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// o
import is from './is';
import empty from './empty';
import each from './each';

/**
* Check every element in an object evaluate to the iterator
*
* @param {object} object The object to check
* @param {function(key: string, value: *)} iterator The function to evaluate
* @param {boolean} follow Whether to follow objects
*
* @returns {boolean} Whether all objects evaluate to the iterator
*/
function every(object, iterator, follow = false) {
// if the object is an object and is not empty
if (is(object) && !empty(object)) {
// set the result to true so we can change it
// to false if the iterator fails
let result = true;

// for each over the object keys and values
// follow is passed into each therefore the
// each function works out whether to follow
// the objects
each(object, (key, value) => {
// run the iterator function on the key and
// value and if it evaluates to false set
// the result to false
if (iterator(key, value) === false) {
// set the result to false
result = false;
}
}, follow);

// return the result
return result;
}

// if the object isn't an object or is empty return false
// because the iterator can't be ran to make a check
return false;
}

export default every;

0 comments on commit 7777204

Please # to comment.