Aller au contenu principal

thru()

thru<T, R>(value, interceptor): R

Invokes interceptor with the value, then returns the result. Useful for transformations in a pipeline.

DEPRECATED

Use direct function application or pipe instead.


Type Parameters

T: T

The type of the input value.

R: R

The type of the result.


Parameters

value: T

The value to pass to interceptor.

interceptor: (value) => R

The function to invoke.


Returns: R

The result of the interceptor.


Since

2.0.0


Also known as

thru (Lodash) · ❌ (es-toolkit, Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Example

// ❌ Deprecated approach
thru([1, 2, 3], arr => arr.map(n => n * 2));
// => [2, 4, 6]

// ✅ Recommended approach (direct)
[1, 2, 3].map(n => n * 2);
// => [2, 4, 6]

// ✅ Recommended approach (in pipe)
import { pipe } from '@pithos/arkhe/function/pipe';

pipe(
[1, 2, 3],
arr => arr.map(n => n * 2),
arr => arr.filter(n => n > 2)
);
// => [4, 6]

How it works?

Passes value through a transformer function. Deprecated: Just call the function directly.

Native Equivalent

// ❌ thru(value, fn)
// ✅ fn(value)

Use Cases

Transform in chain 📌

Apply transformation in chain.

// Instead of thru, just use variables or pipes
const filtered = data.filter(x => x > 0);
const chunked = chunk(filtered, 3);
const result = chunked.map(process);

Wrap intermediate result

Transform result before continuing.

const process = (arr) => {
const filtered = arr.filter(Boolean);
const transformed = customTransform(filtered);
return transformed.map(finalize);
};

Compose operations

Chain operations with function composition.

const result = [filter, transform, finalize]
.reduce((acc, fn) => fn(acc), data);