Skip to content

Types and traversal

How neotraverse treats different JavaScript values. Use t.getType(node) inside callbacks; use typeof when you need finer detail inside the 'primitive' bucket ('string' vs 'number', etc.).

JSON-like trees (the common case)

If your data looks like JSON.parse output, plain objects, arrays, null, strings, numbers, booleans, and nested combinations, you are on the supported path:

  • forEach / map / reduce visit every own enumerable property.
  • clone deep-copies structure and handles circular references.
  • get / set / has and getPath follow own keys only (no prototype chain).
  • includeSymbols: false (default) matches JSON (no symbol keys).

Typical API/config/document trees need nothing special. Add Date, RegExp, Map, or Set when your runtime actually uses them; see the table below for walk vs clone differences.

Full type reference

KindgetType()forEach / map walkclone()Notes
null'null'VisitedCopied
string, number, boolean, bigint, symbol, undefined'primitive'Visited as valuesCopiedUse typeof to branch
function'function'Node value only; not invokedSame reference on propertiesStripped by toJSON()
Plain object, class instance'object'Descends into own enumerable keysDeep clone; keeps prototypeBoxed new String() etc. are 'object' leaves
Array'array'Descends indicesDeep clone
Date'date'Leaf (no keys)Cloned via getTime()
RegExp'regexp'LeafCloned
Map'map'Leaf by default; with { descendIntoMapSet: true } visits each value at its keyDeep-clones entriesSee Iteration
Set'set'Leaf by default; with { descendIntoMapSet: true } visits each element at a numeric indexDeep-clones valuesSame as Map
WeakMap / WeakSet'weakmap' / 'weakset'Leaf (not enumerable in walk)clone / copy return the same referenceCannot query weak refs after GC
Typed array (Uint8Array, …)'typed-array'Descends index keys; copy uses .slice()Leaf in deep clone (buffer copied)
ArrayBuffer'arraybuffer'Leaf.slice(0)
DataView'dataview'LeafClones viewed byte range
Error'error'LeafDeep clone; shallow map copy is lossy (message only)deepEqual: message + name
Promise, URL, DOM nodes, …'object'Own enumerable keys only, if anyGeneric object copyHost objects may behave oddly

Deliberate limits

  • No prototype walking, only own properties (get/has never follow the chain). Safer on untrusted input.
  • No non-enumerable / getter-only keys unless you add your own logic.
  • Map / Set vs walk, by default the walker sees the collection as one node; pass descendIntoMapSet: true to visit entries. clone always deep-copies entries regardless.
  • Boxed primitives (new String('x')), classified as 'object', treated as leaves; unwrap with .valueOf() when needed.
  • diff / patch, acyclic trees only; circular graphs are unsupported.
  • Cross-realm, instanceof may fail; copy() falls back to tag checks for Date / RegExp.

Quick checks in callbacks

Example

ts
import * as t from 'neotraverse';

t.forEach(data, (ctx, x) => {
  switch (t.getType(x)) {
    case 'primitive':
      if (typeof x === 'string') {
        /* … */
      }
      break;
    case 'function':
      // present on the tree, but not called by neotraverse
      break;
    case 'map':
    case 'set':
      // walk leaf, use t.clone(x), descendIntoMapSet, or iterate x yourself
      break;
    case 'arraybuffer':
    case 'dataview':
    case 'typed-array':
      // binary data; clone copies bytes
      break;
    default:
      break;
  }
});

For transforming by kind in a single pass, see Walk variants → getType.

Released under the MIT License.

157.47Mtotal npm downloadssince Dec 2024