Created
December 15, 2015 21:55
-
-
Save hapticdata/a33074a45ad5278872c0 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
/** | |
* 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