vega 0.1.0 → 0.2.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.
@@ -0,0 +1,311 @@
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';
6
+
7
+ function adjustSpatial (item, encode, swap) {
8
+ let t;
9
+
10
+ if (encode.x2) {
11
+ if (encode.x) {
12
+ if (swap && item.x > item.x2) {
13
+ t = item.x;
14
+ item.x = item.x2;
15
+ item.x2 = t;
16
+ }
17
+
18
+ item.width = item.x2 - item.x;
19
+ } else {
20
+ item.x = item.x2 - (item.width || 0);
21
+ }
22
+ }
23
+
24
+ if (encode.xc) {
25
+ item.x = item.xc - (item.width || 0) / 2;
26
+ }
27
+
28
+ if (encode.y2) {
29
+ if (encode.y) {
30
+ if (swap && item.y > item.y2) {
31
+ t = item.y;
32
+ item.y = item.y2;
33
+ item.y2 = t;
34
+ }
35
+
36
+ item.height = item.y2 - item.y;
37
+ } else {
38
+ item.y = item.y2 - (item.height || 0);
39
+ }
40
+ }
41
+
42
+ if (encode.yc) {
43
+ item.y = item.yc - (item.height || 0) / 2;
44
+ }
45
+ }
46
+
47
+ var Constants = {
48
+ NaN: NaN,
49
+ E: Math.E,
50
+ LN2: Math.LN2,
51
+ LN10: Math.LN10,
52
+ LOG2E: Math.LOG2E,
53
+ LOG10E: Math.LOG10E,
54
+ PI: Math.PI,
55
+ SQRT1_2: Math.SQRT1_2,
56
+ SQRT2: Math.SQRT2,
57
+ MIN_VALUE: Number.MIN_VALUE,
58
+ MAX_VALUE: Number.MAX_VALUE
59
+ };
60
+
61
+ var Ops = {
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
+ '&': (a, b) => a & b,
76
+ '|': (a, b) => a | b,
77
+ '^': (a, b) => a ^ b,
78
+ '<<': (a, b) => a << b,
79
+ '>>': (a, b) => a >> b,
80
+ '>>>': (a, b) => a >>> b
81
+ };
82
+
83
+ var Unary = {
84
+ '+': a => +a,
85
+ '-': a => -a,
86
+ '~': a => ~a,
87
+ '!': a => !a
88
+ };
89
+
90
+ const slice = Array.prototype.slice;
91
+
92
+ const apply = (m, args, cast) => {
93
+ const obj = cast ? cast(args[0]) : args[0];
94
+ return obj[m].apply(obj, slice.call(args, 1));
95
+ };
96
+
97
+ 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);
98
+
99
+ var Functions = {
100
+ // math functions
101
+ isNaN: Number.isNaN,
102
+ isFinite: Number.isFinite,
103
+ abs: Math.abs,
104
+ acos: Math.acos,
105
+ asin: Math.asin,
106
+ atan: Math.atan,
107
+ atan2: Math.atan2,
108
+ ceil: Math.ceil,
109
+ cos: Math.cos,
110
+ exp: Math.exp,
111
+ floor: Math.floor,
112
+ log: Math.log,
113
+ max: Math.max,
114
+ min: Math.min,
115
+ pow: Math.pow,
116
+ random: Math.random,
117
+ round: Math.round,
118
+ sin: Math.sin,
119
+ sqrt: Math.sqrt,
120
+ tan: Math.tan,
121
+ clamp: (a, b, c) => Math.max(b, Math.min(c, a)),
122
+ // date functions
123
+ now: Date.now,
124
+ utc: Date.UTC,
125
+ datetime: datetime,
126
+ date: d => new Date(d).getDate(),
127
+ day: d => new Date(d).getDay(),
128
+ year: d => new Date(d).getFullYear(),
129
+ month: d => new Date(d).getMonth(),
130
+ hours: d => new Date(d).getHours(),
131
+ minutes: d => new Date(d).getMinutes(),
132
+ seconds: d => new Date(d).getSeconds(),
133
+ milliseconds: d => new Date(d).getMilliseconds(),
134
+ time: d => new Date(d).getTime(),
135
+ timezoneoffset: d => new Date(d).getTimezoneOffset(),
136
+ utcdate: d => new Date(d).getUTCDate(),
137
+ utcday: d => new Date(d).getUTCDay(),
138
+ utcyear: d => new Date(d).getUTCFullYear(),
139
+ utcmonth: d => new Date(d).getUTCMonth(),
140
+ utchours: d => new Date(d).getUTCHours(),
141
+ utcminutes: d => new Date(d).getUTCMinutes(),
142
+ utcseconds: d => new Date(d).getUTCSeconds(),
143
+ utcmilliseconds: d => new Date(d).getUTCMilliseconds(),
144
+ // sequence functions
145
+ length: x => x.length,
146
+ join: function () {
147
+ return apply('join', arguments);
148
+ },
149
+ indexof: function () {
150
+ return apply('indexOf', arguments);
151
+ },
152
+ lastindexof: function () {
153
+ return apply('lastIndexOf', arguments);
154
+ },
155
+ slice: function () {
156
+ return apply('slice', arguments);
157
+ },
158
+ reverse: x => x.slice().reverse(),
159
+ // string functions
160
+ parseFloat: parseFloat,
161
+ parseInt: parseInt,
162
+ upper: x => String(x).toUpperCase(),
163
+ lower: x => String(x).toLowerCase(),
164
+ substring: function () {
165
+ return apply('substring', arguments, String);
166
+ },
167
+ split: function () {
168
+ return apply('split', arguments, String);
169
+ },
170
+ replace: function () {
171
+ return apply('replace', arguments, String);
172
+ },
173
+ trim: x => String(x).trim(),
174
+ // regexp functions
175
+ regexp: RegExp,
176
+ test: (r, t) => RegExp(r).test(t)
177
+ };
178
+
179
+ const EventFunctions = ['view', 'item', 'group', 'xy', 'x', 'y'];
180
+ const Visitors = {
181
+ Literal: ($, n) => n.value,
182
+ Identifier: ($, n) => {
183
+ const id = n.name;
184
+ return $.memberDepth > 0 ? id : id === 'datum' ? $.datum : id === 'event' ? $.event : id === 'item' ? $.item : Constants[id] || $.params['$' + id];
185
+ },
186
+ MemberExpression: ($, n) => {
187
+ const d = !n.computed,
188
+ o = $(n.object);
189
+ if (d) $.memberDepth += 1;
190
+ const p = $(n.property);
191
+ if (d) $.memberDepth -= 1;
192
+ return o[p];
193
+ },
194
+ CallExpression: ($, n) => {
195
+ const args = n.arguments;
196
+ let name = n.callee.name; // handle special internal functions used by encoders
197
+ // re-route to corresponding standard function
198
+
199
+ if (name.startsWith('_')) {
200
+ name = name.slice(1);
201
+ } // special case "if" due to conditional evaluation of branches
202
+
203
+
204
+ return name === 'if' ? $(args[0]) ? $(args[1]) : $(args[2]) : ($.fn[name] || Functions[name]).apply($.fn, args.map($));
205
+ },
206
+ ArrayExpression: ($, n) => n.elements.map($),
207
+ BinaryExpression: ($, n) => Ops[n.operator]($(n.left), $(n.right)),
208
+ UnaryExpression: ($, n) => Unary[n.operator]($(n.argument)),
209
+ ConditionalExpression: ($, n) => $(n.test) ? $(n.consequent) : $(n.alternate),
210
+ LogicalExpression: ($, n) => n.operator === '&&' ? $(n.left) && $(n.right) : $(n.left) || $(n.right),
211
+ ObjectExpression: ($, n) => n.properties.reduce((o, p) => {
212
+ $.memberDepth += 1;
213
+ const k = $(p.key);
214
+ $.memberDepth -= 1;
215
+ o[k] = $(p.value);
216
+ return o;
217
+ }, {})
218
+ };
219
+ function interpret (ast, fn, params, datum, event, item) {
220
+ const $ = n => Visitors[n.type]($, n);
221
+
222
+ $.memberDepth = 0;
223
+ $.fn = Object.create(fn);
224
+ $.params = params;
225
+ $.datum = datum;
226
+ $.event = event;
227
+ $.item = item; // route event functions to annotated vega event context
228
+
229
+ EventFunctions.forEach(f => $.fn[f] = (...args) => event.vega[f](...args));
230
+ return $(ast);
231
+ }
232
+
233
+ var expression = {
234
+ /**
235
+ * Parse an expression used to update an operator value.
236
+ */
237
+ operator(ctx, expr) {
238
+ const ast = expr.ast,
239
+ fn = ctx.functions;
240
+ return _ => interpret(ast, fn, _);
241
+ },
242
+
243
+ /**
244
+ * Parse an expression provided as an operator parameter value.
245
+ */
246
+ parameter(ctx, expr) {
247
+ const ast = expr.ast,
248
+ fn = ctx.functions;
249
+ return (datum, _) => interpret(ast, fn, _, datum);
250
+ },
251
+
252
+ /**
253
+ * Parse an expression applied to an event stream.
254
+ */
255
+ event(ctx, expr) {
256
+ const ast = expr.ast,
257
+ fn = ctx.functions;
258
+ return event => interpret(ast, fn, undefined, undefined, event);
259
+ },
260
+
261
+ /**
262
+ * Parse an expression used to handle an event-driven operator update.
263
+ */
264
+ handler(ctx, expr) {
265
+ const ast = expr.ast,
266
+ fn = ctx.functions;
267
+ return (_, event) => {
268
+ const datum = event.item && event.item.datum;
269
+ return interpret(ast, fn, _, datum, event);
270
+ };
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
+
288
+ for (const name in channels) {
289
+ v = interpret(channels[name].ast, fn, _, datum, undefined, item);
290
+
291
+ if (item[name] !== v) {
292
+ item[name] = v;
293
+ m = 1;
294
+ }
295
+ }
296
+
297
+ if (marktype !== 'rule') {
298
+ adjustSpatial(item, channels, swap);
299
+ }
300
+
301
+ return m;
302
+ };
303
+ }
304
+
305
+ };
306
+
307
+ exports.expressionInterpreter = expression;
308
+
309
+ Object.defineProperty(exports, '__esModule', { value: true });
310
+
311
+ })));