1 | import {fieldNames} from './util/util';
|
2 | import {Transform, derive} from 'vega-dataflow';
|
3 | import {inherits} from 'vega-util';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export default function Flatten(params) {
|
20 | Transform.call(this, [], params);
|
21 | }
|
22 |
|
23 | Flatten.Definition = {
|
24 | 'type': 'Flatten',
|
25 | 'metadata': {'generates': true},
|
26 | 'params': [
|
27 | { 'name': 'fields', 'type': 'field', 'array': true, 'required': true },
|
28 | { 'name': 'index', 'type': 'string' },
|
29 | { 'name': 'as', 'type': 'string', 'array': true }
|
30 | ]
|
31 | };
|
32 |
|
33 | var prototype = inherits(Flatten, Transform);
|
34 |
|
35 | prototype.transform = function(_, pulse) {
|
36 | var out = pulse.fork(pulse.NO_SOURCE),
|
37 | fields = _.fields,
|
38 | as = fieldNames(fields, _.as || []),
|
39 | index = _.index || null,
|
40 | m = as.length;
|
41 |
|
42 |
|
43 | out.rem = this.value;
|
44 |
|
45 |
|
46 | pulse.visit(pulse.SOURCE, function(t) {
|
47 | var arrays = fields.map(f => f(t)),
|
48 | maxlen = arrays.reduce((l, a) => Math.max(l, a.length), 0),
|
49 | i = 0, j, d, v;
|
50 |
|
51 | for (; i<maxlen; ++i) {
|
52 | d = derive(t);
|
53 | for (j=0; j<m; ++j) {
|
54 | d[as[j]] = (v = arrays[j][i]) == null ? null : v;
|
55 | }
|
56 | if (index) {
|
57 | d[index] = i;
|
58 | }
|
59 | out.add.push(d);
|
60 | }
|
61 | });
|
62 |
|
63 | this.value = out.source = out.add;
|
64 | if (index) out.modifies(index);
|
65 | return out.modifies(as);
|
66 | };
|