For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/function/when.md.

when

Returns a function that applies a transform only when a predicate passes, otherwise returns the value unchanged.

const trimIfString = when(isString, (s: string) => s.trim())
trimIfString('  hello  ') // 'hello'

Usage

when<T>(predicate: (value: T) => boolean, fn: (value: T) => T)

Returns a function that applies a transform only when a predicate passes, otherwise returns the value unchanged.

import { when } from 'massaman'
// or:  import { when } from 'massaman/function'

const trimIfString = when(isString, (s: string) => s.trim())
trimIfString('  hello  ') // 'hello'

Parameters

predicate(value: T) => booleanrequired

the condition used to select behavior.

fn(value: T) => Trequired

the transformation to apply when predicate returns true.

Returns

a function that transforms values only when predicate returns true.

(value: T) => T