> For AI agents: the complete documentation index is available at https://massaman.dev/llms.txt, the full documentation bundle is available at https://massaman.dev/llms-full.txt.

# when

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

```typescript
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.

```typescript
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`.

```typescript
(value: T) => T
```
