Traverse and transform objects by visiting every node on a recursive walk
npx @tessl/cli install tessl/npm-traverse@0.6.0Traverse 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.
npm install traverseconst traverse = require('traverse');For ES modules (if supported by environment):
import traverse from 'traverse';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]Traverse uses a unified approach with both functional and constructor patterns:
new traverse(obj) or traverse(obj) returns a Traverse instancetraverse.map(obj, fn))this object with navigation and modification methodsmap() and clone() preserve original objectsforEach() modify objects directlyCore 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)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)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)// 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)[];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.