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