-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sean Hamilton
committed
Nov 30, 2018
1 parent
e9a34e9
commit 7777204
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |