Skip to content

Instantly share code, notes, and snippets.

@0xlkda
Created September 24, 2020 11:53
Show Gist options
  • Save 0xlkda/fb77b5664c3030551c6140ccbb67dc6f to your computer and use it in GitHub Desktop.
Save 0xlkda/fb77b5664c3030551c6140ccbb67dc6f to your computer and use it in GitHub Desktop.
Filter by list of conditions
const filterBy = (conditions, index = 0) => (value) => {
if(conditions[index] === undefined) return value
if(!conditions[index](value)) return
return filterBy(conditions, index + 1)(value)
}
const values = [1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 25, 27]
const odd = (value) => value % 2 !== 0
const greaterThan3 = (value) => value > 3
const canDivide3 = (value) => value % 3 === 0
const conditions = [odd, greaterThan3, canDivide3]
const results = values.map(filterBy(conditions)).filter(x => x)
console.log(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment