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
18var prototype = inherits(Values, Transform);
19
20prototype.transform = function(_, pulse) {
21 var run = !this.value
22 || _.modified('field')
23 || _.modified('sort')
24 || pulse.changed()
25 || (_.sort && pulse.modified(_.sort.fields));
26
27 if (run) {
28 this.value = (_.sort
29 ? pulse.source.slice().sort(stableCompare(_.sort))
30 : pulse.source).map(_.field);
31 }
32};