vega 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,304 +1,314 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.vega = global.vega || {}));
5
- })(this, (function (exports) { 'use strict';
1
+ import { ascending, isString } from 'vega-util';
6
2
 
7
- function adjustSpatial (item, encode, swap) {
8
- let t;
9
- if (encode.x2) {
10
- if (encode.x) {
11
- if (swap && item.x > item.x2) {
12
- t = item.x;
13
- item.x = item.x2;
14
- item.x2 = t;
15
- }
16
- item.width = item.x2 - item.x;
17
- } else {
18
- item.x = item.x2 - (item.width || 0);
3
+ function adjustSpatial (item, encode, swap) {
4
+ let t;
5
+ if (encode.x2) {
6
+ if (encode.x) {
7
+ if (swap && item.x > item.x2) {
8
+ t = item.x;
9
+ item.x = item.x2;
10
+ item.x2 = t;
19
11
  }
12
+ item.width = item.x2 - item.x;
13
+ } else {
14
+ item.x = item.x2 - (item.width || 0);
20
15
  }
21
- if (encode.xc) {
22
- item.x = item.xc - (item.width || 0) / 2;
23
- }
24
- if (encode.y2) {
25
- if (encode.y) {
26
- if (swap && item.y > item.y2) {
27
- t = item.y;
28
- item.y = item.y2;
29
- item.y2 = t;
30
- }
31
- item.height = item.y2 - item.y;
32
- } else {
33
- item.y = item.y2 - (item.height || 0);
16
+ }
17
+ if (encode.xc) {
18
+ item.x = item.xc - (item.width || 0) / 2;
19
+ }
20
+ if (encode.y2) {
21
+ if (encode.y) {
22
+ if (swap && item.y > item.y2) {
23
+ t = item.y;
24
+ item.y = item.y2;
25
+ item.y2 = t;
34
26
  }
27
+ item.height = item.y2 - item.y;
28
+ } else {
29
+ item.y = item.y2 - (item.height || 0);
35
30
  }
36
- if (encode.yc) {
37
- item.y = item.yc - (item.height || 0) / 2;
38
- }
39
31
  }
32
+ if (encode.yc) {
33
+ item.y = item.yc - (item.height || 0) / 2;
34
+ }
35
+ }
40
36
 
41
- var Constants = {
42
- NaN: NaN,
43
- E: Math.E,
44
- LN2: Math.LN2,
45
- LN10: Math.LN10,
46
- LOG2E: Math.LOG2E,
47
- LOG10E: Math.LOG10E,
48
- PI: Math.PI,
49
- SQRT1_2: Math.SQRT1_2,
50
- SQRT2: Math.SQRT2,
51
- MIN_VALUE: Number.MIN_VALUE,
52
- MAX_VALUE: Number.MAX_VALUE
53
- };
37
+ var Constants = {
38
+ NaN: NaN,
39
+ E: Math.E,
40
+ LN2: Math.LN2,
41
+ LN10: Math.LN10,
42
+ LOG2E: Math.LOG2E,
43
+ LOG10E: Math.LOG10E,
44
+ PI: Math.PI,
45
+ SQRT1_2: Math.SQRT1_2,
46
+ SQRT2: Math.SQRT2,
47
+ MIN_VALUE: Number.MIN_VALUE,
48
+ MAX_VALUE: Number.MAX_VALUE
49
+ };
54
50
 
55
- var Ops = {
56
- '*': (a, b) => a * b,
57
- '+': (a, b) => a + b,
58
- '-': (a, b) => a - b,
59
- '/': (a, b) => a / b,
60
- '%': (a, b) => a % b,
61
- '>': (a, b) => a > b,
62
- '<': (a, b) => a < b,
63
- '<=': (a, b) => a <= b,
64
- '>=': (a, b) => a >= b,
65
- '==': (a, b) => a == b,
66
- '!=': (a, b) => a != b,
67
- '===': (a, b) => a === b,
68
- '!==': (a, b) => a !== b,
69
- '&': (a, b) => a & b,
70
- '|': (a, b) => a | b,
71
- '^': (a, b) => a ^ b,
72
- '<<': (a, b) => a << b,
73
- '>>': (a, b) => a >> b,
74
- '>>>': (a, b) => a >>> b
75
- };
51
+ var Ops = {
52
+ '*': (a, b) => a * b,
53
+ '+': (a, b) => a + b,
54
+ '-': (a, b) => a - b,
55
+ '/': (a, b) => a / b,
56
+ '%': (a, b) => a % b,
57
+ '>': (a, b) => a > b,
58
+ '<': (a, b) => a < b,
59
+ '<=': (a, b) => a <= b,
60
+ '>=': (a, b) => a >= b,
61
+ '==': (a, b) => a == b,
62
+ '!=': (a, b) => a != b,
63
+ '===': (a, b) => a === b,
64
+ '!==': (a, b) => a !== b,
65
+ '&': (a, b) => a & b,
66
+ '|': (a, b) => a | b,
67
+ '^': (a, b) => a ^ b,
68
+ '<<': (a, b) => a << b,
69
+ '>>': (a, b) => a >> b,
70
+ '>>>': (a, b) => a >>> b
71
+ };
76
72
 
77
- var Unary = {
78
- '+': a => +a,
79
- '-': a => -a,
80
- '~': a => ~a,
81
- '!': a => !a
82
- };
73
+ var Unary = {
74
+ '+': a => +a,
75
+ '-': a => -a,
76
+ '~': a => ~a,
77
+ '!': a => !a
78
+ };
83
79
 
84
- const slice = Array.prototype.slice;
85
- const apply = (m, args, cast) => {
86
- const obj = cast ? cast(args[0]) : args[0];
87
- return obj[m].apply(obj, slice.call(args, 1));
88
- };
89
- const datetime = (y, m, d, H, M, S, ms) => new Date(y, m || 0, d != null ? d : 1, H || 0, M || 0, S || 0, ms || 0);
90
- var Functions = {
91
- // math functions
92
- isNaN: Number.isNaN,
93
- isFinite: Number.isFinite,
94
- abs: Math.abs,
95
- acos: Math.acos,
96
- asin: Math.asin,
97
- atan: Math.atan,
98
- atan2: Math.atan2,
99
- ceil: Math.ceil,
100
- cos: Math.cos,
101
- exp: Math.exp,
102
- floor: Math.floor,
103
- log: Math.log,
104
- max: Math.max,
105
- min: Math.min,
106
- pow: Math.pow,
107
- random: Math.random,
108
- round: Math.round,
109
- sin: Math.sin,
110
- sqrt: Math.sqrt,
111
- tan: Math.tan,
112
- clamp: (a, b, c) => Math.max(b, Math.min(c, a)),
113
- // date functions
114
- now: Date.now,
115
- utc: Date.UTC,
116
- datetime: datetime,
117
- date: d => new Date(d).getDate(),
118
- day: d => new Date(d).getDay(),
119
- year: d => new Date(d).getFullYear(),
120
- month: d => new Date(d).getMonth(),
121
- hours: d => new Date(d).getHours(),
122
- minutes: d => new Date(d).getMinutes(),
123
- seconds: d => new Date(d).getSeconds(),
124
- milliseconds: d => new Date(d).getMilliseconds(),
125
- time: d => new Date(d).getTime(),
126
- timezoneoffset: d => new Date(d).getTimezoneOffset(),
127
- utcdate: d => new Date(d).getUTCDate(),
128
- utcday: d => new Date(d).getUTCDay(),
129
- utcyear: d => new Date(d).getUTCFullYear(),
130
- utcmonth: d => new Date(d).getUTCMonth(),
131
- utchours: d => new Date(d).getUTCHours(),
132
- utcminutes: d => new Date(d).getUTCMinutes(),
133
- utcseconds: d => new Date(d).getUTCSeconds(),
134
- utcmilliseconds: d => new Date(d).getUTCMilliseconds(),
135
- // sequence functions
136
- length: x => x.length,
137
- join: function () {
138
- return apply('join', arguments);
139
- },
140
- indexof: function () {
141
- return apply('indexOf', arguments);
142
- },
143
- lastindexof: function () {
144
- return apply('lastIndexOf', arguments);
145
- },
146
- slice: function () {
147
- return apply('slice', arguments);
148
- },
149
- reverse: x => x.slice().reverse(),
150
- // string functions
151
- parseFloat: parseFloat,
152
- parseInt: parseInt,
153
- upper: x => String(x).toUpperCase(),
154
- lower: x => String(x).toLowerCase(),
155
- substring: function () {
156
- return apply('substring', arguments, String);
157
- },
158
- split: function () {
159
- return apply('split', arguments, String);
160
- },
161
- replace: function () {
162
- return apply('replace', arguments, String);
163
- },
164
- trim: x => String(x).trim(),
165
- // regexp functions
166
- regexp: RegExp,
167
- test: (r, t) => RegExp(r).test(t)
168
- };
80
+ const slice = Array.prototype.slice;
81
+ const apply = (m, args, cast) => {
82
+ const obj = cast ? cast(args[0]) : args[0];
83
+ return obj[m].apply(obj, slice.call(args, 1));
84
+ };
85
+ const datetime = function (yearOrTimestring) {
86
+ let m = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
87
+ let d = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
88
+ let H = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
89
+ let M = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
90
+ let S = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0;
91
+ let ms = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 0;
92
+ return isString(yearOrTimestring) ? new Date(yearOrTimestring) : new Date(yearOrTimestring, m, d, H, M, S, ms);
93
+ };
94
+ var Functions = {
95
+ // math functions
96
+ isNaN: Number.isNaN,
97
+ isFinite: Number.isFinite,
98
+ abs: Math.abs,
99
+ acos: Math.acos,
100
+ asin: Math.asin,
101
+ atan: Math.atan,
102
+ atan2: Math.atan2,
103
+ ceil: Math.ceil,
104
+ cos: Math.cos,
105
+ exp: Math.exp,
106
+ floor: Math.floor,
107
+ log: Math.log,
108
+ max: Math.max,
109
+ min: Math.min,
110
+ pow: Math.pow,
111
+ random: Math.random,
112
+ round: Math.round,
113
+ sin: Math.sin,
114
+ sqrt: Math.sqrt,
115
+ tan: Math.tan,
116
+ clamp: (a, b, c) => Math.max(b, Math.min(c, a)),
117
+ // date functions
118
+ now: Date.now,
119
+ utc: Date.UTC,
120
+ datetime: datetime,
121
+ date: d => new Date(d).getDate(),
122
+ day: d => new Date(d).getDay(),
123
+ year: d => new Date(d).getFullYear(),
124
+ month: d => new Date(d).getMonth(),
125
+ hours: d => new Date(d).getHours(),
126
+ minutes: d => new Date(d).getMinutes(),
127
+ seconds: d => new Date(d).getSeconds(),
128
+ milliseconds: d => new Date(d).getMilliseconds(),
129
+ time: d => new Date(d).getTime(),
130
+ timezoneoffset: d => new Date(d).getTimezoneOffset(),
131
+ utcdate: d => new Date(d).getUTCDate(),
132
+ utcday: d => new Date(d).getUTCDay(),
133
+ utcyear: d => new Date(d).getUTCFullYear(),
134
+ utcmonth: d => new Date(d).getUTCMonth(),
135
+ utchours: d => new Date(d).getUTCHours(),
136
+ utcminutes: d => new Date(d).getUTCMinutes(),
137
+ utcseconds: d => new Date(d).getUTCSeconds(),
138
+ utcmilliseconds: d => new Date(d).getUTCMilliseconds(),
139
+ // sequence functions
140
+ length: x => x.length,
141
+ join: function () {
142
+ return apply('join', arguments);
143
+ },
144
+ indexof: function () {
145
+ return apply('indexOf', arguments);
146
+ },
147
+ lastindexof: function () {
148
+ return apply('lastIndexOf', arguments);
149
+ },
150
+ slice: function () {
151
+ return apply('slice', arguments);
152
+ },
153
+ reverse: x => x.slice().reverse(),
154
+ sort: x => x.slice().sort(ascending),
155
+ // string functions
156
+ parseFloat: parseFloat,
157
+ parseInt: parseInt,
158
+ upper: x => String(x).toUpperCase(),
159
+ lower: x => String(x).toLowerCase(),
160
+ substring: function () {
161
+ return apply('substring', arguments, String);
162
+ },
163
+ split: function () {
164
+ return apply('split', arguments, String);
165
+ },
166
+ replace: function () {
167
+ return apply('replace', arguments, String);
168
+ },
169
+ trim: x => String(x).trim(),
170
+ // Base64 encode/decode
171
+ // Convert binary string to base64-encoded ascii
172
+ btoa: x => btoa(x),
173
+ // Convert base64-encoded ascii to binary string
174
+ atob: x => atob(x),
175
+ // regexp functions
176
+ regexp: RegExp,
177
+ test: (r, t) => RegExp(r).test(t)
178
+ };
169
179
 
170
- const EventFunctions = ['view', 'item', 'group', 'xy', 'x', 'y'];
171
- const DisallowedMethods = new Set([Function, eval, setTimeout, setInterval]);
172
- if (typeof setImmediate === 'function') DisallowedMethods.add(setImmediate);
173
- const Visitors = {
174
- Literal: ($, n) => n.value,
175
- Identifier: ($, n) => {
176
- const id = n.name;
177
- return $.memberDepth > 0 ? id : id === 'datum' ? $.datum : id === 'event' ? $.event : id === 'item' ? $.item : Constants[id] || $.params['$' + id];
178
- },
179
- MemberExpression: ($, n) => {
180
- const d = !n.computed,
181
- o = $(n.object);
182
- if (d) $.memberDepth += 1;
183
- const p = $(n.property);
184
- if (d) $.memberDepth -= 1;
185
- if (DisallowedMethods.has(o[p])) {
186
- // eslint-disable-next-line no-console
187
- console.error(`Prevented interpretation of member "${p}" which could lead to insecure code execution`);
188
- return;
189
- }
190
- return o[p];
191
- },
192
- CallExpression: ($, n) => {
193
- const args = n.arguments;
194
- let name = n.callee.name;
180
+ const EventFunctions = ['view', 'item', 'group', 'xy', 'x', 'y'];
181
+ const DisallowedMethods = new Set([Function, eval, setTimeout, setInterval]);
182
+ if (typeof setImmediate === 'function') DisallowedMethods.add(setImmediate);
183
+ const Visitors = {
184
+ Literal: ($, n) => n.value,
185
+ Identifier: ($, n) => {
186
+ const id = n.name;
187
+ return $.memberDepth > 0 ? id : id === 'datum' ? $.datum : id === 'event' ? $.event : id === 'item' ? $.item : Constants[id] || $.params['$' + id];
188
+ },
189
+ MemberExpression: ($, n) => {
190
+ const d = !n.computed,
191
+ o = $(n.object);
192
+ if (d) $.memberDepth += 1;
193
+ const p = $(n.property);
194
+ if (d) $.memberDepth -= 1;
195
+ if (DisallowedMethods.has(o[p])) {
196
+ // eslint-disable-next-line no-console
197
+ console.error(`Prevented interpretation of member "${p}" which could lead to insecure code execution`);
198
+ return;
199
+ }
200
+ return o[p];
201
+ },
202
+ CallExpression: ($, n) => {
203
+ const args = n.arguments;
204
+ let name = n.callee.name;
195
205
 
196
- // handle special internal functions used by encoders
197
- // re-route to corresponding standard function
198
- if (name.startsWith('_')) {
199
- name = name.slice(1);
200
- }
206
+ // handle special internal functions used by encoders
207
+ // re-route to corresponding standard function
208
+ if (name.startsWith('_')) {
209
+ name = name.slice(1);
210
+ }
201
211
 
202
- // special case "if" due to conditional evaluation of branches
203
- return name === 'if' ? $(args[0]) ? $(args[1]) : $(args[2]) : ($.fn[name] || Functions[name]).apply($.fn, args.map($));
204
- },
205
- ArrayExpression: ($, n) => n.elements.map($),
206
- BinaryExpression: ($, n) => Ops[n.operator]($(n.left), $(n.right)),
207
- UnaryExpression: ($, n) => Unary[n.operator]($(n.argument)),
208
- ConditionalExpression: ($, n) => $(n.test) ? $(n.consequent) : $(n.alternate),
209
- LogicalExpression: ($, n) => n.operator === '&&' ? $(n.left) && $(n.right) : $(n.left) || $(n.right),
210
- ObjectExpression: ($, n) => n.properties.reduce((o, p) => {
211
- $.memberDepth += 1;
212
- const k = $(p.key);
213
- $.memberDepth -= 1;
214
- if (DisallowedMethods.has($(p.value))) {
215
- // eslint-disable-next-line no-console
216
- console.error(`Prevented interpretation of property "${k}" which could lead to insecure code execution`);
217
- } else {
218
- o[k] = $(p.value);
219
- }
220
- return o;
221
- }, {})
222
- };
223
- function interpret (ast, fn, params, datum, event, item) {
224
- const $ = n => Visitors[n.type]($, n);
225
- $.memberDepth = 0;
226
- $.fn = Object.create(fn);
227
- $.params = params;
228
- $.datum = datum;
229
- $.event = event;
230
- $.item = item;
212
+ // special case "if" due to conditional evaluation of branches
213
+ return name === 'if' ? $(args[0]) ? $(args[1]) : $(args[2]) : ($.fn[name] || Functions[name]).apply($.fn, args.map($));
214
+ },
215
+ ArrayExpression: ($, n) => n.elements.map($),
216
+ BinaryExpression: ($, n) => Ops[n.operator]($(n.left), $(n.right)),
217
+ UnaryExpression: ($, n) => Unary[n.operator]($(n.argument)),
218
+ ConditionalExpression: ($, n) => $(n.test) ? $(n.consequent) : $(n.alternate),
219
+ LogicalExpression: ($, n) => n.operator === '&&' ? $(n.left) && $(n.right) : $(n.left) || $(n.right),
220
+ ObjectExpression: ($, n) => n.properties.reduce((o, p) => {
221
+ $.memberDepth += 1;
222
+ const k = $(p.key);
223
+ $.memberDepth -= 1;
224
+ if (DisallowedMethods.has($(p.value))) {
225
+ // eslint-disable-next-line no-console
226
+ console.error(`Prevented interpretation of property "${k}" which could lead to insecure code execution`);
227
+ } else {
228
+ o[k] = $(p.value);
229
+ }
230
+ return o;
231
+ }, {})
232
+ };
233
+ function interpret (ast, fn, params, datum, event, item) {
234
+ const $ = n => Visitors[n.type]($, n);
235
+ $.memberDepth = 0;
236
+ $.fn = Object.create(fn);
237
+ $.params = params;
238
+ $.datum = datum;
239
+ $.event = event;
240
+ $.item = item;
231
241
 
232
- // route event functions to annotated vega event context
233
- EventFunctions.forEach(f => $.fn[f] = (...args) => event.vega[f](...args));
234
- return $(ast);
235
- }
242
+ // route event functions to annotated vega event context
243
+ EventFunctions.forEach(f => $.fn[f] = function () {
244
+ return event.vega[f](...arguments);
245
+ });
246
+ return $(ast);
247
+ }
236
248
 
237
- var expression = {
238
- /**
239
- * Parse an expression used to update an operator value.
240
- */
241
- operator(ctx, expr) {
242
- const ast = expr.ast,
243
- fn = ctx.functions;
244
- return _ => interpret(ast, fn, _);
245
- },
246
- /**
247
- * Parse an expression provided as an operator parameter value.
248
- */
249
- parameter(ctx, expr) {
250
- const ast = expr.ast,
251
- fn = ctx.functions;
252
- return (datum, _) => interpret(ast, fn, _, datum);
253
- },
254
- /**
255
- * Parse an expression applied to an event stream.
256
- */
257
- event(ctx, expr) {
258
- const ast = expr.ast,
259
- fn = ctx.functions;
260
- return event => interpret(ast, fn, undefined, undefined, event);
261
- },
262
- /**
263
- * Parse an expression used to handle an event-driven operator update.
264
- */
265
- handler(ctx, expr) {
266
- const ast = expr.ast,
267
- fn = ctx.functions;
268
- return (_, event) => {
269
- const datum = event.item && event.item.datum;
270
- return interpret(ast, fn, _, datum, event);
271
- };
272
- },
273
- /**
274
- * Parse an expression that performs visual encoding.
275
- */
276
- encode(ctx, encode) {
277
- const {
278
- marktype,
279
- channels
280
- } = encode,
281
- fn = ctx.functions,
282
- swap = marktype === 'group' || marktype === 'image' || marktype === 'rect';
283
- return (item, _) => {
284
- const datum = item.datum;
285
- let m = 0,
286
- v;
287
- for (const name in channels) {
288
- v = interpret(channels[name].ast, fn, _, datum, undefined, item);
289
- if (item[name] !== v) {
290
- item[name] = v;
291
- m = 1;
292
- }
293
- }
294
- if (marktype !== 'rule') {
295
- adjustSpatial(item, channels, swap);
249
+ var expression = {
250
+ /**
251
+ * Parse an expression used to update an operator value.
252
+ */
253
+ operator(ctx, expr) {
254
+ const ast = expr.ast,
255
+ fn = ctx.functions;
256
+ return _ => interpret(ast, fn, _);
257
+ },
258
+ /**
259
+ * Parse an expression provided as an operator parameter value.
260
+ */
261
+ parameter(ctx, expr) {
262
+ const ast = expr.ast,
263
+ fn = ctx.functions;
264
+ return (datum, _) => interpret(ast, fn, _, datum);
265
+ },
266
+ /**
267
+ * Parse an expression applied to an event stream.
268
+ */
269
+ event(ctx, expr) {
270
+ const ast = expr.ast,
271
+ fn = ctx.functions;
272
+ return event => interpret(ast, fn, undefined, undefined, event);
273
+ },
274
+ /**
275
+ * Parse an expression used to handle an event-driven operator update.
276
+ */
277
+ handler(ctx, expr) {
278
+ const ast = expr.ast,
279
+ fn = ctx.functions;
280
+ return (_, event) => {
281
+ const datum = event.item && event.item.datum;
282
+ return interpret(ast, fn, _, datum, event);
283
+ };
284
+ },
285
+ /**
286
+ * Parse an expression that performs visual encoding.
287
+ */
288
+ encode(ctx, encode) {
289
+ const {
290
+ marktype,
291
+ channels
292
+ } = encode,
293
+ fn = ctx.functions,
294
+ swap = marktype === 'group' || marktype === 'image' || marktype === 'rect';
295
+ return (item, _) => {
296
+ const datum = item.datum;
297
+ let m = 0,
298
+ v;
299
+ for (const name in channels) {
300
+ v = interpret(channels[name].ast, fn, _, datum, undefined, item);
301
+ if (item[name] !== v) {
302
+ item[name] = v;
303
+ m = 1;
296
304
  }
297
- return m;
298
- };
299
- }
300
- };
301
-
302
- exports.expressionInterpreter = expression;
305
+ }
306
+ if (marktype !== 'rule') {
307
+ adjustSpatial(item, channels, swap);
308
+ }
309
+ return m;
310
+ };
311
+ }
312
+ };
303
313
 
304
- }));
314
+ export { expression as expressionInterpreter };