any
any
(predicate: Predicate) => (collection: Collection) => boolean
Test if any entry returns truthy
const gt2 = n => n > 2 const anyGt2 = any(gt2) anyGt2([1, 2]) // is false anyGt2([1, 2, 3]) // is true anyGt2({ a: 1, b: 2 }) // is false anyGt2({ a: 1, b: 2, c: 3 }) // is true
const gt2 = (n: number) => n > 2 const anyGt2 = any(gt2) anyGt2([1, 2]) // is false anyGt2([1, 2, 3]) // is true anyGt2({ a: 1, b: 2 }) // is false anyGt2({ a: 1, b: 2, c: 3 }) // is true
const gt2 = n => n > 2 const anyGt2 = any(gt2) anyGt2([1, 2]) // is false anyGt2([1, 2, 3]) // is true anyGt2({ a: 1, b: 2 }) // is false anyGt2({ a: 1, b: 2, c: 3 }) // is true
const gt2 = (n: number) => n > 2 const anyGt2 = any(gt2) anyGt2([1, 2]) // is false anyGt2([1, 2, 3]) // is true anyGt2({ a: 1, b: 2 }) // is false anyGt2({ a: 1, b: 2, c: 3 }) // is true
We often want to test if any entry passes a condition. Below, we see if anyone's birthday is today. If so, we should sing!
const today = { month: 'March', date: 2 } const isBirthday = bday => bday.month === today.month && bday.date === today.date const customerBirthdays = { mary: { month: 'March', date: 2 }, sarah: { month: 'July', date: 15 }, } const isAnyonesBday = any(isBirthday) const shouldSing = isAnyonesBday(customerBirthdays) console.log('should sing: ' + shouldSing)
type MonthDate = { month: string; date: number } const today = { month: 'March', date: 2 } const isBirthday = (bday: MonthDate) => bday.month === today.month && bday.date === today.date type Birthdays = Record<string, MonthDate> const customerBirthdays: Birthdays = { mary: { month: 'March', date: 2 }, sarah: { month: 'July', date: 15 }, } const isAnyonesBday = any(isBirthday)<Birthdays> const shouldSing = isAnyonesBday(customerBirthdays) console.log('should sing: ' + shouldSing)
const today = { month: 'March', date: 2 } const isBirthday = bday => bday.month === today.month && bday.date === today.date const customerBirthdays = { mary: { month: 'March', date: 2 }, sarah: { month: 'July', date: 15 }, } const isAnyonesBday = any(isBirthday) const shouldSing = isAnyonesBday( customerBirthdays ) console.log('should sing: ' + shouldSing)
type MonthDate = { month: string; date: number } const today = { month: 'March', date: 2 } const isBirthday = (bday: MonthDate) => bday.month === today.month && bday.date === today.date type Birthdays = Record<string, MonthDate> const customerBirthdays: Birthdays = { mary: { month: 'March', date: 2 }, sarah: { month: 'July', date: 15 }, } const isAnyonesBday = any(isBirthday)<Birthdays> const shouldSing = isAnyonesBday( customerBirthdays ) console.log('should sing: ' + shouldSing)