UNPKG

898 BJavaScriptView Raw
1import {Operator} from 'vega-dataflow';
2import {accessor, accessorFields, accessorName, inherits} from 'vega-util';
3
4/**
5 * Wraps an expression function with access to external parameters.
6 * @constructor
7 * @param {object} params - The parameters for this operator.
8 * @param {function} params.expr - The expression function. The
9 * function should accept both a datum and a parameter object.
10 * This operator's value will be a new function that wraps the
11 * expression function with access to this operator's parameters.
12 */
13export default function Expression(params) {
14 Operator.call(this, null, update, params);
15 this.modified(true);
16}
17
18inherits(Expression, Operator);
19
20function update(_) {
21 var expr = _.expr;
22 return this.value && !_.modified('expr')
23 ? this.value
24 : accessor(
25 datum => expr(datum, _),
26 accessorFields(expr),
27 accessorName(expr)
28 );
29}