CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mapbox--mapbox-gl-style-spec

Specification and utilities for working with Mapbox GL styles including validation, migration, formatting, and expression evaluation

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

style-operations.mddocs/

Style Operations

Tools for manipulating, formatting, and transforming Mapbox GL styles including migration, formatting, diffing, and composition operations.

Capabilities

Style Formatting

Formats style JSON with proper key ordering and indentation for readability and consistency.

/**
 * Formats a style object with proper key ordering and indentation
 * @param style - Style object to format
 * @param space - Number of spaces for indentation (default: 2, use 0 for minified)
 * @returns Formatted JSON string
 */
function format(style: StyleSpecification, space?: number): string;

Usage Examples:

import { format } from "@mapbox/mapbox-gl-style-spec";

const style = {
  version: 8,
  layers: [{ id: "bg", type: "background" }],
  sources: {}
};

// Pretty formatted with 2 spaces
const formatted = format(style, 2);

// Minified (single line)
const minified = format(style, 0);

Style Migration

Migrates older style specifications to the latest version, updating deprecated syntax and properties.

/**
 * Migrates a style from older versions to the current specification
 * Handles v7 to v8 migration and legacy function to expression conversion
 * @param style - Style object to migrate (any version)
 * @returns Migrated style conforming to current specification
 */
function migrate(style: any): StyleSpecification;

Usage Examples:

import { migrate } from "@mapbox/mapbox-gl-style-spec";

// Migrate a v7 style to v8
const v7Style = {
  version: 7,
  sources: { /* ... */ },
  layers: [
    {
      id: "roads",
      type: "line",
      paint: {
        "line-color": {
          "property": "class",
          "type": "categorical",
          "stops": [["primary", "red"], ["secondary", "blue"]]
        }
      }
    }
  ]
};

const v8Style = migrate(v7Style);
// Result has expressions instead of functions

Style Diffing

Creates semantic diffs between two styles, generating command arrays for applying incremental changes.

/**
 * Creates a semantic diff between two styles
 * @param before - Original style
 * @param after - Modified style
 * @returns Array of diff commands for applying changes
 */
function diff(before: StyleSpecification, after: StyleSpecification): DiffCommand[];

type DiffCommand = 
  | SetStyleCommand
  | AddLayerCommand
  | RemoveLayerCommand
  | SetPaintPropertyCommand
  | SetLayoutPropertyCommand
  | SetFilterCommand
  | AddSourceCommand
  | RemoveSourceCommand
  | SetGeoJSONSourceDataCommand
  | SetLayerZoomRangeCommand
  | SetLightCommand
  | SetTerrainCommand
  | SetFogCommand;

interface SetStyleCommand {
  command: 'setStyle';
  args: [StyleSpecification];
}

interface AddLayerCommand {
  command: 'addLayer';
  args: [LayerSpecification, string?];
}

interface RemoveLayerCommand {
  command: 'removeLayer';
  args: [string];
}

interface SetPaintPropertyCommand {
  command: 'setPaintProperty';
  args: [string, string, any];
}

Usage Examples:

import { diff } from "@mapbox/mapbox-gl-style-spec";

const oldStyle = {
  version: 8,
  sources: {},
  layers: [
    { id: "bg", type: "background", paint: { "background-color": "white" } }
  ]
};

const newStyle = {
  version: 8,
  sources: {},
  layers: [
    { id: "bg", type: "background", paint: { "background-color": "black" } }
  ]
};

const commands = diff(oldStyle, newStyle);
// Result: [{ command: 'setPaintProperty', args: ['bg', 'background-color', 'black'] }]

// Apply commands to a map
commands.forEach(command => {
  if (command.command === 'setPaintProperty') {
    map.setPaintProperty(...command.args);
  }
  // Handle other command types...
});

Layer Dereferencing

Resolves layer ref properties by copying properties from referenced layers.

/**
 * Dereferences layers by resolving `ref` properties
 * @param layers - Array of layer specifications that may contain ref properties
 * @returns Array of layers with ref properties resolved
 */
function derefLayers(layers: LayerSpecification[]): LayerSpecification[];

Source Composition

Combines multiple Mapbox vector sources into a single composite source.

/**
 * Composites multiple vector sources into a single source
 * @param style - Style containing sources to composite
 * @returns Style with composited sources
 */
function composite(style: StyleSpecification): StyleSpecification;

Style Visiting

Utilities for iterating over style components with callback functions.

/**
 * Iterates over all sources in a style
 * @param style - Style to iterate over
 * @param callback - Function called for each source
 */
function eachSource(
  style: StyleSpecification, 
  callback: (source: SourceSpecification) => void
): void;

/**
 * Iterates over all layers in a style
 * @param style - Style to iterate over
 * @param callback - Function called for each layer
 */
function eachLayer(
  style: StyleSpecification, 
  callback: (layer: LayerSpecification) => void
): void;

/**
 * Iterates over layer properties with filtering options
 * @param style - Style to iterate over
 * @param options - Options for property iteration
 * @param callback - Function called for each property
 */
function eachProperty(
  style: StyleSpecification,
  options: {
    paint?: boolean;
    layout?: boolean;
  },
  callback: (property: {
    path: string[];
    key: string;
    value: any;
    reference: any;
    set: (value: any) => void;
  }) => void
): void;

Usage Examples:

import { eachSource, eachLayer, eachProperty } from "@mapbox/mapbox-gl-style-spec";

// Iterate over sources
eachSource(style, (source) => {
  console.log(`Source:`, source.type);
});

// Iterate over layers
eachLayer(style, (layer) => {
  console.log(`Layer ${layer.id}:`, layer.type);
});

// Iterate over paint properties
eachProperty(style, { paint: true }, (property) => {
  console.log(`Property ${property.key}:`, property.value);
});

Types

interface VisitOptions {
  paint?: boolean;
  layout?: boolean;
}

interface PropertyInfo {
  path: string[];
  key: string;
  value: any;
  reference: any;
  set: (value: any) => void;
}

docs

cli-tools.md

color-utilities.md

expressions.md

feature-filtering.md

index.md

style-manipulation.md

style-operations.md

validation.md

tile.json