Skip to main content

Function: by()

by<T, U>(selector, predicate): Predicate<T>

Defined in: predicate/builders/index.ts:319

Creates a predicate by composing a selector function with a predicate.

Useful for testing derived values or nested properties.

Type Parameters

T

T

U

U

Parameters

selector

(value) => U

Function to extract a value from the input

predicate

Predicate<U>

Predicate to apply to the extracted value

Returns

Predicate<T>

A predicate that tests the selected value

Example

import * as R from 'remeda'
import { by, gt } from 'receta/predicate'

const users = [
{ name: 'Alice', tags: ['admin', 'user'] },
{ name: 'Bob', tags: ['user'] },
{ name: 'Charlie', tags: ['admin', 'moderator', 'user'] }
]

// Test derived values
R.filter(
users,
by((u) => u.tags.length, gt(1))
)
// => Alice and Charlie (have multiple tags)

// Real-world: Filter by computed values
const products = [
{ name: 'A', price: 100, discount: 0.1 },
{ name: 'B', price: 50, discount: 0.2 },
{ name: 'C', price: 150, discount: 0 }
]
R.filter(
products,
by((p) => p.price * (1 - p.discount), gt(80))
)
// => products with final price > 80

See

  • prop - for simple property access
  • where - for multiple property testing