-
Notifications
You must be signed in to change notification settings - Fork 8
/
reducer.js
51 lines (42 loc) · 1.83 KB
/
reducer.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
"use strict";
var reduce = require("reducible/reduce")
var reducible = require("reducible/reducible")
var isError = require("reducible/is-error")
var end = require("reducible/end")
function reducer(process) {
/**
Convenience function to simplify definitions of transformation function, to
avoid manual definition of `reducible` results and currying transformation
function. It creates typical transformation function with a following
signature:
transform(source, options)
From a pure data `process` function that is called on each value for a
collection with following arguments:
1. `options` - Options passed to the resulting transformation function
most commonly that's a function like in `map(source, f)`.
2. `next` - Function which needs to be invoked with transformed value,
or simply not called to skip the value.
3. `value` - Last value emitted by a collection being reduced.
4. `result` - Accumulate value.
Function is supposed to return new, accumulated `result`. It may either
pass mapped transformed `value` and `result` to the `next` continuation
or skip it.
For example see `map` and `filter` functions.
**/
return function reducer(source, options) {
// When return transformation function is called with a source and
// `options`
return reducible(function reduceReducer(next, initial) {
// When actual result is
reduce(source, function reduceReducerSource(value, result) {
// If value is `end` of source or an error just propagate through,
// otherwise call `process` with all the curried `options` and `next`
// continuation function.
return value === end ? next(value, result) :
isError(value) ? next(value, result) :
process(options, next, value, result)
})
})
}
}
module.exports = reducer