UNPKG

1.04 kBJavaScriptView Raw
1import {Transform, stableCompare} from 'vega-dataflow';
2import {inherits} from 'vega-util';
3
4/**
5 * Extracts an array of values. Assumes the source data has already been
6 * reduced as needed (e.g., by an upstream Aggregate transform).
7 * @constructor
8 * @param {object} params - The parameters for this operator.
9 * @param {function(object): *} params.field - The domain field to extract.
10 * @param {function(*,*): number} [params.sort] - An optional
11 * comparator function for sorting the values. The comparator will be
12 * applied to backing tuples prior to value extraction.
13 */
14export default function Values(params) {
15 Transform.call(this, null, params);
16}
17
18inherits(Values, Transform, {
19 transform(_, pulse) {
20 const run = !this.value
21 || _.modified('field')
22 || _.modified('sort')
23 || pulse.changed()
24 || (_.sort && pulse.modified(_.sort.fields));
25
26 if (run) {
27 this.value = (_.sort
28 ? pulse.source.slice().sort(stableCompare(_.sort))
29 : pulse.source).map(_.field);
30 }
31 }
32});
\No newline at end of file