Created
March 12, 2015 06:40
-
-
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.
This file contains hidden or 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
/** | |
* 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