npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@suchipi/traverse

v5.1.0

Published

Basic depth-first traversal function

Downloads

930

Readme

@suchipi/traverse

Basic traverse function for deeply iterating over objects/arrays/etc.

Features

  • Only searches through an object's own properties
  • Can handle mutation/deletion during traversal
  • Provides your visitor callback a path to the current value (in addition to the value itself)
  • Avoids traversing over already-traversed values, to avoid cycles

Usage

import { traverse } from "@suchipi/traverse";

// It can traverse any JSON-compatible structure.
const tree = {
  type: "Root",
  left: {
    type: "Left",
    children: [1, 2, 3],
  },
  right: {
    type: "Right",
    children: [4, 5, 6],
  },
};

traverse(tree, {
  // If you provide an `after` callback, it will be called on the way *up*
  // the traversal stack; this is equivalent to a depth-first search.
  after: (value, path) => {
    console.log(`At '.${path.join(".")}':`, value);
  },
});
// Output:
//
// At '.type': 'Root'
// At '.left.type': 'Left'
// At '.left.children.0': 1
// At '.left.children.1': 2
// At '.left.children.2': 3
// At '.left.children': [ 1, 2, 3 ]
// At '.left': { type: 'Left', children: [ 1, 2, 3 ] }
// At '.right.type': 'Right'
// At '.right.children.0': 4
// At '.right.children.1': 5
// At '.right.children.2': 6
// At '.right.children': [ 4, 5, 6 ]
// At '.right': { type: 'Right', children: [ 4, 5, 6 ] }
// At '.': {
//   type: 'Root',
//   left: { type: 'Left', children: [ 1, 2, 3 ] },
//   right: { type: 'Right', children: [ 4, 5, 6 ] }
// }

traverse(tree, {
  // If you provide a `before` callback, it will be called on the way *down*
  // the traversal stack; this is equivalent to a breadth-first search.
  before: (value, path) => {
    console.log(`At '.${path.join(".")}':`, value);
  },
});
// Output:
//
// At '.': {
//   type: 'Root',
//   left: { type: 'Left', children: [ 1, 2, 3 ] },
//   right: { type: 'Right', children: [ 4, 5, 6 ] }
// }
// At '.type': Root
// At '.left': { type: 'Left', children: [ 1, 2, 3 ] }
// At '.left.type': Left
// At '.left.children': [ 1, 2, 3 ]
// At '.left.children.0': 1
// At '.left.children.1': 2
// At '.left.children.2': 3
// At '.right': { type: 'Right', children: [ 4, 5, 6 ] }
// At '.right.type': Right
// At '.right.children': [ 4, 5, 6 ]
// At '.right.children.0': 4
// At '.right.children.1': 5
// At '.right.children.2': 6

API

This module has two named exports:

  • A function named traverse
  • A Symbol named stop

traverse

The traverse function has the following type signature:

function traverse(
  tree: any,
  callbacks?: {
    before?: (value: any, path: Array<string | number>) => void | typeof stop;
    after?: (value: any, path: Array<string | number>) => void;
  },
): void;

Pass in any JSON-compatible value (the tree parameter) and a callback function (the callback parameter). Your tree will be searched through, and callback will be called for each descendant in the tree.

When callback is called, it will be called with two arguments:

  • The value of the current descendant (could be an object, an array, a string, a number, etc)
  • The path from the initially-passed tree object to this descendant (an array of strings or numbers)

stop

You can return stop from a before callback passed into traverse to stop traversing downwards beyond the current object's children.

Tips

  • The path Array your callback receives is suitable for use in lodash's set and get functions. As such, you can use lodash's set function to mutate a tree while you traverse over it.

  • If you want to create a new tree instead of mutating the existing one, you can combine lodash's set function with immer.

Troubleshooting

  • Don't pass an object with circular references into traverse. If you do, a callstack overflow error will occur. To verify that the object you're passing into traverse doesn't contain any circular references, you can try passing it into JSON.stringify; JSON.stringify will throw an error if you pass it an object with circular references.

  • If you're expecting an object property to be visited by traverse, but it isn't, make sure the property is an "own," "enumerable" property. For instance, class methods found on an instance of a class are not "own" properties, so traverse won't visit them. See MDN's article on 'Enumerability and ownership of properties' for more info.

  • The contents of Map and Set objects won't be visited by traverse, because it's only designed with JSON-compatible values in mind. However, the Map/Set itself will be visited. As such, if you want to inspect the contents of a Map/Set, you can do so from within your callback. Alternatively, you can convert Maps/Sets in your tree into Objects or Arrays prior to passing your tree into traverse.

License

MIT