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 | inherits(Flatten, Transform, {
|
34 | transform(_, pulse) {
|
35 | const out = pulse.fork(pulse.NO_SOURCE),
|
36 | fields = _.fields,
|
37 | as = fieldNames(fields, _.as || []),
|
38 | index = _.index || null,
|
39 | m = as.length;
|
40 |
|
41 |
|
42 | out.rem = this.value;
|
43 |
|
44 |
|
45 | pulse.visit(pulse.SOURCE, t => {
|
46 | const arrays = fields.map(f => f(t)),
|
47 | maxlen = arrays.reduce((l, a) => Math.max(l, a.length), 0);
|
48 | let i = 0, j, d, v;
|
49 |
|
50 | for (; i<maxlen; ++i) {
|
51 | d = derive(t);
|
52 | for (j=0; j<m; ++j) {
|
53 | d[as[j]] = (v = arrays[j][i]) == null ? null : v;
|
54 | }
|
55 | if (index) {
|
56 | d[index] = i;
|
57 | }
|
58 | out.add.push(d);
|
59 | }
|
60 | });
|
61 |
|
62 | this.value = out.source = out.add;
|
63 | if (index) out.modifies(index);
|
64 | return out.modifies(as);
|
65 | }
|
66 | });
|