Vega Utilities
vega-util is a foundational JavaScript utility library within the Vega ecosystem, providing a comprehensive set of helper methods for common operations across Vega modules. It includes functions for type checking (e.g., `isObject`, `isArray`, `isString`), object manipulation (like `extend`, `merge`), array utilities, string operations, logging, error handling, and more. As of its current stable version v6.2.0, vega-util is exclusively distributed as an ESM module. The project maintains an active release cadence, frequently updating to incorporate new features, bug fixes, and security patches, as evidenced by recent v5.x and v6.x releases. Its primary differentiation lies in being a core, dependency-free utility library specifically tailored to support the data visualization functionalities of Vega and Vega-Lite.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` `vega-util` in a CommonJS context when the package is ESM-only since v6.0.0.fixChange `const { ... } = require('vega-util');` to `import { ... } from 'vega-util';` and ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`). -
TypeError: (0 , vega_util__WEBPACK_IMPORTED_MODULE_0__.isObject) is not a function
cause This Webpack/ESM bundling error typically occurs when attempting to import a default export that doesn't exist, or when a named export is incorrectly treated as a default.fixEnsure you are using named imports for `vega-util` functions: `import { isObject } from 'vega-util';`. Do not use `import isObject from 'vega-util';`. -
TS2305: Module '"vega-util"' has no exported member 'someFunction'.
cause Attempting to import a function that is not exported by `vega-util` or has been renamed/removed in a new version.fixVerify the function name and its availability in the specific `vega-util` version you are using by checking the official API documentation or source code. Ensure your TypeScript configuration is correctly resolving module types.
Warnings
- breaking Vega, including `vega-util`, transitioned to ESM-only in v6.0.0. Projects must use ES module imports (`import ... from 'vega-util'`) and cannot use CommonJS `require()` syntax.
- breaking A security advisory (GHSA-7f2v-3qq3-vvjf, CVE-2025-59840) addressed Cross-Site Scripting (XSS) vulnerabilities in Vega expressions abusing `toString` calls. This was fixed in `vega` v5.33.1 and implicitly affects packages consuming `vega-expression` or `vega-interpreter`.
- breaking Another XSS vulnerability (GHSA-963h-3v39-3pqf, CVE-2025-27793) related to `RegExp.prototype[@@replace]` when running Vega/Vega-Lite JSON definitions was patched. Users running untrusted specifications could execute unexpected JavaScript.
- gotcha The `Object.prototype.hasOwnProperty` usage was replaced with `Object.hasOwn` in `vega-util` v5.31.0. While not directly breaking for most users, this change enhances security by guarding against overridden `Object.prototype` built-ins. Code relying on custom `hasOwnProperty` behavior on `Object.prototype` might see subtle differences, though this is an uncommon pattern.
Install
-
npm install vega-util -
yarn add vega-util -
pnpm add vega-util
Imports
- isObject
const isObject = require('vega-util').isObject;import { isObject } from 'vega-util'; - merge
import merge from 'vega-util';
import { merge } from 'vega-util'; - error
import { error as vegaError } from 'vega-util/src/error';import { error } from 'vega-util';
Quickstart
import { isObject, merge, truthy, pad } from 'vega-util';
console.log('Is { a: 1 } an object?', isObject({ a: 1 }));
console.log('Is "hello" an object?', isObject('hello'));
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 }, e: 4 };
const merged = merge(obj1, obj2);
console.log('Merged object:', merged); // Expected: { a: 1, b: { d: 3 }, e: 4 }
const value = 42;
console.log(`Is ${value} truthy?`, truthy(value));
console.log(`Is null truthy?`, truthy(null));
const str = 'Vega';
const paddedStr = pad(str, 10, ' ', 'left');
console.log(`Padded string: "${paddedStr}"`);
// Example demonstrating a common utility pattern with conditional logic
function processConfig(userConfig: object = {}) {
const defaultConfig = { theme: 'light', animation: true, logLevel: 'info' };
if (!isObject(userConfig)) {
error('Invalid configuration: must be an object.');
return defaultConfig; // Fallback to default
}
return merge(defaultConfig, userConfig);
}
const myConfig = processConfig({ animation: false, logLevel: 'debug' });
console.log('Processed configuration:', myConfig);
const invalidConfig = processConfig('not an object');
console.log('Invalid config handling resulted in:', invalidConfig);