vega2ol 1.1.3 ā 1.1.4
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.
- package/package.json +1 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -4
- package/dist/main.d.ts +0 -25
- package/dist/main.js +0 -273
- package/dist/tests/test-basic.d.ts +0 -1
- package/dist/tests/test-basic.js +0 -303
package/package.json
CHANGED
package/dist/index.d.ts
DELETED
package/dist/index.js
DELETED
package/dist/main.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { ExpressionValue } from "ol/expr/expression.js";
|
|
2
|
-
/**
|
|
3
|
-
* OpenLayers expression types
|
|
4
|
-
* OL expressions are arrays: ['operator', arg1, arg2, ...] or ['case', condition, value, ...]
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Vega AST node types (simplified)
|
|
8
|
-
*/
|
|
9
|
-
interface VegaNode {
|
|
10
|
-
type: string;
|
|
11
|
-
[key: string]: any;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Custom error class for Vega2OL transpilation errors
|
|
15
|
-
*/
|
|
16
|
-
export declare class Vega2OLError extends Error {
|
|
17
|
-
constructor(message: string);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Main transpiler function
|
|
21
|
-
* @param vegaExpressionStr - A Vega expression string (e.g., "datum.value > 100 ? 'red' : 'blue'")
|
|
22
|
-
* @returns OpenLayers expression array
|
|
23
|
-
*/
|
|
24
|
-
export declare function vega2ol(vegaExpressionStr: string): ExpressionValue;
|
|
25
|
-
export { ExpressionValue as OLExpression, VegaNode };
|
package/dist/main.js
DELETED
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
import { parseExpression } from "vega-expression";
|
|
2
|
-
/**
|
|
3
|
-
* Operator mapping from Vega operators to OpenLayers operators
|
|
4
|
-
*/
|
|
5
|
-
const OPERATOR_MAPPING = {
|
|
6
|
-
// Comparison operators
|
|
7
|
-
"==": "==",
|
|
8
|
-
"!=": "!=",
|
|
9
|
-
"<": "<",
|
|
10
|
-
"<=": "<=",
|
|
11
|
-
">": ">",
|
|
12
|
-
">=": ">=",
|
|
13
|
-
// Logical operators
|
|
14
|
-
"&&": "all",
|
|
15
|
-
"||": "any",
|
|
16
|
-
// Arithmetic operators
|
|
17
|
-
"+": "+",
|
|
18
|
-
"-": "-",
|
|
19
|
-
"*": "*",
|
|
20
|
-
"/": "/",
|
|
21
|
-
"%": "%",
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Constants mapping from Vega constants to OpenLayers constants
|
|
25
|
-
*/
|
|
26
|
-
const CONSTANTS_MAPPING = {
|
|
27
|
-
PI: Math.PI,
|
|
28
|
-
E: Math.E,
|
|
29
|
-
LN2: Math.LN2,
|
|
30
|
-
LN10: Math.LN10,
|
|
31
|
-
LOG2E: Math.LOG2E,
|
|
32
|
-
LOG10E: Math.LOG10E,
|
|
33
|
-
SQRT1_2: Math.SQRT1_2,
|
|
34
|
-
SQRT2: Math.SQRT2,
|
|
35
|
-
MIN_VALUE: Number.MIN_VALUE,
|
|
36
|
-
MAX_VALUE: Number.MAX_VALUE,
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* Function mapping from Vega functions to OpenLayers functions
|
|
40
|
-
*/
|
|
41
|
-
const FUNCTION_MAPPING = {
|
|
42
|
-
// Math functions
|
|
43
|
-
abs: "abs",
|
|
44
|
-
floor: "floor",
|
|
45
|
-
ceil: "ceil",
|
|
46
|
-
round: "round",
|
|
47
|
-
sqrt: "sqrt",
|
|
48
|
-
pow: "^",
|
|
49
|
-
sin: "sin",
|
|
50
|
-
cos: "cos",
|
|
51
|
-
atan: "atan",
|
|
52
|
-
atan2: "atan",
|
|
53
|
-
clamp: "clamp",
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* Custom error class for Vega2OL transpilation errors
|
|
57
|
-
*/
|
|
58
|
-
export class Vega2OLError extends Error {
|
|
59
|
-
constructor(message) {
|
|
60
|
-
const fullMessage = `${message}, note that only a subset of Vega is supported`;
|
|
61
|
-
super(fullMessage);
|
|
62
|
-
this.name = "Vega2OLError";
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Visitor class to convert Vega AST to OpenLayers expressions
|
|
67
|
-
*/
|
|
68
|
-
class VegaToOLVisitor {
|
|
69
|
-
constructor() {
|
|
70
|
-
this.scope = {};
|
|
71
|
-
}
|
|
72
|
-
isNegativeOne(node) {
|
|
73
|
-
// Case 1: Literal -1
|
|
74
|
-
if (node.type === "Literal" && node.value === -1) {
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
// Case 2: UnaryExpression: -1
|
|
78
|
-
if (node.type === "UnaryExpression" &&
|
|
79
|
-
node.operator === "-" &&
|
|
80
|
-
node.argument?.type === "Literal" &&
|
|
81
|
-
node.argument.value === 1) {
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
visit(node) {
|
|
87
|
-
if (!node || !node.type) {
|
|
88
|
-
throw new Vega2OLError("Invalid AST node");
|
|
89
|
-
}
|
|
90
|
-
const method = `visit${node.type}`;
|
|
91
|
-
if (typeof this[method] === "function") {
|
|
92
|
-
return this[method](node);
|
|
93
|
-
}
|
|
94
|
-
throw new Vega2OLError(`Unsupported ${node.type} node`);
|
|
95
|
-
}
|
|
96
|
-
// Literal values
|
|
97
|
-
visitLiteral(node) {
|
|
98
|
-
const { value } = node;
|
|
99
|
-
if (value === null)
|
|
100
|
-
return null;
|
|
101
|
-
if (typeof value === "boolean")
|
|
102
|
-
return value;
|
|
103
|
-
if (typeof value === "number")
|
|
104
|
-
return value;
|
|
105
|
-
if (typeof value === "string")
|
|
106
|
-
return value;
|
|
107
|
-
throw new Vega2OLError(`Unsupported literal type: ${typeof value}`);
|
|
108
|
-
}
|
|
109
|
-
// Member access: datum.value, datum['value']
|
|
110
|
-
visitMemberExpression(node) {
|
|
111
|
-
const { object, property } = node;
|
|
112
|
-
if (object.type === "Identifier" && object.name === "datum") {
|
|
113
|
-
if (typeof property === "string") {
|
|
114
|
-
return ["get", property];
|
|
115
|
-
}
|
|
116
|
-
// For computed properties - if property is an Identifier with 'name'
|
|
117
|
-
if (property.type === "Identifier" && property.name) {
|
|
118
|
-
return ["get", property.name];
|
|
119
|
-
}
|
|
120
|
-
// For computed properties like datum[variable]
|
|
121
|
-
return ["get", this.visit(property)];
|
|
122
|
-
}
|
|
123
|
-
throw new Vega2OLError(`Unsupported member access`);
|
|
124
|
-
}
|
|
125
|
-
isIndexOfCall(node) {
|
|
126
|
-
return (node.type === "CallExpression" &&
|
|
127
|
-
node.callee?.type === "Identifier" &&
|
|
128
|
-
node.callee.name.toLowerCase() === "indexof");
|
|
129
|
-
}
|
|
130
|
-
// Binary operations: a + b, a > b, etc.
|
|
131
|
-
visitBinaryExpression(node) {
|
|
132
|
-
const { operator, left, right } = node;
|
|
133
|
-
// Special handling for indexOf comparisons
|
|
134
|
-
if (operator === "!=" || operator === "==") {
|
|
135
|
-
let call = null;
|
|
136
|
-
if (this.isIndexOfCall(left) && this.isNegativeOne(right)) {
|
|
137
|
-
call = left;
|
|
138
|
-
}
|
|
139
|
-
else if (this.isIndexOfCall(right) && this.isNegativeOne(left)) {
|
|
140
|
-
call = right;
|
|
141
|
-
}
|
|
142
|
-
if (call) {
|
|
143
|
-
const [arrayArg, valueArg] = call.arguments || [];
|
|
144
|
-
if (arrayArg && valueArg) {
|
|
145
|
-
const arrayOL = this.visit(arrayArg);
|
|
146
|
-
const valueOL = this.visit(valueArg);
|
|
147
|
-
const inExpr = [
|
|
148
|
-
"in",
|
|
149
|
-
valueOL,
|
|
150
|
-
["literal", arrayOL],
|
|
151
|
-
];
|
|
152
|
-
// indexof(...) != -1 ā is in
|
|
153
|
-
if (operator === "!=") {
|
|
154
|
-
return inExpr;
|
|
155
|
-
}
|
|
156
|
-
// indexof(...) == -1 ā is NOT in
|
|
157
|
-
if (operator === "==") {
|
|
158
|
-
return ["!", inExpr];
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
const leftOL = this.visit(left);
|
|
164
|
-
const rightOL = this.visit(right);
|
|
165
|
-
const olOp = OPERATOR_MAPPING[operator];
|
|
166
|
-
if (!olOp) {
|
|
167
|
-
throw new Vega2OLError(`Unsupported binary operator: ${operator}`);
|
|
168
|
-
}
|
|
169
|
-
return [olOp, leftOL, rightOL];
|
|
170
|
-
}
|
|
171
|
-
// Unary operations: !x, -x, +x
|
|
172
|
-
visitUnaryExpression(node) {
|
|
173
|
-
const { operator, argument } = node;
|
|
174
|
-
const argOL = this.visit(argument);
|
|
175
|
-
if (operator === "!") {
|
|
176
|
-
return ["!", argOL];
|
|
177
|
-
}
|
|
178
|
-
if (operator === "-") {
|
|
179
|
-
return ["*", argOL, -1];
|
|
180
|
-
}
|
|
181
|
-
if (operator === "+") {
|
|
182
|
-
return argOL;
|
|
183
|
-
}
|
|
184
|
-
throw new Vega2OLError(`Unsupported unary operator: ${operator}`);
|
|
185
|
-
}
|
|
186
|
-
// Conditional: test ? consequent : alternate
|
|
187
|
-
visitConditionalExpression(node) {
|
|
188
|
-
const { test, consequent, alternate } = node;
|
|
189
|
-
const testOL = this.visit(test);
|
|
190
|
-
const consequentOL = this.visit(consequent);
|
|
191
|
-
const alternateOL = this.visit(alternate);
|
|
192
|
-
// OL 'case' expression: ['case', condition1, value1, condition2, value2, ..., defaultValue]
|
|
193
|
-
return ["case", testOL, consequentOL, alternateOL];
|
|
194
|
-
}
|
|
195
|
-
// Function calls: floor(x), ceil(y), etc.
|
|
196
|
-
visitCallExpression(node) {
|
|
197
|
-
const { callee, arguments: args } = node;
|
|
198
|
-
let funcName;
|
|
199
|
-
if (callee.type === "Identifier") {
|
|
200
|
-
funcName = callee.name;
|
|
201
|
-
}
|
|
202
|
-
else if (callee.type === "MemberExpression" &&
|
|
203
|
-
callee.object.name === "Math") {
|
|
204
|
-
funcName = callee.property;
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
throw new Vega2OLError(`Unsupported function call`);
|
|
208
|
-
}
|
|
209
|
-
if (this.isIndexOfCall(node)) {
|
|
210
|
-
throw new Vega2OLError(`indexof is only supported in the pattern 'indexof(array, value) != -1' (or '== -1'), ` +
|
|
211
|
-
`it cannot be used as a standalone expression in OpenLayers`);
|
|
212
|
-
}
|
|
213
|
-
const olFunc = FUNCTION_MAPPING[funcName.toLowerCase()];
|
|
214
|
-
if (!olFunc) {
|
|
215
|
-
throw new Vega2OLError(`Unsupported function: ${funcName}`);
|
|
216
|
-
}
|
|
217
|
-
const olArgs = args.map((arg) => this.visit(arg));
|
|
218
|
-
return [olFunc, ...olArgs];
|
|
219
|
-
}
|
|
220
|
-
// Array literal: [1, 2, 3]
|
|
221
|
-
visitArrayExpression(node) {
|
|
222
|
-
const { elements } = node;
|
|
223
|
-
return elements.map((elem) => this.visit(elem));
|
|
224
|
-
}
|
|
225
|
-
// Logical expressions
|
|
226
|
-
visitLogicalExpression(node) {
|
|
227
|
-
const { operator, left, right } = node;
|
|
228
|
-
const leftOL = this.visit(left);
|
|
229
|
-
const rightOL = this.visit(right);
|
|
230
|
-
const olOp = OPERATOR_MAPPING[operator];
|
|
231
|
-
if (!olOp) {
|
|
232
|
-
throw new Vega2OLError(`Unsupported logical operator: ${operator}`);
|
|
233
|
-
}
|
|
234
|
-
return [olOp, leftOL, rightOL];
|
|
235
|
-
}
|
|
236
|
-
// Identifier/Variable: x, PI, etc.
|
|
237
|
-
visitIdentifier(node) {
|
|
238
|
-
const { name } = node;
|
|
239
|
-
// Check if it's in the current scope
|
|
240
|
-
if (name in this.scope) {
|
|
241
|
-
return this.scope[name];
|
|
242
|
-
}
|
|
243
|
-
if (name in CONSTANTS_MAPPING) {
|
|
244
|
-
return CONSTANTS_MAPPING[name];
|
|
245
|
-
}
|
|
246
|
-
// Return as string (might be a function or field)
|
|
247
|
-
return name;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Main transpiler function
|
|
252
|
-
* @param vegaExpressionStr - A Vega expression string (e.g., "datum.value > 100 ? 'red' : 'blue'")
|
|
253
|
-
* @returns OpenLayers expression array
|
|
254
|
-
*/
|
|
255
|
-
export function vega2ol(vegaExpressionStr) {
|
|
256
|
-
if (typeof vegaExpressionStr !== "string") {
|
|
257
|
-
throw new Vega2OLError("Input must be a string");
|
|
258
|
-
}
|
|
259
|
-
try {
|
|
260
|
-
// Parse Vega expression to AST
|
|
261
|
-
const ast = parseExpression(vegaExpressionStr);
|
|
262
|
-
// Visit the AST and convert to OL expression
|
|
263
|
-
const visitor = new VegaToOLVisitor();
|
|
264
|
-
const olExpression = visitor.visit(ast);
|
|
265
|
-
return olExpression;
|
|
266
|
-
}
|
|
267
|
-
catch (error) {
|
|
268
|
-
if (error instanceof Vega2OLError) {
|
|
269
|
-
throw error;
|
|
270
|
-
}
|
|
271
|
-
throw new Vega2OLError(`Failed to parse expression: ${error.message}`);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/tests/test-basic.js
DELETED
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
import { vega2ol, Vega2OLError } from "../main.js";
|
|
2
|
-
// Simple assertion helper
|
|
3
|
-
function assertEqual(actual, expected, message) {
|
|
4
|
-
const actualStr = JSON.stringify(actual);
|
|
5
|
-
const expectedStr = JSON.stringify(expected);
|
|
6
|
-
if (actualStr === expectedStr) {
|
|
7
|
-
console.log(` ā
${message}`);
|
|
8
|
-
return true;
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
console.log(` ā ${message}`);
|
|
12
|
-
console.log(` Expected: ${expectedStr}`);
|
|
13
|
-
console.log(` Got: ${actualStr}`);
|
|
14
|
-
return false;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
// Assert that vega2ol throws a Vega2OLError for the given expression
|
|
18
|
-
async function assertThrows(expr, message, messageContains) {
|
|
19
|
-
try {
|
|
20
|
-
const result = await vega2ol(expr);
|
|
21
|
-
console.log(` ā ${message}`);
|
|
22
|
-
console.log(` Expected a Vega2OLError, but got result: ${JSON.stringify(result)}`);
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
if (!(error instanceof Vega2OLError)) {
|
|
27
|
-
console.log(` ā ${message}`);
|
|
28
|
-
console.log(` Expected Vega2OLError, got: ${error.constructor.name}: ${error.message}`);
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
if (messageContains && !error.message.includes(messageContains)) {
|
|
32
|
-
console.log(` ā ${message}`);
|
|
33
|
-
console.log(` Error message did not contain "${messageContains}"`);
|
|
34
|
-
console.log(` Got message: ${error.message}`);
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
console.log(` ā
${message}`);
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
// Run tests
|
|
42
|
-
async function runTests() {
|
|
43
|
-
console.log("š Testing vega2ol transpiler\n");
|
|
44
|
-
let passed = 0;
|
|
45
|
-
let failed = 0;
|
|
46
|
-
try {
|
|
47
|
-
// Test 1: Literal values
|
|
48
|
-
console.log("š Literal Values");
|
|
49
|
-
if (assertEqual(await vega2ol("42"), 42, "Number literal"))
|
|
50
|
-
passed++;
|
|
51
|
-
else
|
|
52
|
-
failed++;
|
|
53
|
-
if (assertEqual(await vega2ol("'hello'"), "hello", "String literal"))
|
|
54
|
-
passed++;
|
|
55
|
-
else
|
|
56
|
-
failed++;
|
|
57
|
-
if (assertEqual(await vega2ol("true"), true, "Boolean literal"))
|
|
58
|
-
passed++;
|
|
59
|
-
else
|
|
60
|
-
failed++;
|
|
61
|
-
// Test 2: Member access
|
|
62
|
-
console.log("\nš Member Access (datum.field)");
|
|
63
|
-
if (assertEqual(await vega2ol("datum.value"), ["get", "value"], "Simple datum access"))
|
|
64
|
-
passed++;
|
|
65
|
-
else
|
|
66
|
-
failed++;
|
|
67
|
-
if (assertEqual(await vega2ol("datum.population"), ["get", "population"], "Datum field access"))
|
|
68
|
-
passed++;
|
|
69
|
-
else
|
|
70
|
-
failed++;
|
|
71
|
-
// Test 3: Comparison operators
|
|
72
|
-
console.log("\nš Comparison Operators");
|
|
73
|
-
if (assertEqual(await vega2ol("datum.value > 100"), [">", ["get", "value"], 100], "Greater than operator"))
|
|
74
|
-
passed++;
|
|
75
|
-
else
|
|
76
|
-
failed++;
|
|
77
|
-
if (assertEqual(await vega2ol("datum.value == 100"), ["==", ["get", "value"], 100], "Equality operator"))
|
|
78
|
-
passed++;
|
|
79
|
-
else
|
|
80
|
-
failed++;
|
|
81
|
-
// Test 4: Ternary/Conditional (the main example from docs)
|
|
82
|
-
console.log("\nš Conditional Expressions");
|
|
83
|
-
if (assertEqual(await vega2ol("datum.value > 100 ? 'red' : 'blue'"), ["case", [">", ["get", "value"], 100], "red", "blue"], "Simple ternary expression"))
|
|
84
|
-
passed++;
|
|
85
|
-
else
|
|
86
|
-
failed++;
|
|
87
|
-
// Test 5: Complex nested conditional
|
|
88
|
-
if (assertEqual(await vega2ol("datum.value > 100 ? 'red' : datum.value > 50 ? 'yellow' : 'green'"), [
|
|
89
|
-
"case",
|
|
90
|
-
[">", ["get", "value"], 100],
|
|
91
|
-
"red",
|
|
92
|
-
["case", [">", ["get", "value"], 50], "yellow", "green"],
|
|
93
|
-
], "Nested ternary expression"))
|
|
94
|
-
passed++;
|
|
95
|
-
else
|
|
96
|
-
failed++;
|
|
97
|
-
// Test 6: Logical operators
|
|
98
|
-
console.log("\nš Logical Operators");
|
|
99
|
-
if (assertEqual(await vega2ol("datum.x > 0 && datum.y < 100"), ["all", [">", ["get", "x"], 0], ["<", ["get", "y"], 100]], "AND operator"))
|
|
100
|
-
passed++;
|
|
101
|
-
else
|
|
102
|
-
failed++;
|
|
103
|
-
if (assertEqual(await vega2ol("datum.a > 1 || datum.b < 5"), ["any", [">", ["get", "a"], 1], ["<", ["get", "b"], 5]], "OR operator"))
|
|
104
|
-
passed++;
|
|
105
|
-
else
|
|
106
|
-
failed++;
|
|
107
|
-
// Test 7: Arithmetic operators
|
|
108
|
-
console.log("\nš Arithmetic Operators");
|
|
109
|
-
if (assertEqual(await vega2ol("datum.a + datum.b"), ["+", ["get", "a"], ["get", "b"]], "Addition operator"))
|
|
110
|
-
passed++;
|
|
111
|
-
else
|
|
112
|
-
failed++;
|
|
113
|
-
if (assertEqual(await vega2ol("datum.a * datum.b"), ["*", ["get", "a"], ["get", "b"]], "Multiplication operator"))
|
|
114
|
-
passed++;
|
|
115
|
-
else
|
|
116
|
-
failed++;
|
|
117
|
-
// Test 8: Unary operations
|
|
118
|
-
console.log("\nš Unary Operations");
|
|
119
|
-
if (assertEqual(await vega2ol("!datum.active"), ["!", ["get", "active"]], "Negation operator"))
|
|
120
|
-
passed++;
|
|
121
|
-
else
|
|
122
|
-
failed++;
|
|
123
|
-
if (assertEqual(await vega2ol("-datum.value"), ["*", ["get", "value"], -1], "Unary minus"))
|
|
124
|
-
passed++;
|
|
125
|
-
else
|
|
126
|
-
failed++;
|
|
127
|
-
// Test 9: Function calls
|
|
128
|
-
console.log("\nš Function Calls");
|
|
129
|
-
if (assertEqual(await vega2ol("floor(datum.value)"), ["floor", ["get", "value"]], "Floor function"))
|
|
130
|
-
passed++;
|
|
131
|
-
else
|
|
132
|
-
failed++;
|
|
133
|
-
if (assertEqual(await vega2ol("ceil(datum.value)"), ["ceil", ["get", "value"]], "ceil()"))
|
|
134
|
-
passed++;
|
|
135
|
-
else
|
|
136
|
-
failed++;
|
|
137
|
-
if (assertEqual(await vega2ol("round(datum.value)"), ["round", ["get", "value"]], "round()"))
|
|
138
|
-
passed++;
|
|
139
|
-
else
|
|
140
|
-
failed++;
|
|
141
|
-
if (assertEqual(await vega2ol("abs(datum.value)"), ["abs", ["get", "value"]], "abs()"))
|
|
142
|
-
passed++;
|
|
143
|
-
else
|
|
144
|
-
failed++;
|
|
145
|
-
if (assertEqual(await vega2ol("sqrt(datum.value)"), ["sqrt", ["get", "value"]], "sqrt()"))
|
|
146
|
-
passed++;
|
|
147
|
-
else
|
|
148
|
-
failed++;
|
|
149
|
-
if (assertEqual(await vega2ol("sin(datum.value)"), ["sin", ["get", "value"]], "sin()"))
|
|
150
|
-
passed++;
|
|
151
|
-
else
|
|
152
|
-
failed++;
|
|
153
|
-
if (assertEqual(await vega2ol("cos(datum.value)"), ["cos", ["get", "value"]], "cos()"))
|
|
154
|
-
passed++;
|
|
155
|
-
else
|
|
156
|
-
failed++;
|
|
157
|
-
if (assertEqual(await vega2ol("pow(datum.base, 2)"), ["^", ["get", "base"], 2], "pow() maps to OL's '^' operator, not a 'pow' function"))
|
|
158
|
-
passed++;
|
|
159
|
-
else
|
|
160
|
-
failed++;
|
|
161
|
-
if (assertEqual(await vega2ol("atan(datum.value)"), ["atan", ["get", "value"]], "atan() single-arg form"))
|
|
162
|
-
passed++;
|
|
163
|
-
else
|
|
164
|
-
failed++;
|
|
165
|
-
if (assertEqual(await vega2ol("atan2(datum.x, datum.y)"), ["atan", ["get", "x"], ["get", "y"]], "atan2() two-arg form"))
|
|
166
|
-
passed++;
|
|
167
|
-
else
|
|
168
|
-
failed++;
|
|
169
|
-
if (assertEqual(await vega2ol("clamp(datum.value, 0, 100)"), ["clamp", ["get", "value"], 0, 100], "clamp() preserves (value, min, max) arg order"))
|
|
170
|
-
passed++;
|
|
171
|
-
else
|
|
172
|
-
failed++;
|
|
173
|
-
// Test 10: Real-world example - Color mapping
|
|
174
|
-
console.log("\nš Real-world Examples");
|
|
175
|
-
if (assertEqual(await vega2ol("datum.population > 1000000 ? '#FF0000' : datum.population > 500000 ? '#FFA500' : '#FFFF00'"), [
|
|
176
|
-
"case",
|
|
177
|
-
[">", ["get", "population"], 1000000],
|
|
178
|
-
"#FF0000",
|
|
179
|
-
["case", [">", ["get", "population"], 500000], "#FFA500", "#FFFF00"],
|
|
180
|
-
], "Color mapping by population size"))
|
|
181
|
-
passed++;
|
|
182
|
-
else
|
|
183
|
-
failed++;
|
|
184
|
-
// Test 11: Array expressions
|
|
185
|
-
console.log("\nš Array Expressions");
|
|
186
|
-
if (assertEqual(await vega2ol("[1, 2, 3]"), [1, 2, 3], "Array literal"))
|
|
187
|
-
passed++;
|
|
188
|
-
else
|
|
189
|
-
failed++;
|
|
190
|
-
// Test 12: Constants
|
|
191
|
-
console.log("\nš Vega Constants");
|
|
192
|
-
if (assertEqual(await vega2ol("PI"), Math.PI, "PI constant"))
|
|
193
|
-
passed++;
|
|
194
|
-
else
|
|
195
|
-
failed++;
|
|
196
|
-
// Test 13: Binary expressions
|
|
197
|
-
console.log("\nš Binary Expressions");
|
|
198
|
-
if (assertEqual(await vega2ol("indexof(['USA', 'India'], datum.country) != -1"), ["in", ["get", "country"], ["literal", ["USA", "India"]]], "indexOf with != -1 (is in)"))
|
|
199
|
-
passed++;
|
|
200
|
-
else
|
|
201
|
-
failed++;
|
|
202
|
-
if (assertEqual(await vega2ol("-1 != indexof(['USA', 'India'], datum.country)"), ["in", ["get", "country"], ["literal", ["USA", "India"]]], "reverse indexOf with != -1 (is in)"))
|
|
203
|
-
passed++;
|
|
204
|
-
else
|
|
205
|
-
failed++;
|
|
206
|
-
if (assertEqual(await vega2ol("indexof(['hot', 'warm'], datum.temperature) != -1 ? 'red' : 'blue'"), [
|
|
207
|
-
"case",
|
|
208
|
-
["in", ["get", "temperature"], ["literal", ["hot", "warm"]]],
|
|
209
|
-
"red",
|
|
210
|
-
"blue",
|
|
211
|
-
], "indexOf in ternary (if in)"))
|
|
212
|
-
passed++;
|
|
213
|
-
else
|
|
214
|
-
failed++;
|
|
215
|
-
if (assertEqual(await vega2ol("indexof(['USA', 'India'], datum.country) == -1"), ["!", ["in", ["get", "country"], ["literal", ["USA", "India"]]]], "indexOf with == -1 (is not in)"))
|
|
216
|
-
passed++;
|
|
217
|
-
else
|
|
218
|
-
failed++;
|
|
219
|
-
if (assertEqual(await vega2ol("-1 == indexof(['USA', 'India'], datum.country)"), ["!", ["in", ["get", "country"], ["literal", ["USA", "India"]]]], "reverse indexOf with == -1 (is not in)"))
|
|
220
|
-
passed++;
|
|
221
|
-
else
|
|
222
|
-
failed++;
|
|
223
|
-
// Test 14: Unsupported / unknown function errors
|
|
224
|
-
console.log("\nš Errors: Vega Functions With No OL Equivalent");
|
|
225
|
-
if (await assertThrows("upper(datum.name)", "upper() throws (no OL string case operator)", "Unsupported function"))
|
|
226
|
-
passed++;
|
|
227
|
-
else
|
|
228
|
-
failed++;
|
|
229
|
-
if (await assertThrows("lower(datum.name)", "lower() throws (no OL string case operator)", "Unsupported function"))
|
|
230
|
-
passed++;
|
|
231
|
-
else
|
|
232
|
-
failed++;
|
|
233
|
-
if (await assertThrows("toString(datum.value)", "toString() throws (no OL type coercion operator)", "Unsupported function"))
|
|
234
|
-
passed++;
|
|
235
|
-
else
|
|
236
|
-
failed++;
|
|
237
|
-
if (await assertThrows("isValid(datum.value)", "isValid() throws (no OL type-checking operator)", "Unsupported function"))
|
|
238
|
-
passed++;
|
|
239
|
-
else
|
|
240
|
-
failed++;
|
|
241
|
-
if (await assertThrows("length(datum.items)", "length() throws (no OL length operator)", "Unsupported function"))
|
|
242
|
-
passed++;
|
|
243
|
-
else
|
|
244
|
-
failed++;
|
|
245
|
-
if (await assertThrows("format(datum.value, ',.2f')", "format() throws (no OL number-formatting operator)", "Unsupported function"))
|
|
246
|
-
passed++;
|
|
247
|
-
else
|
|
248
|
-
failed++;
|
|
249
|
-
if (await assertThrows("now()", "now() throws (no OL date/time operators)", "Unsupported function"))
|
|
250
|
-
passed++;
|
|
251
|
-
else
|
|
252
|
-
failed++;
|
|
253
|
-
if (await assertThrows("min(datum.a, datum.b)", "min() throws (no OL aggregate min/max operator)", "Unsupported function"))
|
|
254
|
-
passed++;
|
|
255
|
-
else
|
|
256
|
-
failed++;
|
|
257
|
-
if (await assertThrows("tan(datum.value)", "tan() throws (OL only supports sin/cos/atan)", "Unsupported function"))
|
|
258
|
-
passed++;
|
|
259
|
-
else
|
|
260
|
-
failed++;
|
|
261
|
-
if (await assertThrows("indexof(datum.items, 'x')", "bare indexof() outside the != -1 / == -1 pattern throws a specific error", "indexof is only supported in the pattern"))
|
|
262
|
-
passed++;
|
|
263
|
-
else
|
|
264
|
-
failed++;
|
|
265
|
-
console.log("\nš Errors: Names That Don't Exist In Vega Or OL");
|
|
266
|
-
if (await assertThrows("totallyMadeUpFunction(datum.value)", "Nonexistent function name throws", "Unsupported function"))
|
|
267
|
-
passed++;
|
|
268
|
-
else
|
|
269
|
-
failed++;
|
|
270
|
-
if (await assertThrows("fooBarBaz(1, 2, 3)", "Another nonexistent function name throws", "Unsupported function"))
|
|
271
|
-
passed++;
|
|
272
|
-
else
|
|
273
|
-
failed++;
|
|
274
|
-
// Test 15: Error handling
|
|
275
|
-
console.log("\nš Error Handling");
|
|
276
|
-
try {
|
|
277
|
-
await vega2ol(null);
|
|
278
|
-
console.log("ā Should throw for null input");
|
|
279
|
-
failed++;
|
|
280
|
-
}
|
|
281
|
-
catch (error) {
|
|
282
|
-
if (error instanceof Vega2OLError) {
|
|
283
|
-
console.log(" ā
Throws Vega2OLError for invalid input");
|
|
284
|
-
passed++;
|
|
285
|
-
}
|
|
286
|
-
else {
|
|
287
|
-
console.log("ā Wrong error type");
|
|
288
|
-
failed++;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
catch (error) {
|
|
293
|
-
console.error("š„ Test suite error:", error);
|
|
294
|
-
failed++;
|
|
295
|
-
}
|
|
296
|
-
// Summary
|
|
297
|
-
console.log("\n" + "=".repeat(50));
|
|
298
|
-
console.log(`š Test Results: ${passed} passed, ${failed} failed`);
|
|
299
|
-
console.log("=".repeat(50));
|
|
300
|
-
return failed === 0 ? 0 : 1;
|
|
301
|
-
}
|
|
302
|
-
// Run the tests
|
|
303
|
-
runTests().then((code) => process.exit(code));
|