/
tap.ts
50 lines (46 loc) · 1.33 KB
/
tap.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { purry } from "./purry";
/**
* Calls the given function with the given value, then returns the given value.
* The return value of the provided function is ignored.
*
* This allows "tapping into" a function sequence in a pipe, to perform side
* effects on intermediate results.
*
* @param value - The value to pass into the function.
* @param fn - The function to call.
* @signature
* R.tap(value, fn)
* @example
* R.tap("foo", console.log) // => "foo"
* @dataFirst
* @category Other
*/
export function tap<T>(value: T, fn: (value: T) => void): T;
/**
* Calls the given function with the given value, then returns the given value.
* The return value of the provided function is ignored.
*
* This allows "tapping into" a function sequence in a pipe, to perform side
* effects on intermediate results.
*
* @param fn - The function to call.
* @signature
* R.tap(fn)(value)
* @example
* R.pipe(
* [-5, -1, 2, 3],
* R.filter(n => n > 0),
* R.tap(console.log), // prints [2, 3]
* R.map(n => n * 2)
* ) // => [4, 6]
* @dataLast
* @category Other
*/
export function tap<T, F extends (value: T) => unknown>(fn: F): (value: T) => T;
export function tap(): unknown {
return purry(_tap, arguments);
}
function _tap<T>(value: T, fn: (value: T) => void): T {
fn(value);
return value;
}