rubico 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +158 -81
- package/Transducer.js +28 -0
- package/Transducer.test.js +134 -97
- package/_internal/reducerTryCatch.js +55 -0
- package/build +257 -280
- package/dist/Transducer.es.js +508 -0
- package/dist/Transducer.es.min.js +7 -0
- package/dist/Transducer.js +515 -0
- package/dist/Transducer.min.js +8 -0
- package/dist/Transducer.mjs +508 -0
- package/dist/__.es.js +1 -1
- package/dist/__.es.min.js +1 -1
- package/dist/__.js +1 -1
- package/dist/__.min.js +1 -1
- package/dist/__.mjs +1 -1
- package/dist/all.es.js +1 -1
- package/dist/all.es.min.js +1 -1
- package/dist/all.js +1 -1
- package/dist/all.min.js +1 -1
- package/dist/all.mjs +1 -1
- package/dist/always.es.js +1 -1
- package/dist/always.es.min.js +1 -1
- package/dist/always.js +1 -1
- package/dist/always.min.js +1 -1
- package/dist/always.mjs +1 -1
- package/dist/and.es.js +1 -1
- package/dist/and.es.min.js +1 -1
- package/dist/and.js +1 -1
- package/dist/and.min.js +1 -1
- package/dist/and.mjs +1 -1
- package/dist/assign.es.js +1 -1
- package/dist/assign.es.min.js +1 -1
- package/dist/assign.js +1 -1
- package/dist/assign.min.js +1 -1
- package/dist/assign.mjs +1 -1
- package/dist/compose.es.js +1 -1
- package/dist/compose.es.min.js +1 -1
- package/dist/compose.js +1 -1
- package/dist/compose.min.js +1 -1
- package/dist/compose.mjs +1 -1
- package/dist/curry.es.js +1 -1
- package/dist/curry.es.min.js +1 -1
- package/dist/curry.js +1 -1
- package/dist/curry.min.js +1 -1
- package/dist/curry.mjs +1 -1
- package/dist/eq.es.js +1 -1
- package/dist/eq.es.min.js +1 -1
- package/dist/eq.js +1 -1
- package/dist/eq.min.js +1 -1
- package/dist/eq.mjs +1 -1
- package/dist/every.es.js +1 -1
- package/dist/every.es.min.js +1 -1
- package/dist/every.js +1 -1
- package/dist/every.min.js +1 -1
- package/dist/every.mjs +1 -1
- package/dist/filter.es.js +1 -1
- package/dist/filter.es.min.js +1 -1
- package/dist/filter.js +1 -1
- package/dist/filter.min.js +1 -1
- package/dist/filter.mjs +1 -1
- package/dist/flatMap.es.js +1 -1
- package/dist/flatMap.es.min.js +1 -1
- package/dist/flatMap.js +1 -1
- package/dist/flatMap.min.js +1 -1
- package/dist/flatMap.mjs +1 -1
- package/dist/forEach.es.js +1 -1
- package/dist/forEach.es.min.js +1 -1
- package/dist/forEach.js +1 -1
- package/dist/forEach.min.js +1 -1
- package/dist/forEach.mjs +1 -1
- package/dist/get.es.js +1 -1
- package/dist/get.es.min.js +1 -1
- package/dist/get.js +1 -1
- package/dist/get.min.js +1 -1
- package/dist/get.mjs +1 -1
- package/dist/gt.es.js +1 -1
- package/dist/gt.es.min.js +1 -1
- package/dist/gt.js +1 -1
- package/dist/gt.min.js +1 -1
- package/dist/gt.mjs +1 -1
- package/dist/gte.es.js +1 -1
- package/dist/gte.es.min.js +1 -1
- package/dist/gte.js +1 -1
- package/dist/gte.min.js +1 -1
- package/dist/gte.mjs +1 -1
- package/dist/lt.es.js +1 -1
- package/dist/lt.es.min.js +1 -1
- package/dist/lt.js +1 -1
- package/dist/lt.min.js +1 -1
- package/dist/lt.mjs +1 -1
- package/dist/lte.es.js +1 -1
- package/dist/lte.es.min.js +1 -1
- package/dist/lte.js +1 -1
- package/dist/lte.min.js +1 -1
- package/dist/lte.mjs +1 -1
- package/dist/map.es.js +1 -1
- package/dist/map.es.min.js +1 -1
- package/dist/map.js +1 -1
- package/dist/map.min.js +1 -1
- package/dist/map.mjs +1 -1
- package/dist/not.es.js +1 -1
- package/dist/not.es.min.js +1 -1
- package/dist/not.js +1 -1
- package/dist/not.min.js +1 -1
- package/dist/not.mjs +1 -1
- package/dist/omit.es.js +1 -1
- package/dist/omit.es.min.js +1 -1
- package/dist/omit.js +1 -1
- package/dist/omit.min.js +1 -1
- package/dist/omit.mjs +1 -1
- package/dist/or.es.js +1 -1
- package/dist/or.es.min.js +1 -1
- package/dist/or.js +1 -1
- package/dist/or.min.js +1 -1
- package/dist/or.mjs +1 -1
- package/dist/pick.es.js +1 -1
- package/dist/pick.es.min.js +1 -1
- package/dist/pick.js +1 -1
- package/dist/pick.min.js +1 -1
- package/dist/pick.mjs +1 -1
- package/dist/pipe.es.js +1 -1
- package/dist/pipe.es.min.js +1 -1
- package/dist/pipe.js +1 -1
- package/dist/pipe.min.js +1 -1
- package/dist/pipe.mjs +1 -1
- package/dist/reduce.es.js +1 -1
- package/dist/reduce.es.min.js +1 -1
- package/dist/reduce.js +1 -1
- package/dist/reduce.min.js +1 -1
- package/dist/reduce.mjs +1 -1
- package/dist/rubico.es.js +28 -10
- package/dist/rubico.es.min.js +2 -2
- package/dist/rubico.global.js +2794 -0
- package/dist/rubico.global.min.js +8 -0
- package/dist/rubico.js +15 -6
- package/dist/rubico.min.js +2 -2
- package/dist/rubico.mjs +28 -10
- package/dist/set.es.js +1 -1
- package/dist/set.es.min.js +1 -1
- package/dist/set.js +1 -1
- package/dist/set.min.js +1 -1
- package/dist/set.mjs +1 -1
- package/dist/some.es.js +1 -1
- package/dist/some.es.min.js +1 -1
- package/dist/some.js +1 -1
- package/dist/some.min.js +1 -1
- package/dist/some.mjs +1 -1
- package/dist/switchCase.es.js +1 -1
- package/dist/switchCase.es.min.js +1 -1
- package/dist/switchCase.js +1 -1
- package/dist/switchCase.min.js +1 -1
- package/dist/switchCase.mjs +1 -1
- package/dist/tap.es.js +1 -1
- package/dist/tap.es.min.js +1 -1
- package/dist/tap.js +1 -1
- package/dist/tap.min.js +1 -1
- package/dist/tap.mjs +1 -1
- package/dist/thunkify.es.js +1 -1
- package/dist/thunkify.es.min.js +1 -1
- package/dist/thunkify.js +1 -1
- package/dist/thunkify.min.js +1 -1
- package/dist/thunkify.mjs +1 -1
- package/dist/transform.es.js +1 -1
- package/dist/transform.es.min.js +1 -1
- package/dist/transform.js +1 -1
- package/dist/transform.min.js +1 -1
- package/dist/transform.mjs +1 -1
- package/dist/tryCatch.es.js +1 -1
- package/dist/tryCatch.es.min.js +1 -1
- package/dist/tryCatch.js +1 -1
- package/dist/tryCatch.min.js +1 -1
- package/dist/tryCatch.mjs +1 -1
- package/dist/x/append.es.js +1 -1
- package/dist/x/append.es.min.js +1 -1
- package/dist/x/append.js +1 -1
- package/dist/x/append.min.js +1 -1
- package/dist/x/append.mjs +1 -1
- package/dist/x/callProp.es.js +1 -1
- package/dist/x/callProp.es.min.js +1 -1
- package/dist/x/callProp.js +1 -1
- package/dist/x/callProp.min.js +1 -1
- package/dist/x/callProp.mjs +1 -1
- package/dist/x/defaultsDeep.es.js +1 -1
- package/dist/x/defaultsDeep.es.min.js +1 -1
- package/dist/x/defaultsDeep.js +1 -1
- package/dist/x/defaultsDeep.min.js +1 -1
- package/dist/x/defaultsDeep.mjs +1 -1
- package/dist/x/differenceWith.es.js +1 -1
- package/dist/x/differenceWith.es.min.js +1 -1
- package/dist/x/differenceWith.js +1 -1
- package/dist/x/differenceWith.min.js +1 -1
- package/dist/x/differenceWith.mjs +1 -1
- package/dist/x/filterOut.es.js +1 -1
- package/dist/x/filterOut.es.min.js +1 -1
- package/dist/x/filterOut.js +1 -1
- package/dist/x/filterOut.min.js +1 -1
- package/dist/x/filterOut.mjs +1 -1
- package/dist/x/find.es.js +1 -1
- package/dist/x/find.es.min.js +1 -1
- package/dist/x/find.js +1 -1
- package/dist/x/find.min.js +1 -1
- package/dist/x/find.mjs +1 -1
- package/dist/x/findIndex.es.js +1 -1
- package/dist/x/findIndex.es.min.js +1 -1
- package/dist/x/findIndex.js +1 -1
- package/dist/x/findIndex.min.js +1 -1
- package/dist/x/findIndex.mjs +1 -1
- package/dist/x/first.es.js +1 -1
- package/dist/x/first.es.min.js +1 -1
- package/dist/x/first.js +1 -1
- package/dist/x/first.min.js +1 -1
- package/dist/x/first.mjs +1 -1
- package/dist/x/flatten.es.js +1 -1
- package/dist/x/flatten.es.min.js +1 -1
- package/dist/x/flatten.js +1 -1
- package/dist/x/flatten.min.js +1 -1
- package/dist/x/flatten.mjs +1 -1
- package/dist/x/groupBy.es.js +1 -1
- package/dist/x/groupBy.es.min.js +1 -1
- package/dist/x/groupBy.js +1 -1
- package/dist/x/groupBy.min.js +1 -1
- package/dist/x/groupBy.mjs +1 -1
- package/dist/x/has.es.js +1 -1
- package/dist/x/has.es.min.js +1 -1
- package/dist/x/has.js +1 -1
- package/dist/x/has.min.js +1 -1
- package/dist/x/has.mjs +1 -1
- package/dist/x/identity.es.js +1 -1
- package/dist/x/identity.es.min.js +1 -1
- package/dist/x/identity.js +1 -1
- package/dist/x/identity.min.js +1 -1
- package/dist/x/identity.mjs +1 -1
- package/dist/x/includes.es.js +1 -1
- package/dist/x/includes.es.min.js +1 -1
- package/dist/x/includes.js +1 -1
- package/dist/x/includes.min.js +1 -1
- package/dist/x/includes.mjs +1 -1
- package/dist/x/isDeepEqual.es.js +1 -1
- package/dist/x/isDeepEqual.es.min.js +1 -1
- package/dist/x/isDeepEqual.js +1 -1
- package/dist/x/isDeepEqual.min.js +1 -1
- package/dist/x/isDeepEqual.mjs +1 -1
- package/dist/x/isEmpty.es.js +1 -1
- package/dist/x/isEmpty.es.min.js +1 -1
- package/dist/x/isEmpty.js +1 -1
- package/dist/x/isEmpty.min.js +1 -1
- package/dist/x/isEmpty.mjs +1 -1
- package/dist/x/isEqual.es.js +1 -1
- package/dist/x/isEqual.es.min.js +1 -1
- package/dist/x/isEqual.js +1 -1
- package/dist/x/isEqual.min.js +1 -1
- package/dist/x/isEqual.mjs +1 -1
- package/dist/x/isFunction.es.js +1 -1
- package/dist/x/isFunction.es.min.js +1 -1
- package/dist/x/isFunction.js +1 -1
- package/dist/x/isFunction.min.js +1 -1
- package/dist/x/isFunction.mjs +1 -1
- package/dist/x/isIn.es.js +1 -1
- package/dist/x/isIn.es.min.js +1 -1
- package/dist/x/isIn.js +1 -1
- package/dist/x/isIn.min.js +1 -1
- package/dist/x/isIn.mjs +1 -1
- package/dist/x/isObject.es.js +1 -1
- package/dist/x/isObject.es.min.js +1 -1
- package/dist/x/isObject.js +1 -1
- package/dist/x/isObject.min.js +1 -1
- package/dist/x/isObject.mjs +1 -1
- package/dist/x/isString.es.js +1 -1
- package/dist/x/isString.es.min.js +1 -1
- package/dist/x/isString.js +1 -1
- package/dist/x/isString.min.js +1 -1
- package/dist/x/isString.mjs +1 -1
- package/dist/x/keys.es.js +1 -1
- package/dist/x/keys.es.min.js +1 -1
- package/dist/x/keys.js +1 -1
- package/dist/x/keys.min.js +1 -1
- package/dist/x/keys.mjs +1 -1
- package/dist/x/last.es.js +1 -1
- package/dist/x/last.es.min.js +1 -1
- package/dist/x/last.js +1 -1
- package/dist/x/last.min.js +1 -1
- package/dist/x/last.mjs +1 -1
- package/dist/x/maxBy.es.js +1 -1
- package/dist/x/maxBy.es.min.js +1 -1
- package/dist/x/maxBy.js +1 -1
- package/dist/x/maxBy.min.js +1 -1
- package/dist/x/maxBy.mjs +1 -1
- package/dist/x/noop.es.js +1 -1
- package/dist/x/noop.es.min.js +1 -1
- package/dist/x/noop.js +1 -1
- package/dist/x/noop.min.js +1 -1
- package/dist/x/noop.mjs +1 -1
- package/dist/x/pluck.es.js +1 -1
- package/dist/x/pluck.es.min.js +1 -1
- package/dist/x/pluck.js +1 -1
- package/dist/x/pluck.min.js +1 -1
- package/dist/x/pluck.mjs +1 -1
- package/dist/x/prepend.es.js +1 -1
- package/dist/x/prepend.es.min.js +1 -1
- package/dist/x/prepend.js +1 -1
- package/dist/x/prepend.min.js +1 -1
- package/dist/x/prepend.mjs +1 -1
- package/dist/x/size.es.js +1 -1
- package/dist/x/size.es.min.js +1 -1
- package/dist/x/size.js +1 -1
- package/dist/x/size.min.js +1 -1
- package/dist/x/size.mjs +1 -1
- package/dist/x/trace.es.js +1 -1
- package/dist/x/trace.es.min.js +1 -1
- package/dist/x/trace.js +1 -1
- package/dist/x/trace.min.js +1 -1
- package/dist/x/trace.mjs +1 -1
- package/dist/x/unionWith.es.js +1 -1
- package/dist/x/unionWith.es.min.js +1 -1
- package/dist/x/unionWith.js +1 -1
- package/dist/x/unionWith.min.js +1 -1
- package/dist/x/unionWith.mjs +1 -1
- package/dist/x/uniq.es.js +1 -1
- package/dist/x/uniq.es.min.js +1 -1
- package/dist/x/uniq.js +1 -1
- package/dist/x/uniq.min.js +1 -1
- package/dist/x/uniq.mjs +1 -1
- package/dist/x/unless.es.js +1 -1
- package/dist/x/unless.es.min.js +1 -1
- package/dist/x/unless.js +1 -1
- package/dist/x/unless.min.js +1 -1
- package/dist/x/unless.mjs +1 -1
- package/dist/x/values.es.js +1 -1
- package/dist/x/values.es.min.js +1 -1
- package/dist/x/values.js +1 -1
- package/dist/x/values.min.js +1 -1
- package/dist/x/values.mjs +1 -1
- package/dist/x/when.es.js +1 -1
- package/dist/x/when.es.min.js +1 -1
- package/dist/x/when.js +1 -1
- package/dist/x/when.min.js +1 -1
- package/dist/x/when.mjs +1 -1
- package/dist-test.js +26 -4
- package/es.js +15 -6
- package/index.js +15 -6
- package/package.json +1 -1
- package/rubico.js +14 -5
- package/tmp.js +1 -0
- package/dist/__.min.mjs +0 -7
- package/dist/all.min.mjs +0 -7
- package/dist/always.min.mjs +0 -7
- package/dist/and.min.mjs +0 -7
- package/dist/assign.min.mjs +0 -7
- package/dist/compose.min.mjs +0 -7
- package/dist/curry.min.mjs +0 -7
- package/dist/eq.min.mjs +0 -7
- package/dist/every.min.mjs +0 -7
- package/dist/filter.min.mjs +0 -7
- package/dist/flatMap.min.mjs +0 -7
- package/dist/forEach.min.mjs +0 -7
- package/dist/get.min.mjs +0 -7
- package/dist/gt.min.mjs +0 -7
- package/dist/gte.min.mjs +0 -7
- package/dist/lt.min.mjs +0 -7
- package/dist/lte.min.mjs +0 -7
- package/dist/map.min.mjs +0 -7
- package/dist/not.min.mjs +0 -7
- package/dist/omit.min.mjs +0 -7
- package/dist/or.min.mjs +0 -7
- package/dist/pick.min.mjs +0 -7
- package/dist/pipe.min.mjs +0 -7
- package/dist/reduce.min.mjs +0 -7
- package/dist/rubico.min.mjs +0 -7
- package/dist/set.min.mjs +0 -7
- package/dist/some.min.mjs +0 -7
- package/dist/switchCase.min.mjs +0 -7
- package/dist/tap.min.mjs +0 -7
- package/dist/thunkify.min.mjs +0 -7
- package/dist/transform.min.mjs +0 -7
- package/dist/tryCatch.min.mjs +0 -7
- package/dist/x/append.min.mjs +0 -7
- package/dist/x/callProp.min.mjs +0 -7
- package/dist/x/defaultsDeep.min.mjs +0 -7
- package/dist/x/differenceWith.min.mjs +0 -7
- package/dist/x/filterOut.min.mjs +0 -7
- package/dist/x/find.min.mjs +0 -7
- package/dist/x/findIndex.min.mjs +0 -7
- package/dist/x/first.min.mjs +0 -7
- package/dist/x/flatten.min.mjs +0 -7
- package/dist/x/groupBy.min.mjs +0 -7
- package/dist/x/has.min.mjs +0 -7
- package/dist/x/identity.min.mjs +0 -7
- package/dist/x/includes.min.mjs +0 -7
- package/dist/x/isDeepEqual.min.mjs +0 -7
- package/dist/x/isEmpty.min.mjs +0 -7
- package/dist/x/isEqual.min.mjs +0 -7
- package/dist/x/isFunction.min.mjs +0 -7
- package/dist/x/isIn.min.mjs +0 -7
- package/dist/x/isObject.min.mjs +0 -7
- package/dist/x/isString.min.mjs +0 -7
- package/dist/x/keys.min.mjs +0 -7
- package/dist/x/last.min.mjs +0 -7
- package/dist/x/maxBy.min.mjs +0 -7
- package/dist/x/noop.min.mjs +0 -7
- package/dist/x/pluck.min.mjs +0 -7
- package/dist/x/prepend.min.mjs +0 -7
- package/dist/x/size.min.mjs +0 -7
- package/dist/x/trace.min.mjs +0 -7
- package/dist/x/unionWith.min.mjs +0 -7
- package/dist/x/uniq.min.mjs +0 -7
- package/dist/x/unless.min.mjs +0 -7
- package/dist/x/values.min.mjs +0 -7
- package/dist/x/when.min.mjs +0 -7
package/README.md
CHANGED
|
@@ -19,90 +19,68 @@ const isOdd = number => number % 2 == 1
|
|
|
19
19
|
|
|
20
20
|
const asyncSquare = async number => number ** 2
|
|
21
21
|
|
|
22
|
-
const
|
|
22
|
+
const numbers = [1, 2, 3, 4, 5]
|
|
23
|
+
|
|
24
|
+
pipe(numbers, [
|
|
23
25
|
filter(isOdd),
|
|
24
26
|
map(asyncSquare),
|
|
27
|
+
console.log, // [1, 9, 25]
|
|
25
28
|
])
|
|
26
|
-
|
|
27
|
-
squaredOdds([1, 2, 3, 4, 5]).then(console.log) // [1, 9, 25]
|
|
28
29
|
```
|
|
29
30
|
|
|
30
31
|
# Installation
|
|
31
|
-
[Core build](https://unpkg.com/rubico/index.js) ([~6.
|
|
32
|
+
[Core build](https://unpkg.com/rubico/index.js) ([~6.8 kB minified and gzipped](https://unpkg.com/rubico/dist/rubico.min.js))
|
|
32
33
|
|
|
33
34
|
with `npm`
|
|
34
35
|
```bash
|
|
35
36
|
npm i rubico
|
|
36
37
|
```
|
|
37
38
|
|
|
38
|
-
require `rubico` in
|
|
39
|
+
require `rubico` in CommonJS.
|
|
39
40
|
```javascript
|
|
41
|
+
// import rubico core globally
|
|
42
|
+
require('rubico/global')
|
|
43
|
+
|
|
44
|
+
// import rubico core as rubico
|
|
40
45
|
const rubico = require('rubico')
|
|
46
|
+
|
|
47
|
+
// import an operator from rubico core
|
|
41
48
|
const pipe = require('rubico/pipe')
|
|
42
|
-
|
|
49
|
+
|
|
50
|
+
// import rubico/x as x
|
|
51
|
+
const x = require('rubico/x')
|
|
52
|
+
|
|
53
|
+
// import an operator from rubico/x
|
|
43
54
|
const defaultsDeep = require('rubico/x/defaultsDeep')
|
|
55
|
+
|
|
56
|
+
// import rubico's Transducer module
|
|
57
|
+
const Transducer = require('rubico/Transducer')
|
|
44
58
|
```
|
|
45
59
|
|
|
46
|
-
import `rubico`
|
|
60
|
+
import `rubico` in the browser.
|
|
47
61
|
```html [htmlmixed]
|
|
48
|
-
|
|
49
|
-
<script src="https://unpkg.com/rubico/dist/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
<script>
|
|
53
|
-
console.log(rubico) // { pipe, tap, ... }
|
|
54
|
-
console.log(pipe) // [Function: pipe]
|
|
55
|
-
console.log(tap) // [Function: tap]
|
|
56
|
-
console.log(defaultsDeep) // [Function: defaultsDeep]
|
|
57
|
-
</script>
|
|
58
|
-
|
|
59
|
-
<!-- minified -->
|
|
62
|
+
<!-- import rubico core globally -->
|
|
63
|
+
<script src="https://unpkg.com/rubico/dist/global.min.js"></script>
|
|
64
|
+
|
|
65
|
+
<!-- import rubico core as rubico -->
|
|
60
66
|
<script src="https://unpkg.com/rubico/dist/rubico.min.js"></script>
|
|
67
|
+
|
|
68
|
+
<!-- import an operator from rubico core -->
|
|
61
69
|
<script src="https://unpkg.com/rubico/dist/pipe.min.js"></script>
|
|
62
|
-
<script src="https://unpkg.com/rubico/dist/tap.min.js"></script>
|
|
63
|
-
<script src="https://unpkg.com/rubico/dist/x/defaultsDeep.min.js"></script>
|
|
64
|
-
<script>
|
|
65
|
-
console.log(rubico) // { pipe, tap, ... }
|
|
66
|
-
console.log(pipe) // [Function: pipe]
|
|
67
|
-
console.log(tap) // [Function: tap]
|
|
68
|
-
console.log(defaultsDeep) // [Function: defaultsDeep]
|
|
69
|
-
</script>
|
|
70
|
-
```
|
|
71
70
|
|
|
72
|
-
import
|
|
73
|
-
|
|
74
|
-
import rubico from 'https://unpkg.com/rubico/dist/rubico.es.js'
|
|
75
|
-
import pipe from 'https://unpkg.com/rubico/dist/pipe.es.js'
|
|
76
|
-
import tap from 'https://unpkg.com/rubico/dist/tap.es.js'
|
|
77
|
-
import defaultsDeep from 'https://unpkg.com/rubico/dist/x/defaultsDeep.es.js'
|
|
78
|
-
|
|
79
|
-
// minified
|
|
80
|
-
import rubico from 'https://unpkg.com/rubico/dist/rubico.es.min.js'
|
|
81
|
-
import pipe from 'https://unpkg.com/rubico/dist/pipe.es.min.js'
|
|
82
|
-
import tap from 'https://unpkg.com/rubico/dist/tap.es.min.js'
|
|
83
|
-
import defaultsDeep from 'https://unpkg.com/rubico/dist/x/defaultsDeep.es.min.js'
|
|
84
|
-
```
|
|
71
|
+
<!-- import an operator from rubico/x -->
|
|
72
|
+
<script src="https://unpkg.com/rubico/dist/x/defaultsDeep.min.js"></script>
|
|
85
73
|
|
|
86
|
-
import
|
|
87
|
-
|
|
88
|
-
import rubico from 'rubico/dist/rubico.mjs'
|
|
89
|
-
import pipe from 'rubico/dist/pipe.mjs'
|
|
90
|
-
import tap from 'rubico/dist/tap.mjs'
|
|
91
|
-
import defaultsDeep from 'rubico/dist/x/defaultsDeep.mjs'
|
|
92
|
-
|
|
93
|
-
// minified
|
|
94
|
-
import rubico from 'rubico/dist/rubico.min.mjs'
|
|
95
|
-
import pipe from 'rubico/dist/pipe.min.mjs'
|
|
96
|
-
import tap from 'rubico/dist/tap.min.mjs'
|
|
97
|
-
import defaultsDeep from 'rubico/dist/x/defaultsDeep.min.mjs'
|
|
74
|
+
<!-- import rubico's Transducer module -->
|
|
75
|
+
<script src="https://unpkg.com/rubico/dist/Transducer.min.js"></script>
|
|
98
76
|
```
|
|
99
77
|
|
|
100
78
|
# Motivation
|
|
101
79
|
|
|
102
80
|
A note from the author
|
|
103
|
-
> At a certain point in my career, I grew frustrated with the entanglement of my own code. While looking for something better, I found functional programming. I was excited by the idea of functional composition, but disillusioned by the redundancy of effectful types. I started
|
|
81
|
+
> At a certain point in my career, I grew frustrated with the entanglement of my own code. While looking for something better, I found functional programming. I was excited by the idea of functional composition, but disillusioned by the redundancy of effectful types. I started rubico to capitalize on the prior while rebuking the latter. Many iterations since then, the library has grown into something I personally enjoy using, and continue to use to this day.
|
|
104
82
|
|
|
105
|
-
|
|
83
|
+
rubico is founded on the following principles:
|
|
106
84
|
* asynchronous code should be simple
|
|
107
85
|
* functional style should not care about async
|
|
108
86
|
* functional transformations should be composable, performant, and simple to express
|
|
@@ -111,54 +89,153 @@ When you import this library, you obtain the freedom that comes from having thos
|
|
|
111
89
|
|
|
112
90
|
# Introduction
|
|
113
91
|
|
|
114
|
-
|
|
92
|
+
rubico is a library for async-enabled functional programming in JavaScript. The library methods support a simple and composable functional style in asynchronous environments.
|
|
115
93
|
|
|
116
94
|
```javascript
|
|
117
95
|
const {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
96
|
+
// compose functions
|
|
97
|
+
pipe, compose,
|
|
98
|
+
|
|
99
|
+
// compose effects
|
|
100
|
+
tap, forEach,
|
|
101
|
+
|
|
102
|
+
// control flow
|
|
103
|
+
switchCase,
|
|
104
|
+
|
|
105
|
+
// handle errors
|
|
106
|
+
tryCatch,
|
|
107
|
+
|
|
108
|
+
// compose objects
|
|
109
|
+
all, assign, get, set, pick, omit,
|
|
110
|
+
|
|
111
|
+
// transform data
|
|
121
112
|
map, filter, reduce, transform, flatMap,
|
|
122
|
-
|
|
113
|
+
|
|
114
|
+
// compose predicates
|
|
115
|
+
and, or, not, some, every,
|
|
116
|
+
|
|
117
|
+
// comparison operators
|
|
123
118
|
eq, gt, lt, gte, lte,
|
|
124
|
-
|
|
125
|
-
|
|
119
|
+
|
|
120
|
+
// partial application
|
|
121
|
+
thunkify, always, curry, __,
|
|
126
122
|
} = rubico
|
|
127
123
|
```
|
|
128
124
|
|
|
129
|
-
|
|
125
|
+
With async-enabled, or [a]synchronous, functional programming, functions provided to the rubico methods may be asynchronous and return a Promise. Any promises in argument position are also resolved before continuing with the operation.
|
|
130
126
|
|
|
131
127
|
```javascript [playground]
|
|
132
|
-
const
|
|
128
|
+
const helloPromise = Promise.resolve('hello')
|
|
133
129
|
|
|
134
|
-
|
|
130
|
+
pipe(helloPromise, [ // helloPromise is resolved for 'hello'
|
|
131
|
+
async greeting => `${greeting} world`,
|
|
132
|
+
// the Promise returned from the async function is resolved
|
|
133
|
+
// and the resolved value is passed to console.log
|
|
135
134
|
|
|
136
|
-
|
|
137
|
-
toTodosUrl,
|
|
138
|
-
fetch,
|
|
139
|
-
response => response.json(),
|
|
140
|
-
console.log,
|
|
135
|
+
console.log, // hello world
|
|
141
136
|
])
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Most methods support both an eager and a tacit API. The eager API takes all required arguments and executes at once, while its tacit API takes only the non-data arguments and executes lazily, returning a function that expects the data arguments. This dual API supports a natural and composable code style.
|
|
140
|
+
|
|
141
|
+
```javascript [playground]
|
|
142
|
+
const myObj = { a: 1, b: 2, c: 3 }
|
|
143
|
+
|
|
144
|
+
// the first use of map is eager
|
|
145
|
+
const myDuplicatedSquaredObject = map(myObj, pipe([
|
|
146
|
+
number => [number, number],
|
|
147
|
+
|
|
148
|
+
// the second use of map is tacit
|
|
149
|
+
map(number => number ** 2),
|
|
150
|
+
]))
|
|
151
|
+
|
|
152
|
+
console.log(myDuplicatedSquaredObject)
|
|
153
|
+
// { a: [1, 1], b: [4, 4], c: [9, 9] }
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The rubico methods are versatile and act on a wide range of vanilla JavaScript types to create declarative, extensible, and async-enabled function compositions. The same operator `map` can act on an array and also a `Map` data structure.
|
|
157
|
+
|
|
158
|
+
```javascript [playground]
|
|
159
|
+
const { pipe, tap, map, filter } = rubico
|
|
160
|
+
|
|
161
|
+
const toTodosUrl = id => `https://jsonplaceholder.typicode.com/todos/${id}`
|
|
142
162
|
|
|
143
163
|
const todoIDs = [1, 2, 3, 4, 5]
|
|
144
164
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
165
|
+
pipe(todoIDs, [
|
|
166
|
+
|
|
167
|
+
// fetch todos per id of todoIDs
|
|
168
|
+
map(pipe([
|
|
169
|
+
toTodosUrl,
|
|
170
|
+
fetch,
|
|
171
|
+
response => response.json(),
|
|
172
|
+
|
|
173
|
+
tap(console.log),
|
|
174
|
+
// { userId: 1, id: 4, title: 'et porro tempora', completed: true }
|
|
175
|
+
// { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
|
|
176
|
+
// { userId: 1, id: 3, title: 'fugiat veniam minus', completed: false }
|
|
177
|
+
// { userId: 1, id: 2, title: 'quis ut nam facilis...', completed: false }
|
|
178
|
+
// { userId: 1, id: 5, title: 'laboriosam mollitia...', completed: false }
|
|
179
|
+
])),
|
|
180
|
+
|
|
181
|
+
// group the todos by userId in a new Map
|
|
182
|
+
function createUserTodosMap(todos) {
|
|
183
|
+
const userTodosMap = new Map()
|
|
184
|
+
for (const todo of todos) {
|
|
185
|
+
const { userId } = todo
|
|
186
|
+
if (userTodosMap.has(userId)) {
|
|
187
|
+
userTodosMap.get(userId).push(todo)
|
|
188
|
+
} else {
|
|
189
|
+
userTodosMap.set(userId, [todo])
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return userTodosMap
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
// filter for completed todos
|
|
196
|
+
// map iterates through each value (array of todos) of the userTodosMap
|
|
197
|
+
// filter iterates through each todo of the arrays of todos
|
|
198
|
+
map(filter(function didComplete(todo) {
|
|
199
|
+
return todo.completed
|
|
200
|
+
})),
|
|
201
|
+
|
|
202
|
+
tap(console.log),
|
|
203
|
+
// Map(1) {
|
|
204
|
+
// 1 => [ { userId: 1, id: 4, title: 'et porro tempora', completed: true } ]
|
|
205
|
+
// }
|
|
206
|
+
])
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
rubico offers transducers in its `Transducer` module. You can consume these transducers with the `transform` and `compose` methods. You should use `compose` over `pipe` to chain a left-to-right composition of transducers.
|
|
151
210
|
|
|
152
|
-
|
|
153
|
-
|
|
211
|
+
```javascript [playground]
|
|
212
|
+
const isOdd = number => number % 2 == 1
|
|
213
|
+
|
|
214
|
+
const asyncSquare = async number => number ** 2
|
|
215
|
+
|
|
216
|
+
const generateNumbers = function* () {
|
|
217
|
+
yield 1
|
|
218
|
+
yield 2
|
|
219
|
+
yield 3
|
|
220
|
+
yield 4
|
|
221
|
+
yield 5
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
pipe(generateNumbers(), [
|
|
225
|
+
transform(compose([
|
|
226
|
+
Transducer.filter(isOdd),
|
|
227
|
+
Transducer.map(asyncSquare),
|
|
228
|
+
]), []),
|
|
229
|
+
console.log, // [1, 9, 25]
|
|
230
|
+
])
|
|
154
231
|
```
|
|
155
232
|
|
|
156
|
-
For advanced asynchronous use cases,
|
|
233
|
+
For advanced asynchronous use cases, some of the methods have property functions that have different asynchronous behavior, e.g.
|
|
157
234
|
* `map` - apply a mapper function concurrently
|
|
158
235
|
* `map.pool` - apply a mapper function concurrently with a concurrency limit
|
|
159
236
|
* `map.series` - apply a mapper function serially
|
|
160
237
|
|
|
161
|
-
For
|
|
238
|
+
For more functions beyond the core methods, please visit `rubico/x`. You can find the full documentation at [rubico.land/docs](https://rubico.land/docs).
|
|
162
239
|
|
|
163
240
|
# Contributing
|
|
164
241
|
Your feedback and contributions are welcome. If you have a suggestion, please raise an issue. Prior to that, please search through the issues first in case your suggestion has been made already. If you decide to work on an issue, or feel like taking initiative and contributing anything at all, feel free to create a pull request and I will get back to you shortly.
|
|
@@ -166,7 +243,7 @@ Your feedback and contributions are welcome. If you have a suggestion, please ra
|
|
|
166
243
|
Pull requests should provide some basic context and link the relevant issue. Here is an [example pull request](https://github.com/a-synchronous/rubico/pull/12). If you are interested in contributing, the [help wanted](https://github.com/a-synchronous/rubico/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) tag is a good place to start.
|
|
167
244
|
|
|
168
245
|
# License
|
|
169
|
-
|
|
246
|
+
rubico is [MIT Licensed](https://github.com/a-synchronous/rubico/blob/master/LICENSE).
|
|
170
247
|
|
|
171
248
|
# Support
|
|
172
249
|
* minimum Node.js version: 12
|
package/Transducer.js
CHANGED
|
@@ -3,7 +3,9 @@ const reducerMap = require('./_internal/reducerMap')
|
|
|
3
3
|
const reducerFilter = require('./_internal/reducerFilter')
|
|
4
4
|
const reducerFlatMap = require('./_internal/reducerFlatMap')
|
|
5
5
|
const reducerForEach = require('./_internal/reducerForEach')
|
|
6
|
+
const reducerTryCatch = require('./_internal/reducerTryCatch')
|
|
6
7
|
const curry2 = require('./_internal/curry2')
|
|
8
|
+
const curry3 = require('./_internal/curry3')
|
|
7
9
|
const __ = require('./_internal/placeholder')
|
|
8
10
|
|
|
9
11
|
/**
|
|
@@ -155,8 +157,34 @@ Transducer.forEach = function transducerForEach(func) {
|
|
|
155
157
|
return curry2(reducerForEach, __, func)
|
|
156
158
|
}
|
|
157
159
|
|
|
160
|
+
/**
|
|
161
|
+
* @name Transducer.passthrough
|
|
162
|
+
*
|
|
163
|
+
* @synopsis
|
|
164
|
+
* ```coffeescript [specscript]
|
|
165
|
+
* type Reducer = (accumulator any, item any)=>(nextAccumulator Promise|any)
|
|
166
|
+
* type Transducer = Reducer=>Reducer
|
|
167
|
+
*
|
|
168
|
+
* Transducer.passthrough(func function) -> passthroughTransducer Transducer
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
158
171
|
Transducer.passthrough = function transducerPassthrough(reducer) {
|
|
159
172
|
return reducer
|
|
160
173
|
}
|
|
161
174
|
|
|
175
|
+
/**
|
|
176
|
+
* @name Transducer.tryCatch
|
|
177
|
+
*
|
|
178
|
+
* @synopsis
|
|
179
|
+
* ```coffeescript [specscript]
|
|
180
|
+
* type Reducer = (accumulator any, item any)=>(nextAccumulator Promise|any)
|
|
181
|
+
* type Transducer = Reducer=>Reducer
|
|
182
|
+
*
|
|
183
|
+
* Transducer.tryCatch(func function) -> tryCatchTransducer Transducer
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
Transducer.tryCatch = function transducerTryCatch(transducerTryer, catcher) {
|
|
187
|
+
return curry3(reducerTryCatch, __, transducerTryer, catcher)
|
|
188
|
+
}
|
|
189
|
+
|
|
162
190
|
module.exports = Transducer
|
package/Transducer.test.js
CHANGED
|
@@ -1,117 +1,154 @@
|
|
|
1
|
-
|
|
1
|
+
require('./global')
|
|
2
2
|
const Transducer = require('./Transducer')
|
|
3
|
-
const
|
|
4
|
-
const reduce = require('./reduce')
|
|
5
|
-
const transform = require('./transform')
|
|
3
|
+
const assert = require('assert')
|
|
6
4
|
|
|
7
5
|
describe('Transducer', () => {
|
|
8
6
|
const ade = assert.deepEqual
|
|
9
7
|
const ase = assert.strictEqual
|
|
10
8
|
const aok = assert.ok
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const isOdd = number => number % 2 == 1
|
|
10
|
+
const concat = (y, xi) => y.concat(xi)
|
|
11
|
+
const add = (y, xi) => y + xi
|
|
12
|
+
const isOdd = number => number % 2 == 1
|
|
16
13
|
|
|
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
|
-
|
|
14
|
+
it('reduce with sync transduced reducers', async () => {
|
|
15
|
+
const squareOdds = compose([
|
|
16
|
+
Transducer.filter(isOdd),
|
|
17
|
+
Transducer.map(x => x ** 2),
|
|
18
|
+
])
|
|
19
|
+
ade(
|
|
20
|
+
reduce(squareOdds(concat), [])([1, 2, 3, 4, 5]),
|
|
21
|
+
[1, 9, 25],
|
|
22
|
+
)
|
|
23
|
+
ade(
|
|
24
|
+
reduce(squareOdds((y, xi) => y.add(xi)), new Set())([1, 2, 3, 4, 5]),
|
|
25
|
+
new Set([1, 9, 25]),
|
|
26
|
+
)
|
|
27
|
+
const appendAlphas = compose([
|
|
28
|
+
Transducer.map(x => `${x}a`),
|
|
29
|
+
Transducer.map(x => `${x}b`),
|
|
30
|
+
Transducer.map(x => `${x}c`),
|
|
31
|
+
])
|
|
32
|
+
ase(
|
|
33
|
+
reduce(appendAlphas(add), '')('123'),
|
|
34
|
+
'1abc2abc3abc',
|
|
35
|
+
)
|
|
36
|
+
ade(
|
|
37
|
+
reduce(appendAlphas(concat), [])('123'),
|
|
38
|
+
['1abc', '2abc', '3abc'],
|
|
39
|
+
)
|
|
40
|
+
})
|
|
44
41
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
42
|
+
it('reduce with an async transduced reducer', async () => {
|
|
43
|
+
const hosWithHey = compose([
|
|
44
|
+
Transducer.filter(async x => x === 'ho'),
|
|
45
|
+
Transducer.map(x => Promise.resolve(`${x}hey`)),
|
|
46
|
+
])
|
|
47
|
+
const hihos = { a: 'hi', b: 'ho', c: 'hi', d: 'ho', e: 'hi', f: 'ho' }
|
|
48
|
+
aok(reduce(hosWithHey(add), '')(hihos) instanceof Promise),
|
|
49
|
+
aok(reduce(hosWithHey(concat), [])(hihos) instanceof Promise),
|
|
50
|
+
ase(
|
|
51
|
+
await reduce(hosWithHey(add), '')(hihos),
|
|
52
|
+
'hoheyhoheyhohey',
|
|
53
|
+
)
|
|
54
|
+
ade(
|
|
55
|
+
await reduce(hosWithHey(concat), [])(hihos),
|
|
56
|
+
['hohey', 'hohey', 'hohey'],
|
|
57
|
+
)
|
|
58
|
+
})
|
|
62
59
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
it('transform with error handling on a transducer composition', async () => {
|
|
61
|
+
const caughtErrors = []
|
|
62
|
+
const numbers = [1, 2, 3, 4, 5]
|
|
63
|
+
const result = await transform(numbers, Transducer.tryCatch(compose([
|
|
64
|
+
Transducer.filter(number => number % 2 == 1),
|
|
65
|
+
Transducer.map(number => number ** 2),
|
|
66
|
+
Transducer.map(number => {
|
|
67
|
+
throw new Error(number)
|
|
68
|
+
}),
|
|
69
|
+
Transducer.map(number => number * 10), // should never reach here
|
|
70
|
+
]), (error, item) => {
|
|
71
|
+
caughtErrors.push(error)
|
|
72
|
+
return item
|
|
73
|
+
}), [])
|
|
74
|
+
assert.equal(caughtErrors.length, 3)
|
|
75
|
+
assert.deepEqual(result, [1, 3, 5])
|
|
76
|
+
assert.deepEqual(caughtErrors.map(get('message')), ['1', '9', '25'])
|
|
77
|
+
})
|
|
68
78
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
79
|
+
it('transform with error handling on a transducer in a composition', async () => {
|
|
80
|
+
const caughtErrors = []
|
|
81
|
+
const numbers = [1, 2, 3, 4, 5]
|
|
82
|
+
const result = await transform(numbers, compose([
|
|
83
|
+
Transducer.filter(number => number % 2 == 1),
|
|
84
|
+
Transducer.map(number => number ** 2),
|
|
85
|
+
Transducer.tryCatch(
|
|
86
|
+
Transducer.map(number => {
|
|
87
|
+
throw new Error(number)
|
|
88
|
+
}),
|
|
89
|
+
(error, item) => {
|
|
90
|
+
caughtErrors.push(error)
|
|
91
|
+
return item
|
|
92
|
+
},
|
|
93
|
+
),
|
|
94
|
+
Transducer.map(number => number * 10), // should reach here with error handler
|
|
95
|
+
]), [])
|
|
96
|
+
assert.equal(caughtErrors.length, 3)
|
|
97
|
+
assert.deepEqual(result, [10, 90, 250])
|
|
98
|
+
assert.deepEqual(caughtErrors.map(get('message')), ['1', '9', '25'])
|
|
99
|
+
})
|
|
77
100
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
])
|
|
101
|
+
it('flatMapping transducer', async () => {
|
|
102
|
+
const createOddMultiplesSync = compose([
|
|
103
|
+
Transducer.filter(isOdd),
|
|
104
|
+
Transducer.flatMap(number => [number * 2, number * 3]),
|
|
105
|
+
])
|
|
82
106
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
ade(result, [2, 3, 6, 9])
|
|
92
|
-
})
|
|
93
|
-
})
|
|
107
|
+
ase(
|
|
108
|
+
reduce([1, 2, 3], createOddMultiplesSync(add), 0),
|
|
109
|
+
2 + 3 + 6 + 9,
|
|
110
|
+
)
|
|
111
|
+
ade(
|
|
112
|
+
reduce([1, 2, 3], createOddMultiplesSync(concat), []),
|
|
113
|
+
[2, 3, 6, 9],
|
|
114
|
+
)
|
|
94
115
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
})(() => {}), null)
|
|
100
|
-
assert.equal(sum1, 15)
|
|
116
|
+
const createOddMultiplesAsync = compose([
|
|
117
|
+
Transducer.filter(isOdd),
|
|
118
|
+
Transducer.flatMap(async number => [number * 2, number * 3]),
|
|
119
|
+
])
|
|
101
120
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
121
|
+
const p1 = reduce([1, 2, 3], createOddMultiplesAsync(add), 0)
|
|
122
|
+
aok(p1 instanceof Promise)
|
|
123
|
+
const p2 = reduce([1, 2, 3], createOddMultiplesAsync(concat), [])
|
|
124
|
+
aok(p2 instanceof Promise)
|
|
125
|
+
await p1.then(result => {
|
|
126
|
+
ase(result, 2 + 3 + 6 + 9)
|
|
107
127
|
})
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
assert.deepEqual(
|
|
111
|
-
transform({ a: 1, b: 2, c: 3 }, Transducer.passthrough, []),
|
|
112
|
-
[1, 2, 3],
|
|
113
|
-
)
|
|
128
|
+
await p2.then(result => {
|
|
129
|
+
ade(result, [2, 3, 6, 9])
|
|
114
130
|
})
|
|
115
131
|
})
|
|
116
132
|
|
|
133
|
+
it('forEach transducer', async () => {
|
|
134
|
+
let sum1 = 0
|
|
135
|
+
reduce([1, 2, 3, 4, 5], Transducer.forEach(number => {
|
|
136
|
+
sum1 += number
|
|
137
|
+
})(() => {}), null)
|
|
138
|
+
assert.equal(sum1, 15)
|
|
139
|
+
|
|
140
|
+
let sum2 = 0
|
|
141
|
+
await reduce([1, 2, 3, 4, 5], Transducer.forEach(async number => {
|
|
142
|
+
sum2 += number
|
|
143
|
+
})(() => {}), null)
|
|
144
|
+
assert.equal(sum2, 15)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('passthrough transducer', async () => {
|
|
148
|
+
assert.deepEqual(
|
|
149
|
+
transform({ a: 1, b: 2, c: 3 }, Transducer.passthrough, []),
|
|
150
|
+
[1, 2, 3],
|
|
151
|
+
)
|
|
152
|
+
})
|
|
153
|
+
|
|
117
154
|
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const isPromise = require('./isPromise')
|
|
2
|
+
const __ = require('./placeholder')
|
|
3
|
+
const curry5 = require('./curry5')
|
|
4
|
+
const curry2 = require('./curry2')
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @name _reducerTryCatchErrorHandler
|
|
8
|
+
*
|
|
9
|
+
* @synopsis
|
|
10
|
+
* ```coffeescript [specscript]
|
|
11
|
+
* _reducerTryCatchErrorHandler(
|
|
12
|
+
* reducer function,
|
|
13
|
+
* error Error,
|
|
14
|
+
* accum any,
|
|
15
|
+
* item any,
|
|
16
|
+
* ) -> Promise|any
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
const _reducerTryCatchErrorHandler = function (
|
|
20
|
+
catcher, reducer, error, accum, item,
|
|
21
|
+
) {
|
|
22
|
+
const c = catcher(error, item)
|
|
23
|
+
return isPromise(c) ? c.then(curry2(reducer, accum, __)) : reducer(accum, c)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @name reducerTryCatch
|
|
28
|
+
*
|
|
29
|
+
* @synopsis
|
|
30
|
+
* ```coffeescript [specscript]
|
|
31
|
+
* type Reducer = (accum any, item any)=>(nextAccumulator Promise|any)
|
|
32
|
+
*
|
|
33
|
+
* reducerTryCatch(
|
|
34
|
+
* reducer function,
|
|
35
|
+
* catcher function,
|
|
36
|
+
* ) -> errorHandlingReducer function
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
const reducerTryCatch = function (reducer, transducerTryer, catcher) {
|
|
40
|
+
const finalReducer = transducerTryer(reducer)
|
|
41
|
+
return function errorHandlingReducer(accum, item) {
|
|
42
|
+
try {
|
|
43
|
+
const ret = finalReducer(accum, item)
|
|
44
|
+
return isPromise(ret) ? ret.catch(curry5(
|
|
45
|
+
_reducerTryCatchErrorHandler, catcher, reducer, __, accum, item,
|
|
46
|
+
)) : ret
|
|
47
|
+
} catch (error) {
|
|
48
|
+
return _reducerTryCatchErrorHandler(
|
|
49
|
+
catcher, reducer, error, accum, item,
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = reducerTryCatch
|