Vega Utilities

2.1.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates importing and using core utility functions like `isObject`, `merge`, `truthy`, and `pad`.

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);

view raw JSON →