Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 851 Bytes

File metadata and controls

34 lines (28 loc) · 851 Bytes
title category
Filter by predicates
Array

JavaScript version

const filterByPredicates = (arr, predicates, condition = "and") => arr.filter(item => 
  condition === "and" ? predicates.every(func => func(item)) : predicates.some(func => func(item))
)

TypeScript version

const filterByPredicates = <T, _>(
  arr: T[], 
  predicates: Array<(val: T) => boolean>, 
  condition: "and" | "or" = "and"
): T[] => arr.filter(item => 
  condition === "and" ? predicates.every(func => func(item)) : predicates.some(func => func(item))
)

Example

const nums = [-2, -1, 0, 1, 2, 3.1];
const isEven = num => num % 2 === 0;
const isPositive = num => num > 0;
filterByPredicates(nums, [isPositive, Number.isInteger], "and"); // [1, 2]
filterByPredicates(nums, [isEven, isPositive], "or"); // [-2, 0, 1, 2, 3.1]