Skip to content

traverse preprocess()

Eugene Lazutkin edited this page May 8, 2026 · 5 revisions

Preprocesses patterns for unification by wrapping objects with open/soft markers.

Introduction

Prepares objects for pattern matching by marking them as "open" (can have extra properties) or "exact" (must match exactly). Used internally by match().

import preprocess from 'deep6/traverse/preprocess.js';
import unify from 'deep6/unify.js';

// Open pattern - matches objects with at least these properties
const openPattern = preprocess({a: 1}, {openObjects: true});

// Closed pattern - matches objects with exactly these properties
const closedPattern = preprocess({a: 1}, {openObjects: false});

API

preprocess(pattern [, options])

Arguments:

  • pattern — a required pattern object to preprocess.
  • options — an optional object. The following optional properties are recognized:
    • openObjects — a boolean flag. When true, objects can have extra properties (default: depends on caller).
    • openArrays — a boolean flag. When true, arrays can have extra elements.
    • openMaps — a boolean flag. When true, Maps can have extra entries.
    • openSets — a boolean flag. When true, Sets can have extra elements.
    • circular — a boolean flag to handle circular references.
    • symbols — a boolean flag to include symbol properties.
    • allProps — a boolean flag to include non-enumerable properties.
    • context — a custom context object.
    • processObject — a custom object processor.
    • processOther — a custom value processor.
    • processCircular — a custom circular reference processor.
    • registry — a custom type handler registry (flat array of Constructor/handler pairs). See walk().
    • filters — custom filter functions. See walk().

The function returns a preprocessed pattern ready for unify().

Registry

The preprocess module exports registry and filters arrays for custom type handling:

import preprocess from 'deep6/traverse/preprocess.js';

// Custom type: pass through without wrapping
import {processOther} from 'deep6/traverse/walk.js';
preprocess.registry.push(MyClass, processOther);

Built-in registry handles: Command, Array, Variable, Unifier, Date, RegExp, Map, Set, URL, Promise, typed arrays, DataView, ArrayBuffer. Non-container types pass through unchanged.

Notes

Implemented using walk(). The main match() function uses this internally with {openObjects: true, openArrays: true, openMaps: true, openSets: true, circular: true} by default.

Clone this wiki locally