vega 0.4.0 → 0.6.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,304 @@
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, DisallowedObjectProperties } 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 = (yearOrTimestring, m = 0, d = 1, H = 0, M = 0, S = 0, ms = 0) => isString(yearOrTimestring) ? new Date(yearOrTimestring) : new Date(yearOrTimestring, m, d, H, M, S, ms);
86
+ var Functions = {
87
+ // math functions
88
+ isNaN: Number.isNaN,
89
+ isFinite: Number.isFinite,
90
+ abs: Math.abs,
91
+ acos: Math.acos,
92
+ asin: Math.asin,
93
+ atan: Math.atan,
94
+ atan2: Math.atan2,
95
+ ceil: Math.ceil,
96
+ cos: Math.cos,
97
+ exp: Math.exp,
98
+ floor: Math.floor,
99
+ log: Math.log,
100
+ max: Math.max,
101
+ min: Math.min,
102
+ pow: Math.pow,
103
+ random: Math.random,
104
+ round: Math.round,
105
+ sin: Math.sin,
106
+ sqrt: Math.sqrt,
107
+ tan: Math.tan,
108
+ clamp: (a, b, c) => Math.max(b, Math.min(c, a)),
109
+ // date functions
110
+ now: Date.now,
111
+ utc: Date.UTC,
112
+ datetime: datetime,
113
+ date: d => new Date(d).getDate(),
114
+ day: d => new Date(d).getDay(),
115
+ year: d => new Date(d).getFullYear(),
116
+ month: d => new Date(d).getMonth(),
117
+ hours: d => new Date(d).getHours(),
118
+ minutes: d => new Date(d).getMinutes(),
119
+ seconds: d => new Date(d).getSeconds(),
120
+ milliseconds: d => new Date(d).getMilliseconds(),
121
+ time: d => new Date(d).getTime(),
122
+ timezoneoffset: d => new Date(d).getTimezoneOffset(),
123
+ utcdate: d => new Date(d).getUTCDate(),
124
+ utcday: d => new Date(d).getUTCDay(),
125
+ utcyear: d => new Date(d).getUTCFullYear(),
126
+ utcmonth: d => new Date(d).getUTCMonth(),
127
+ utchours: d => new Date(d).getUTCHours(),
128
+ utcminutes: d => new Date(d).getUTCMinutes(),
129
+ utcseconds: d => new Date(d).getUTCSeconds(),
130
+ utcmilliseconds: d => new Date(d).getUTCMilliseconds(),
131
+ // sequence functions
132
+ length: x => x.length,
133
+ join: function () {
134
+ return apply('join', arguments);
135
+ },
136
+ indexof: function () {
137
+ return apply('indexOf', arguments);
138
+ },
139
+ lastindexof: function () {
140
+ return apply('lastIndexOf', arguments);
141
+ },
142
+ slice: function () {
143
+ return apply('slice', arguments);
144
+ },
145
+ reverse: x => x.slice().reverse(),
146
+ sort: x => x.slice().sort(ascending),
147
+ // string functions
148
+ parseFloat: parseFloat,
149
+ parseInt: parseInt,
150
+ upper: x => String(x).toUpperCase(),
151
+ lower: x => String(x).toLowerCase(),
152
+ substring: function () {
153
+ return apply('substring', arguments, String);
154
+ },
155
+ split: function () {
156
+ return apply('split', arguments, String);
157
+ },
158
+ replace: function () {
159
+ return apply('replace', arguments, String);
160
+ },
161
+ trim: x => String(x).trim(),
162
+ // Base64 encode/decode
163
+ // Convert binary string to base64-encoded ascii
164
+ btoa: x => btoa(x),
165
+ // Convert base64-encoded ascii to binary string
166
+ atob: x => atob(x),
167
+ // regexp functions
168
+ regexp: RegExp,
169
+ test: (r, t) => RegExp(r).test(t)
170
+ };
169
171
 
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;
172
+ const EventFunctions = ['view', 'item', 'group', 'xy', 'x', 'y'];
173
+ const DisallowedMethods = new Set([Function, eval, setTimeout, setInterval]);
174
+ if (typeof setImmediate === 'function') DisallowedMethods.add(setImmediate);
175
+ const Visitors = {
176
+ Literal: ($, n) => n.value,
177
+ Identifier: ($, n) => {
178
+ const id = n.name;
179
+ return $.memberDepth > 0 ? id : id === 'datum' ? $.datum : id === 'event' ? $.event : id === 'item' ? $.item : Constants[id] || $.params['$' + id];
180
+ },
181
+ MemberExpression: ($, n) => {
182
+ const d = !n.computed,
183
+ o = $(n.object);
184
+ if (d) $.memberDepth += 1;
185
+ const p = $(n.property);
186
+ if (d) $.memberDepth -= 1;
187
+ if (DisallowedMethods.has(o[p])) {
188
+ // eslint-disable-next-line no-console
189
+ console.error(`Prevented interpretation of member "${p}" which could lead to insecure code execution`);
190
+ return;
191
+ }
192
+ return o[p];
193
+ },
194
+ CallExpression: ($, n) => {
195
+ const args = n.arguments;
196
+ let name = n.callee.name;
195
197
 
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
- }
198
+ // handle special internal functions used by encoders
199
+ // re-route to corresponding standard function
200
+ if (name.startsWith('_')) {
201
+ name = name.slice(1);
202
+ }
201
203
 
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;
204
+ // special case "if" due to conditional evaluation of branches
205
+ return name === 'if' ? $(args[0]) ? $(args[1]) : $(args[2]) : ($.fn[name] || Functions[name]).apply($.fn, args.map($));
206
+ },
207
+ ArrayExpression: ($, n) => n.elements.map($),
208
+ BinaryExpression: ($, n) => Ops[n.operator]($(n.left), $(n.right)),
209
+ UnaryExpression: ($, n) => Unary[n.operator]($(n.argument)),
210
+ ConditionalExpression: ($, n) => $(n.test) ? $(n.consequent) : $(n.alternate),
211
+ LogicalExpression: ($, n) => n.operator === '&&' ? $(n.left) && $(n.right) : $(n.left) || $(n.right),
212
+ ObjectExpression: ($, n) => n.properties.reduce((o, p) => {
213
+ $.memberDepth += 1;
214
+ const k = $(p.key);
215
+ $.memberDepth -= 1;
216
+ if (DisallowedObjectProperties.has($(p.value))) {
217
+ // eslint-disable-next-line no-console
218
+ console.error(`Prevented interpretation of property "${k}" which could lead to insecure code execution`);
219
+ } else {
220
+ o[k] = $(p.value);
221
+ }
222
+ return o;
223
+ }, {})
224
+ };
225
+ function interpret (ast, fn, params, datum, event, item) {
226
+ const $ = n => Visitors[n.type]($, n);
227
+ $.memberDepth = 0;
228
+ $.fn = Object.create(fn);
229
+ $.params = params;
230
+ $.datum = datum;
231
+ $.event = event;
232
+ $.item = item;
231
233
 
232
- // route event functions to annotated vega event context
233
- EventFunctions.forEach(f => $.fn[f] = (...args) => event.vega[f](...args));
234
- return $(ast);
235
- }
234
+ // route event functions to annotated vega event context
235
+ EventFunctions.forEach(f => $.fn[f] = (...args) => event.vega[f](...args));
236
+ return $(ast);
237
+ }
236
238
 
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);
239
+ var expression = {
240
+ /**
241
+ * Parse an expression used to update an operator value.
242
+ */
243
+ operator(ctx, expr) {
244
+ const ast = expr.ast,
245
+ fn = ctx.functions;
246
+ return _ => interpret(ast, fn, _);
247
+ },
248
+ /**
249
+ * Parse an expression provided as an operator parameter value.
250
+ */
251
+ parameter(ctx, expr) {
252
+ const ast = expr.ast,
253
+ fn = ctx.functions;
254
+ return (datum, _) => interpret(ast, fn, _, datum);
255
+ },
256
+ /**
257
+ * Parse an expression applied to an event stream.
258
+ */
259
+ event(ctx, expr) {
260
+ const ast = expr.ast,
261
+ fn = ctx.functions;
262
+ return event => interpret(ast, fn, undefined, undefined, event);
263
+ },
264
+ /**
265
+ * Parse an expression used to handle an event-driven operator update.
266
+ */
267
+ handler(ctx, expr) {
268
+ const ast = expr.ast,
269
+ fn = ctx.functions;
270
+ return (_, event) => {
271
+ const datum = event.item && event.item.datum;
272
+ return interpret(ast, fn, _, datum, event);
273
+ };
274
+ },
275
+ /**
276
+ * Parse an expression that performs visual encoding.
277
+ */
278
+ encode(ctx, encode) {
279
+ const {
280
+ marktype,
281
+ channels
282
+ } = encode,
283
+ fn = ctx.functions,
284
+ swap = marktype === 'group' || marktype === 'image' || marktype === 'rect';
285
+ return (item, _) => {
286
+ const datum = item.datum;
287
+ let m = 0,
288
+ v;
289
+ for (const name in channels) {
290
+ v = interpret(channels[name].ast, fn, _, datum, undefined, item);
291
+ if (item[name] !== v) {
292
+ item[name] = v;
293
+ m = 1;
296
294
  }
297
- return m;
298
- };
299
- }
300
- };
301
-
302
- exports.expressionInterpreter = expression;
295
+ }
296
+ if (marktype !== 'rule') {
297
+ adjustSpatial(item, channels, swap);
298
+ }
299
+ return m;
300
+ };
301
+ }
302
+ };
303
303
 
304
- }));
304
+ export { expression as expressionInterpreter };