This repository has been archived by the owner on Dec 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Fold.js
51 lines (44 loc) · 1.45 KB
/
Fold.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
import {derive, Transform} from 'vega-dataflow';
import {inherits, accessorName} from 'vega-util';
/**
* Folds one more tuple fields into multiple tuples in which the field
* name and values are available under new 'key' and 'value' fields.
* @constructor
* @param {object} params - The parameters for this operator.
* @param {function(object): *} params.fields - An array of field accessors
* for the tuple fields that should be folded.
* @param {Array<string>} [params.as] - Output field names for folded key
* and value fields, defaults to ['key', 'value'].
*/
export default function Fold(params) {
Transform.call(this, [], params);
}
Fold.Definition = {
"type": "Fold",
"metadata": {"generates": true},
"params": [
{ "name": "fields", "type": "field", "array": true, "required": true },
{ "name": "as", "type": "string", "array": true, "length": 2, "default": ["key", "value"] }
]
};
var prototype = inherits(Fold, Transform);
prototype.transform = function(_, pulse) {
var out = pulse.fork(pulse.NO_SOURCE),
fields = _.fields,
fnames = fields.map(accessorName),
as = _.as || ['key', 'value'],
k = as[0],
v = as[1],
n = fields.length;
out.rem = this.value;
pulse.visit(pulse.SOURCE, function(t) {
for (var i=0, d; i<n; ++i) {
d = derive(t);
d[k] = fnames[i];
d[v] = fields[i](t);
out.add.push(d);
}
});
this.value = out.source = out.add;
return out.modifies(as);
};