Skip to content

Instantly share code, notes, and snippets.

@jpiccari
Created March 12, 2015 06:40
Show Gist options
  • Save jpiccari/11c2c30eb4d121b6be5c to your computer and use it in GitHub Desktop.
Save jpiccari/11c2c30eb4d121b6be5c to your computer and use it in GitHub Desktop.
Find the smallest, non-empty, array given a set of successively applied filters.
/**
* Succesively filter an array until the minimum result set is found
* @param {array} array - Array to be filtered
* @param {function...} fn1..fnN - Any number of filter functions
* @returns {array} The smallest non-empty array given the set of filters
*/
function successiveFilter(array /* ... */) {
var prevArray,
fnArray = Array.prototype.slice.call(arguments, 1);
while (array.length > 1 && fnArray.length) {
prevArray = array;
array = prevArray.filter(fnArray.shift());
}
return array.length ? array : prevArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment