{"library":"object-traversal","title":"Flexible Object Traversal","description":"The `object-traversal` package provides a flexible and highly performant utility for depth-first or breadth-first traversal of JavaScript objects. Currently at stable version 1.0.1, the library maintains an active release cadence, with recent updates fixing minor issues after a major breaking change. Its key differentiators include exceptional speed, claiming to traverse over 20 million nodes per second and being approximately 10 times faster than popular alternatives, along with extensive configurability options for traversal order, maximum depth, and cycle handling. The library is lightweight with zero external dependencies, making it suitable for both Node.js (>=10) and browser environments, and it ships with full TypeScript support, providing robust type definitions for callbacks and options.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install object-traversal"],"cli":null},"imports":["import { traverse } from 'object-traversal';","import type { TraversalCallback } from 'object-traversal';","import type { TraversalCallbackContext } from 'object-traversal';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { traverse, TraversalCallbackContext } from 'object-traversal';\n\nconst exampleObject = {\n  name: 'Hello World!',\n  age: 1,\n  accounts: 2,\n  friends: 3,\n  nested: {\n    value: 10,\n    list: [1, 2, { deep: 5 }],\n  },\n  arr: [{ num: 42 }],\n};\n\nfunction doubleNumbers({ parent, key, value, meta }: TraversalCallbackContext) {\n  // `parent` and `key` are null for the root object. \n  // Only modify if we are inside an object/array property.\n  if (parent && key && typeof value === 'number') {\n    parent[key] = value * 2;\n    console.log(`Doubled ${meta.nodePath}: ${value} -> ${parent[key]}`);\n  }\n}\n\nconsole.log('Original object:', JSON.stringify(exampleObject, null, 2));\n\n// Perform in-place modification\ntraverse(exampleObject, doubleNumbers, {\n    traversalType: 'depth-first', // Explicitly set for clarity (default is 'depth-first')\n    pathSeparator: '/', // Use a different separator for demonstration\n});\n\nconsole.log('\\nModified object:', JSON.stringify(exampleObject, null, 2));\n\n// Example of finding a node and halting traversal\nlet foundNodePath: string | null = null;\ntraverse(exampleObject, ({ value, meta }) => {\n    if (value === 84) { // Searching for the doubled value of 42\n        foundNodePath = meta.nodePath;\n        return true; // Halt traversal once found\n    }\n}, { haltOnTruthy: true });\n\nconsole.log(`\\nFound value 84 at path: ${foundNodePath}`);","lang":"typescript","description":"Demonstrates how to use `traverse` to modify an object in-place by doubling all numeric values and finding a specific node while halting traversal early.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}