Skip to content

Instantly share code, notes, and snippets.

@hapticdata
Created December 15, 2015 21:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hapticdata/a33074a45ad5278872c0 to your computer and use it in GitHub Desktop.
Save hapticdata/a33074a45ad5278872c0 to your computer and use it in GitHub Desktop.
/**
* create a function that will return true once any time the value changes
* @param {Object} object
* @param {String} key
*/
function watchProperty(object, key){
var value = object[key];
return function(){
var changed = value !== object[key];
if(changed){
value =object[key];
}
return changed;
};
}
module.exports = exports = watchProperty;
/**
* watch an array of properties on an object
* if any of the values have changed return true
* @returns true if any of the values have changed
*/
exports.any = function some(object, keys){
var fns = keys.map(function(key){
return watchProperty(object, key);
});
return function(){
for(var i=0; i<fns.length; i++){
if( fns[i]() ){
return true;
}
}
return false;
};
};
/**
* watch an array of properties on an object
* if all of them have changed return true
* @returns true if all objects have changed value
*/
exports.all = function every(object, keys){
var fns = keys.map(function(key){
return watchProperty(object, key);
});
return function(){
for(var i=0; i<fns.length; i++){
if( !fns[i]() ){
return false;
}
}
return true;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment