1 | import {fieldNames} from './util/util';
|
2 | import {Transform, ingest, rederive, tupleid} from 'vega-dataflow';
|
3 | import {inherits} from 'vega-util';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export default function Project(params) {
|
18 | Transform.call(this, null, params);
|
19 | }
|
20 |
|
21 | Project.Definition = {
|
22 | 'type': 'Project',
|
23 | 'metadata': {'generates': true, 'changes': true},
|
24 | 'params': [
|
25 | { 'name': 'fields', 'type': 'field', 'array': true },
|
26 | { 'name': 'as', 'type': 'string', 'null': true, 'array': true }
|
27 | ]
|
28 | };
|
29 |
|
30 | var prototype = inherits(Project, Transform);
|
31 |
|
32 | prototype.transform = function(_, pulse) {
|
33 | var fields = _.fields,
|
34 | as = fieldNames(_.fields, _.as || []),
|
35 | derive = fields
|
36 | ? function(s, t) { return project(s, t, fields, as); }
|
37 | : rederive,
|
38 | out, lut;
|
39 |
|
40 | if (this.value) {
|
41 | lut = this.value;
|
42 | } else {
|
43 | pulse = pulse.addAll();
|
44 | lut = this.value = {};
|
45 | }
|
46 |
|
47 | out = pulse.fork(pulse.NO_SOURCE);
|
48 |
|
49 | pulse.visit(pulse.REM, function(t) {
|
50 | var id = tupleid(t);
|
51 | out.rem.push(lut[id]);
|
52 | lut[id] = null;
|
53 | });
|
54 |
|
55 | pulse.visit(pulse.ADD, function(t) {
|
56 | var dt = derive(t, ingest({}));
|
57 | lut[tupleid(t)] = dt;
|
58 | out.add.push(dt);
|
59 | });
|
60 |
|
61 | pulse.visit(pulse.MOD, function(t) {
|
62 | out.mod.push(derive(t, lut[tupleid(t)]));
|
63 | });
|
64 |
|
65 | return out;
|
66 | };
|
67 |
|
68 | function project(s, t, fields, as) {
|
69 | for (var i=0, n=fields.length; i<n; ++i) {
|
70 | t[as[i]] = fields[i](s);
|
71 | }
|
72 | return t;
|
73 | }
|