Skip to content

traverse walk()

Eugene Lazutkin edited this page Apr 3, 2026 · 6 revisions

Walks an object tree non-recursively, visiting all values with customizable processors.

Introduction

This is the foundation traversal module used by clone, assemble, deref, and preprocess. It uses a stack-based approach to avoid recursion limits and handles circular references.

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

const result = [];
walk({a: 1, b: {c: 2}}, {
  processOther: (v, ctx) => result.push(v),
  circular: true
});
// result contains all primitive values

API

walk(o [, options])

Arguments:

  • o — a required object to traverse. It can be anything.
  • options — an optional object. The following optional properties are recognized:
    • processObject — a function called for each object encountered.
    • processOther — a function called for non-object values.
    • processCircular — a function called when circular references are detected.
    • registry — an array of [Constructor, handler] pairs for custom types.
    • filters — an array of predicate functions to skip processing.
    • circular — a boolean flag to enable circular reference detection.
    • symbols — a boolean flag to include symbol properties.
    • allProps — a boolean flag to include non-enumerable properties.
    • context — a custom context object passed to processors.

Exports

  • Circular — marker class for circular references.
  • Command — deferred processing command class.
  • processOther — default non-object processor.
  • processCircular — default circular reference processor.
  • processMap(fn, fnSeen) — creates a Map processor.
  • postMapCircular — handles circular references in Maps.
  • buildNewMap — builds a new Map from processed values.
  • replaceObject — replaces values in stack with an object.
  • processObject(fn, fnSeen) — creates an Object processor.
  • postObjectCircular — handles circular references in Objects.
  • getObjectData — extracts property descriptors and keys.
  • buildNewObject — builds a new object from processed values.
  • processVariable — processor for Variable instances.
  • processCommand — processor for Command objects.
  • setObject — sets up circular reference handling.
  • registry — default registry of type handlers.
  • filters — default filter functions.

Clone this wiki locally