Skip to main content

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);