CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-traverse

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

Pending
Overview
Eval results
Files

paths.mddocs/

Path Operations

Methods for accessing and modifying object properties using path arrays. Paths are arrays of property keys that describe the route to a specific value in nested object structures.

Capabilities

get Method

Retrieves the value at the specified path in the object.

/**
 * Get value at the specified path
 * @param {Array} path - Array of property keys representing the path
 * @returns {*} Value at the path, or undefined if path doesn't exist
 */
traverse(obj).get(path)

// Functional version
traverse.get(obj, path)

Usage Examples:

const traverse = require('traverse');

const obj = { 
    a: { 
        b: [1, 2, { c: 'hello' }] 
    } 
};

// Get nested values
const value1 = traverse(obj).get(['a', 'b', 2, 'c']); // 'hello'
const value2 = traverse(obj).get(['a', 'b', 0]);      // 1
const value3 = traverse(obj).get(['a', 'b']);         // [1, 2, { c: 'hello' }]
const value4 = traverse(obj).get(['nonexistent']);    // undefined

// Empty path returns the root object
const root = traverse(obj).get([]);                   // obj

// Functional approach
const value = traverse.get(obj, ['a', 'b', 2, 'c']); // 'hello'

set Method

Sets a value at the specified path, creating intermediate objects as needed.

/**
 * Set value at the specified path, creating intermediate objects as needed
 * @param {Array} path - Array of property keys representing the path
 * @param {*} value - Value to set at the path
 * @returns {*} The value that was set
 */
traverse(obj).set(path, value)

// Functional version
traverse.set(obj, path, value)

Usage Examples:

const traverse = require('traverse');

const obj = { a: { b: 1 } };

// Set existing path
traverse(obj).set(['a', 'b'], 42);
// obj is now: { a: { b: 42 } }

// Create new nested path
traverse(obj).set(['a', 'c', 'd'], 'hello');
// obj is now: { a: { b: 42, c: { d: 'hello' } } }

// Set array index
const arr = { items: [1, 2, 3] };
traverse(arr).set(['items', 1], 'modified');
// arr is now: { items: [1, 'modified', 3] }

// Functional approach
traverse.set(obj, ['x', 'y', 'z'], 'deep value');

has Method

Checks whether a path exists in the object.

/**
 * Check if the specified path exists in the object
 * @param {Array} path - Array of property keys representing the path
 * @returns {boolean} True if path exists, false otherwise
 */
traverse(obj).has(path)

// Functional version
traverse.has(obj, path)

Usage Examples:

const traverse = require('traverse');

const obj = { 
    a: { 
        b: [1, 2, { c: null }] 
    } 
};

// Check existing paths
traverse(obj).has(['a']);                    // true
traverse(obj).has(['a', 'b']);               // true  
traverse(obj).has(['a', 'b', 2, 'c']);      // true (even though value is null)
traverse(obj).has(['a', 'b', 0]);           // true

// Check non-existent paths
traverse(obj).has(['a', 'b', 'nonexistent']); // false
traverse(obj).has(['x']);                     // false
traverse(obj).has(['a', 'b', 5]);            // false (array index out of bounds)

// Empty path
traverse(obj).has([]);                        // true (root always exists)

// Functional approach
const exists = traverse.has(obj, ['a', 'b', 2, 'c']); // true

Path Format

Paths are arrays containing property keys that can be strings, numbers, or symbols:

// String keys for object properties
['property', 'nested', 'key']

// Number keys for array indices  
['items', 0, 'name']

// Mixed keys for complex structures
['users', 1, 'addresses', 0, 'street']

// Symbol keys (when includeSymbols: true)
const sym = Symbol('key');
[sym, 'property']

Symbol Support

By default, symbol properties are not included in path operations. Use the includeSymbols option to enable symbol support:

const sym = Symbol('hidden');
const obj = { 
    normal: 'value',
    [sym]: { data: 'secret' }
};

// Default behavior - symbols ignored
traverse(obj).has([sym]); // false

// With symbol support
traverse(obj, { includeSymbols: true }).has([sym]); // true
traverse(obj, { includeSymbols: true }).get([sym, 'data']); // 'secret'

Error Handling

Path operations handle edge cases gracefully:

  • Non-existent paths: get() returns undefined, has() returns false
  • Invalid intermediate objects: set() creates objects as needed
  • Array bounds: Out-of-bounds indices are treated as non-existent
  • Null/undefined intermediate values: Treated as non-traversable, causing path lookup to fail
  • Symbol properties: Ignored unless includeSymbols: true is used

Install with Tessl CLI

npx tessl i tessl/npm-traverse

docs

context.md

extraction.md

index.md

paths.md

traversal.md

tile.json