Skip to content

Instantly share code, notes, and snippets.

@iammapping
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iammapping/5e979da604527a7e411a to your computer and use it in GitHub Desktop.
Save iammapping/5e979da604527a7e411a to your computer and use it in GitHub Desktop.
/**
* 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