-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpairwise.ts
33 lines (32 loc) · 1.03 KB
/
pairwise.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { Pipeline } from '../types';
import { isPromise } from '../utils/is-promise';
/**
* Decorator that returns the previous and current value as array
*
* @param {Pipeline.StepFunction<TIn, TOut>} fn - selector function to select the array to loop
* @returns {Pipeline.StepFunction<TIn, [TIn, TOut]>}
*
* @example
*
* const { pairwise, step, pipe } = require('@axa/bautajs-core');
*
* const getMovie = step(() => ({ name: 'star wars' }));
* const getMovieImdbIdAsync = step<{ name: string }, string>(() => Promise.resolve('imdb12354'));
*
* const pipeline = pipe(getMovie, pairwise(getMovieImdbIdAsync), ([movie, imdbId]) => ({
* ...movie,
* imdb_id: imdbId
* }));
*
*/
export function pairwise<TIn, TOut>(
fn: Pipeline.StepFunction<TIn, TOut>
): Pipeline.StepFunction<TIn, [TIn, TOut]> {
return (prev: TIn, ctx, bautajs) => {
const result = fn(prev, ctx, bautajs);
if (isPromise(result)) {
return (result as Promise<TOut>).then((r: TOut) => [prev, r]);
}
return [prev, result as TOut];
};
}