Registry / serialization / vega2ol

vega2ol

JSON →
library1.1.1jsnpmunverified

A TypeScript library that transpiles Vega expressions to OpenLayers array-based expression format. v1.1.1 (latest), stable release. Key differentiator: bridges Vega-Lite/Vega visualization specs with OpenLayers map styling by converting conditional logic, math/string/array functions, and member access (e.g., datum.field) into OpenLayers expressions like ['case', ['>', ['get', 'value'], 100], 'red', 'blue']. Ideal for OpenLayers users who prefer Vega's concise JavaScript-like syntax.

npm install vega2ol
INSTALL
IMPORT
SIG · VEGA2OL
V
vega2ol
serializationjavascriptv1.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

vega2ol
import { vega2ol } from 'vega2ol'
const vega2ol = require('vega2ol')
ESM-only, shipped with TypeScript type definitions. Named export only; no default export.
vega2ol (default import)
import { vega2ol } from 'vega2ol'
import vega2ol from 'vega2ol'
Only a named export exists. Default import is not valid and will throw at runtime.
VegaToOLVisitor
import { VegaToOLVisitor } from 'vega2ol'
import { Visitor } from 'vega2ol'
Class for advanced usage if you need to extend or customize the transpiler. Exported but typically not imported directly; use vega2ol() function instead.

Transpile a Vega conditional expression to OpenLayers format and apply to a style object.

import { vega2ol } from 'vega2ol'; const vegaExpr = "datum.population > 1000000 ? '#FF0000' : '#0000FF'"; const olExpr = vega2ol(vegaExpr); console.log(olExpr); // ['case', ['>', ['get', 'population'], 1000000], '#FF0000', '#0000FF'] const style = { 'fill-color': olExpr, 'fill-opacity': vega2ol("datum.category == 'important' ? 1 : 0.5") }; console.log(style);
Debug
Known issues
gotchavega2ol does not support all Vega functions (e.g., instanceof, regexp). Unsupported functions will cause a runtime error.
fix
Check the documentation for a list of supported functions. Use only supported functionality or manually translate complex expressions.
affects: <=1.1.1
gotchaThe library uses vega-expression to parse expressions, which may have its own quirks (e.g., strict syntax). Incorrect Vega syntax will throw parse errors.
fix
Ensure your Vega expression is valid per Vega specification; avoid trailing commas or invalid operators.
affects: >=1.0.0
gotchaOpenLayers expression format is array-based, not an object or string. vega2ol returns arrays; do not stringify them prematurely.
fix
Use the returned array directly in OpenLayers style objects; do not call JSON.stringify() unless you intend to send it as data.
affects: >=1.0.0
gotchaMember access like datum.x is always converted to ['get', 'x'], which reads from OpenLayers feature properties. If your data is stored differently (e.g., in a different property name), you need to adjust the expression.
fix
Use only datum.fieldName pattern; OpenLayers expressions use ['get', 'x'] to access feature attributes. Ensure your feature has the expected property keys.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot find module 'vega2ol'
Missing installation or incorrect import in CommonJS environment.
fix
Ensure the package is installed: npm install vega2ol. Use ESM import syntax: import { vega2ol } from 'vega2ol'.
Error: Unsupported expression type: CallExpression with function 'myFunc'
The expression uses a function not supported by vega2ol.
fix
Use only supported functions: floor, ceil, round, abs, sqrt, pow, log, exp, sin, cos, tan, min, max, tostring, upper, lower, substring, length, indexof, slice, type, isvalid, isnumber, isstring, isboolean.
SyntaxError: Expected ')' but got ';'
Vega expression syntax error, often due to trailing semicolon or extra punctuation.
fix
Remove trailing semicolons and ensure valid Vega expression syntax. For example, use datum.x > 5 ? 'a' : 'b' without semicolons.
TypeError: vega2ol is not a function
Using default import instead of named import.
fix
Change import statement to import { vega2ol } from 'vega2ol'.
Upgrade
Version history
1.1.1latest on npm
Audit
Dependencies
vega-expressionrequiredParses Vega expression strings into an Abstract Syntax Tree (AST) for transpilation.
Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
vega2ol — npm install vega2ol · libregistry