-
Notifications
You must be signed in to change notification settings - Fork 15
/
all.js
217 lines (209 loc) · 6.02 KB
/
all.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const isPromise = require('./_internal/isPromise')
const areAnyValuesPromises = require('./_internal/areAnyValuesPromises')
const areAllValuesNonfunctions = require('./_internal/areAllValuesNonfunctions')
const promiseAll = require('./_internal/promiseAll')
const promiseObjectAll = require('./_internal/promiseObjectAll')
const isArray = require('./_internal/isArray')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const curryArgs2 = require('./_internal/curryArgs2')
const functionArrayAll = require('./_internal/functionArrayAll')
const functionArrayAllSeries = require('./_internal/functionArrayAllSeries')
const functionObjectAll = require('./_internal/functionObjectAll')
/**
* @name _allValues
*
* @synopsis
* ```coffeescript [specscript]
* _allValues(values Array<Promise|any>) -> Promise<Array>
* _allValues(values Object<Promise|any>) -> Promise<Object>
* ```
*/
const _allValues = function (values) {
if (isArray(values)) {
return areAnyValuesPromises(values)
? promiseAll(values)
: values
}
return areAnyValuesPromises(values)
? promiseObjectAll(values)
: values
}
/**
* @name all
*
* @synopsis
* ```coffeescript [specscript]
* all(values Promise|Array<Promise|any>) -> result Promise|Array
* all(values Promise|Object<Promise|any>) -> result Promise|Object
*
* all(
* ...args,
* resolversOrValues Array<function|Promise|any>
* ) -> result Promise|Array
*
* all(
* resolversOrValues Array<function|Promise|any>
* )(...args) -> result Promise|Array
*
* all(
* ...args,
* resolversOrValues Object<function|Promise|any>
* ) -> result Promise|Object
*
* all(
* resolversOrValues Object<function|Promise|any>
* )(...args) -> result Promise|Object
* ```
*
* @description
* Calls an array or object of resolver functions or values `resolversOrValues` with provided arguments.
*
* ```javascript [playground]
* const createArrayOfGreetingsFor = all([
* name => `Hi ${name}`,
* name => `Hey ${name}`,
* name => `Hello ${name}`,
* ])
*
* const arrayOfGreetingsFor1 = createArrayOfGreetingsFor('1')
*
* console.log(arrayOfGreetingsFor1)
* // ['Hi 1', 'Hey 1', 'Hello 1']
* ```
*
* If provided only values for `resolversOrValues`, returns an array or object with the same shape as `resolversOrValues` with any Promises resolved.
*
* ```javascript [playground]
* all([
* Promise.resolve(1),
* Promise.resolve(2),
* 3,
* ]).then(console.log) // [1, 2, 3]
*
* all({
* a: Promise.resolve(1),
* b: Promise.resolve(2),
* c: 3,
* }).then(console.log) // { a: 1, b: 2, c: 3 }
* ```
*
* `all` can be used in a pipeline to compose and manpulate data.
*
* ```javascript [playground]
* const identity = value => value
*
* const userbase = new Map()
* userbase.set('1', { _id: 1, name: 'George' })
*
* const getUserByID = async id => userbase.get(id)
*
* const getAndLogUserById = pipe([
* all({
* id: identity,
* user: getUserByID,
* }),
* tap(({ id, user }) => {
* console.log(`Got user ${JSON.stringify(user)} by id ${id}`)
* }),
* ])
*
* getAndLogUserById('1') // Got user {"_id":1,"name":"George"} by id 1
* ```
*
* Values may be provided along with functions, in which case they are set on the result object or array directly. If any of these values are promises, they are resolved for their values before being set on the result object or array.
*
* ```javascript [playground]
* all({}, {
* a: Promise.resolve(1),
* b: 2,
* c: () => 3,
* d: async () => 4,
* }).then(console.log) // { a: 1, b: 2, c: 3, d: 4 }
*
* all([], [
* Promise.resolve(1),
* 2,
* () => 3,
* async () => 4,
* ]).then(console.log) // [1, 2, 3, 4]
* ```
*
* 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]
* all(Promise.resolve({ a: 1 }), [
* obj => obj.a + 1,
* obj => obj.a + 2,
* obj => obj.a + 3,
* ]).then(console.log) // [2, 3, 4]
* ```
*
* @execution concurrent
*/
const all = function (...args) {
if (args.length == 1) {
const resolversOrValues = args[0]
if (isPromise(resolversOrValues)) {
return resolversOrValues.then(_allValues)
}
if (areAllValuesNonfunctions(resolversOrValues)) {
return _allValues(resolversOrValues)
}
return isArray(resolversOrValues)
? curryArgs2(functionArrayAll, resolversOrValues, __)
: curryArgs2(functionObjectAll, resolversOrValues, __)
}
const resolversOrValues = args[args.length - 1]
const argValues = args.slice(0, -1)
if (areAnyValuesPromises(argValues)) {
return isArray(resolversOrValues)
? promiseAll(argValues)
.then(curry2(functionArrayAll, resolversOrValues, __))
: promiseAll(argValues)
.then(curry2(functionObjectAll, resolversOrValues, __))
}
return isArray(resolversOrValues)
? functionArrayAll(resolversOrValues, argValues)
: functionObjectAll(resolversOrValues, argValues)
}
/**
* @name all.series
*
* @synopsis
* ```coffeescript [specscript]
* all.series(...args, funcsArray Array<function>) -> result Promise|Array
*
* all.series(funcsArray Array<function>)(...args) -> result Promise|Array
* ```
*
* @description
* `all` with serial execution.
*
* ```javascript [playground]
* const sleep = ms => () => new Promise(resolve => setTimeout(resolve, ms))
*
* all.series([
* greeting => console.log(greeting + ' world'),
* sleep(1000),
* greeting => console.log(greeting + ' mom'),
* sleep(1000),
* greeting => console.log(greeting + ' goodbye'),
* ])('hello') // hello world
* // hello mom
* // hello goodbye
* ```
*
* @execution series
*/
all.series = function allSeries(...args) {
const funcs = args.pop()
if (args.length == 0) {
return curryArgs2(functionArrayAllSeries, funcs, __)
}
if (areAnyValuesPromises(args)) {
return promiseAll(args).then(curry2(functionArrayAllSeries, funcs, __))
}
return functionArrayAllSeries(funcs, args)
}
module.exports = all