or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

context.mdextraction.mdindex.mdpaths.mdtraversal.md
tile.json

tessl/npm-traverse

Traverse and transform objects by visiting every node on a recursive walk

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/traverse@0.6.x

To install, run

npx @tessl/cli install tessl/npm-traverse@0.6.0

index.mddocs/

Traverse

Traverse is a JavaScript library for recursively walking and transforming objects by visiting every node. It provides both immutable transformations and in-place modifications, with comprehensive support for circular reference detection, deep cloning, and path-based operations on complex nested data structures.

Package Information

  • Package Name: traverse
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install traverse

Core Imports

const traverse = require('traverse');

For ES modules (if supported by environment):

import traverse from 'traverse';

Basic Usage

const traverse = require('traverse');

// Transform negative numbers in-place
const obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];
traverse(obj).forEach(function (x) {
    if (x < 0) this.update(x + 128);
});
// obj is now: [5, 6, 125, [7, 8, 126, 1], { f: 10, g: 115 }]

// Collect leaf nodes using reduce
const data = { a: [1,2,3], b: 4, c: [5,6], d: { e: [7,8], f: 9 } };
const leaves = traverse(data).reduce(function (acc, x) {
    if (this.isLeaf) acc.push(x);
    return acc;
}, []);
// Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Architecture

Traverse uses a unified approach with both functional and constructor patterns:

  • Constructor Pattern: new traverse(obj) or traverse(obj) returns a Traverse instance
  • Functional Pattern: All instance methods are available as static functions (e.g., traverse.map(obj, fn))
  • Traversal Context: Callbacks receive rich context via this object with navigation and modification methods
  • Immutable Operations: Methods like map() and clone() preserve original objects
  • In-place Operations: Methods like forEach() modify objects directly
  • Type Handling: Special support for Arrays, Dates, RegExp, Errors, typed arrays, and circular references

Capabilities

Object Traversal

Core traversal methods for iterating through object structures with callback functions that receive rich context information.

// Constructor approach
function traverse(obj, options) // Returns Traverse instance

// Instance methods
traverse(obj).forEach(callback)     // In-place modification
traverse(obj).map(callback)         // Immutable transformation  
traverse(obj).reduce(callback, init) // Reduce to single value

// Functional approach  
traverse.forEach(obj, callback, options)
traverse.map(obj, callback, options)
traverse.reduce(obj, callback, init)

Object Traversal

Path Operations

Methods for accessing and modifying object properties using path arrays, enabling programmatic navigation of nested structures.

traverse(obj).get(path)        // Get value at path
traverse(obj).set(path, value) // Set value at path  
traverse(obj).has(path)        // Check if path exists

// Functional versions
traverse.get(obj, path)
traverse.set(obj, path, value)  
traverse.has(obj, path)

Path Operations

Data Extraction

Utility methods for extracting information from objects including all paths, all node values, and deep cloning with circular reference handling.

traverse(obj).paths()  // Get all non-cyclic paths
traverse(obj).nodes()  // Get all node values
traverse(obj).clone()  // Deep clone with circular reference handling

// Functional versions
traverse.paths(obj)
traverse.nodes(obj)
traverse.clone(obj, options)

Data Extraction

Types

// Options for traversal operations
interface TraverseOptions {
  immutable?: boolean;      // Create immutable copies during traversal
  includeSymbols?: boolean; // Include symbol properties in traversal
}

// Path type - array of property keys
type Path = (string | number | symbol)[];

Context API

When using callback functions with traversal methods, the this context provides extensive information and control methods. The context includes navigation properties, state flags, and modification methods.

Traversal Context