-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathforEach.js
162 lines (157 loc) · 5.35 KB
/
forEach.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
const isPromise = require('./_internal/isPromise')
const __ = require('./_internal/placeholder')
const curry2 = require('./_internal/curry2')
const isArray = require('./_internal/isArray')
const isGeneratorFunction = require('./_internal/isGeneratorFunction')
const isAsyncGeneratorFunction = require('./_internal/isAsyncGeneratorFunction')
const arrayForEach = require('./_internal/arrayForEach')
const objectForEach = require('./_internal/objectForEach')
const iteratorForEach = require('./_internal/iteratorForEach')
const asyncIteratorForEach = require('./_internal/asyncIteratorForEach')
const symbolIterator = require('./_internal/symbolIterator')
const symbolAsyncIterator = require('./_internal/symbolAsyncIterator')
const arrayForEachSeries = require('./_internal/arrayForEachSeries')
const objectForEachSeries = require('./_internal/objectForEachSeries')
const iteratorForEachSeries = require('./_internal/iteratorForEachSeries')
const asyncIteratorForEachSeries = require('./_internal/asyncIteratorForEachSeries')
// type Collection = Array|Iterable|AsyncIterable|{ forEach: function }|Object
// _forEach(collection Collection, callback function) -> collection Collection
const _forEach = function (collection, callback) {
if (isArray(collection)) {
return arrayForEach(collection, callback)
}
if (collection == null) {
return collection
}
if (typeof collection.forEach == 'function') {
collection.forEach(callback)
return collection
}
if (typeof collection[symbolIterator] == 'function') {
return iteratorForEach(collection[symbolIterator](), callback)
}
if (typeof collection[symbolAsyncIterator] == 'function') {
return asyncIteratorForEach(collection[symbolAsyncIterator](), callback)
}
if (collection.constructor == Object) {
return objectForEach(collection, callback)
}
return collection
}
/**
* @name forEach
*
* @synopsis
* ```coffeescript [specscript]
* type Collection = Array|Iterable|AsyncIterable|{ forEach: function }|Object
*
* forEach(collection Collection, callback function) -> collection Promise|Collection
*
* forEach(callback function)(collection Collection) -> collection Promise|Collection
* ```
*
* @description
* Execute a callback for each item of a collection, returning a Promise if the execution is asynchronous. Asynchronous execution happens concurrently.
*
* ```javascript [playground]
* forEach([1, 2, 3, 4, 5], console.log) // 1 2 3 4 5
*
* forEach({ a: 1, b: 2, c: 3 }, console.log) // 1 2 3
* ```
*
* Omit the data argument for a composable API
*
* ```javascript [playground]
* pipe([1, 2, 3, 4, 5], [
* filter(number => number % 2 == 1),
* map(number => number ** 2),
* forEach(console.log), // 1
* // 9
* // 25
* ])
* ```
*
* 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]
* forEach(Promise.resolve([1, 2, 3]), console.log)
* // 1
* // 2
* // 3
* ```
*/
const forEach = function (arg0, arg1) {
if (typeof arg0 == 'function') {
return curry2(_forEach, __, arg0)
}
return isPromise(arg0)
? arg0.then(curry2(_forEach, __, arg1))
: _forEach(arg0, arg1)
}
/**
* @name _forEachSeries
*
* @synopsis
* ```coffeescript [specscript]
* type Collection = Array|Iterable|AsyncIterable|{ forEach: function }|Object
*
* _forEachSeries(collection Collection, callback function) -> collection Promise|Collection
* ```
*/
const _forEachSeries = function (collection, callback) {
if (isArray(collection)) {
return arrayForEachSeries(collection, callback)
}
if (collection == null) {
throw new TypeError(`invalid collection ${collection}`)
}
if (typeof collection[symbolIterator] == 'function') {
return iteratorForEachSeries(collection[symbolIterator](), callback)
}
if (typeof collection[symbolAsyncIterator] == 'function') {
return asyncIteratorForEachSeries(collection[symbolAsyncIterator](), callback)
}
if (collection.constructor == Object) {
return objectForEachSeries(collection, callback)
}
throw new TypeError(`invalid collection ${collection}`)
}
/**
* @name forEach.series
*
* @synopsis
* ```coffeescript [specscript]
* type Collection = Array|Iterable|AsyncIterable|{ forEach: function }|Object
*
* forEach.series(collection Collection, callback function) -> collection Promise|Collection
*
* forEach.series(callback function)(collection Collection) -> collection Promise|Collection
* ```
*
* @description
* Execute a callback for each item of a collection, returning a Promise if the execution is asynchronous. Asynchronous execution happens in series.
*
* ```javascript [playground]
* forEach.series([1, 2, 3, 4, 5], console.log) // 1 2 3 4 5
*
* forEach.series({ a: 1, b: 2, c: 3 }, console.log) // 1 2 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]
* forEach.series(Promise.resolve([1, 2, 3]), console.log)
* // 1
* // 2
* // 3
* ```
*/
forEach.series = function forEachSeries(arg0, arg1) {
if (typeof arg0 == 'function') {
return curry2(_forEachSeries, __, arg0)
}
return isPromise(arg0)
? arg0.then(curry2(_forEachSeries, __, arg1))
: _forEachSeries(arg0, arg1)
}
module.exports = forEach