Skip to content

Instantly share code, notes, and snippets.

@founddrama
Created March 2, 2012 20:24
Show Gist options
  • Save founddrama/1961042 to your computer and use it in GitHub Desktop.
Save founddrama/1961042 to your computer and use it in GitHub Desktop.
/**
* Scans the provided filter object for filters matching `inStack`.
*
* @method _filter
* @private
* @param {Object} filter An object representing a set of filters for a given object
* @param {Array} inStack An array of path properties [ 'a', 'b', 'c' ] is a path to { a: { b: { c: '...' } } } that will be used to look for whitelist entries.
* @param {Object} inObject An object representing the value at the end of `s`. In the above example, it would be '...'. Passed into the filter function, if one exists.
* @return {Boolean} True if this value should be returned by the object serializer, false if not.
**/
NS._filter = function(filter, inStack, inObject) {
var stack = inStack.slice(0), // we're going to be modifying the provided array, so create a clone
shouldInclude = false, // by default, exclude all the things
pathArray = [], // an array representing increasingly-specific filter paths
pathString; // memoize the path string generated by join, so we aren't doing it 3 times
while(stack.length) {
pathArray.push(stack.splice(0,1));
pathString = path.join('.');
if(filter[pathString] === true) {
shouldInclude = true;
} else if(filter[pathString] === false) {
shouldInclude = false;
} else if(YLang.isFunction(filter[pathString])) {
shouldInclude = filter[path](stack, inObject);
}
}
return shouldInclude;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment