-
Notifications
You must be signed in to change notification settings - Fork 15
/
tap.js
135 lines (129 loc) · 3.8 KB
/
tap.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const isPromise = require('./_internal/isPromise')
const always = require('./_internal/always')
const thunkifyArgs = require('./_internal/thunkifyArgs')
const thunkConditional = require('./_internal/thunkConditional')
const curry2 = require('./_internal/curry2')
const curry3 = require('./_internal/curry3')
const curryArgs2 = require('./_internal/curryArgs2')
const curryArgs3 = require('./_internal/curryArgs3')
const __ = require('./_internal/placeholder')
const areAnyValuesPromises = require('./_internal/areAnyValuesPromises')
const promiseAll = require('./_internal/promiseAll')
// _tap(args Array, f function) -> Promise|any
const _tap = function (args, f) {
const result = args[0],
call = f(...args)
return isPromise(call) ? call.then(always(result)) : result
}
/**
* @name tap
*
* @synopsis
* ```coffeescript [specscript]
* tap(...args, f function) -> Promise|args[0]
* tap(f function)(...args) -> Promise|args[0]
* ```
*
* @description
* Call a function with provided arguments, returning the first argument. The return value of the function call is discarded.
*
* ```javascript [playground]
* const pipeline = pipe([
* tap(value => console.log(value)),
* tap(value => console.log(value + 'bar')),
* tap(value => console.log(value + 'barbaz')),
* ])
*
* pipeline('foo') // 'foo'
* // 'foobar'
* // 'foobarbaz'
* ```
*
* Any promises passed in argument position are resolved for their values before further execution. This only applies to the eager version of the API.
*
* ```javascript [playground]
* tap(Promise.resolve(1), Promise.resolve(2), 3, console.log) // 1 2 3
* ```
*/
const tap = function (...args) {
const f = args.pop()
if (args.length == 0) {
return curryArgs2(_tap, __, f)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(_tap, __, f))
}
return _tap(args, f)
}
/**
* @name _tapIf
*
* @synopsis
* ```coffeescript [specscript]
* _tapIf(
* predicate function,
* f function,
* args Array,
* ) -> Promise|args[0]
* ```
*/
const _tapIf = function (predicate, f, args) {
const b = predicate(...args)
if (isPromise(b)) {
return b.then(curry3(
thunkConditional,
__,
thunkifyArgs(tap(f), args),
always(args[0]),
))
}
if (b) {
const execution = f(...args)
if (isPromise(execution)) {
return execution.then(always(args[0]))
}
}
return args[0]
}
/**
* @name tap.if
*
* @synopsis
* ```coffeescript [specscript]
* tap.if(...args, predicate function, f function) -> Promise|args[0]
* tap.if(predicate function, f function)(...args) -> Promise|args[0]
* ```
*
* @description
* A version of `tap` that accepts a predicate function (a function that returns a boolean value) before the function `f` to execute. Only executes `f` if the predicate function tests true. The arguments are the same to both the predicate function and the function to execute `f`.
*
* ```javascript [playground]
* const isOdd = number => number % 2 == 1
*
* const logIfOdd = tap.if(isOdd, console.log)
*
* logIfOdd(2)
* logIfOdd(3) // 3
* ```
*
* Any promises passed in argument position are resolved for their values before further execution. This only applies to the eager version of the API.
*
* ```javascript [playground]
* tap.if(Promise.resolve(1), n => n < 5, console.log) // 1
* tap.if(Promise.resolve(6), n => n < 5, console.log)
* ```
*/
tap.if = function (...args) {
if (args.length == 2) {
return curryArgs3(_tapIf, args[0], args[1], __)
}
const argsLength = args.length
const f = args[argsLength - 1]
const predicate = args[argsLength - 2]
const argValues = args.slice(0, -2)
if (areAnyValuesPromises(argValues)) {
return promiseAll(argValues).then(curry3(_tapIf, predicate, f, __))
}
return _tapIf(predicate, f, args)
}
module.exports = tap