Last active
August 29, 2015 14:01
-
-
Save iammapping/5e979da604527a7e411a to your computer and use it in GitHub Desktop.
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
/** | |
* set goals to do | |
* | |
* Usage: | |
* | |
* var meet = enoughToDo('a', 'b', function(goals) { | |
* // Congratulations! All goals met | |
* // It's enough to do the final callback | |
* console.log(goals); | |
* }); | |
* meet('a'); | |
* meet('b'); // all goals met | |
* | |
* @param {String} goals | |
* @param {Function} callback trigger while all goals met | |
* @return {Function} meet function | |
*/ | |
var enoughToDo = function(/*[goal, ..], cb*/) { | |
var self = this, | |
args = Array.prototype.slice.call(arguments, 0), | |
callback = typeof args[args.length - 1] == 'function' ? args.pop() : function() {}, | |
goals = args, | |
meets = []; | |
return function() { | |
meets = meets.concat(Array.prototype.slice.call(arguments, 0)); | |
// check goals met | |
if (goals.length | |
== | |
goals.filter(function(v) { | |
return meets.indexOf(v) > -1; | |
}).length | |
) { | |
callback(goals, meets); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment