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

traverse-object

v0.0.1

Published

Recursively traverse an object tree and callback a function for each property found.

Downloads

2

Readme

traverse-object

Traverse a Javascript object tree (assuming it is not cyclic) and call a callback for each found node.

In addition, you can filter all property paths (e.g. 'user.0.firstName') by an Array of RegExp or Strings so that you only visit the nodes you care about.

Definition

traverse(object, callback, propertiesStrOrRegEx = undefined)
  • object (required): any iterable (e.g. by in) Javascript Object.
  • callback (required): a function to call on every matched property. Has signature (obj, path, value). obj will be the immediate parent object of the match. path will be an Array of the full path to the current match. value will be the value of the matched property. Return true from the callback if you want to stop traversing.
  • propertiesOrStrOrRegEx (optional): a filter for which properties to visit. A String, RegExp, or an Array of mixed Strings or RegExps. If you do not pass this argument, then every descendent property is visited and the callback will be called for all of them.

Property paths and propertiesOrStrOrRegEx Wildcards

While traversing the tree of objects, the current path is considered to be a .-delimited String. This String is tested against the value(s) in propertiesOrStrOrRegEx, which can zero or more explicit RegExp instances or Strings.

Strings are converted to RegExp objects where the * character is considered a .*. For example the String '*firstName' will be converted into new RegExp(".*firstName"). Regular expressions are cached between calls but lastIndex=0 is explicitly set before each test to ensure that there is not state leaking between calls.

For example, if you want to visit the tree for any property with a path that ends in 'firstName' you could pass in '*firstName'. If you want to visit the tree for all properties that have 'firstName' anywhere in their path, you can pass in '*firstName*'.

Simple Example

var traverse = require('traverse-object');

let user = {
  firstName: 'Joshua',
  lastName: 'Jung',
  profession: {
    name: 'Software Engineer',
    contributions: ['trie-search', 'traverse-object']
  }
};

traverse(user, console.log); // Will output (obj, path, value) for each key in the tree

Advanced Example

Sanitize an object tree of all passwords and oauth tokens:

var traverse = require('../index');

let users = [
  { name: 'Josh', password: 'asdfasdf1234' },
  { name: 'Matej', token: 'testtest' },
  { name: 'Elisia', password: 'asdfasdf1234' },
  { name: 'Jonas' },
  { name: 'Lasse', password: 'asdfasdf1234' },
  { name: 'MyCorp LLC', oauth: { token: 'asdfasdf1234' } },
];

let sanitizeCallback = (obj, path, value) => {
  delete obj[path[path.length - 1]];
};

traverse(users, sanitizeCallback, [/password/, '*token']);

console.log(users);

// [ { name: 'Josh' },
//   { name: 'Matej' },
//   { name: 'Elisia' },
//   { name: 'Jonas' },
//   { name: 'Lasse' },
//   { name: 'MyCorp LLC', oauth: {} } ]

Testing

$ npm i -g mocha

$ npm test

...

16 passing (10ms)

License

The MIT License (MIT)

Copyright (c) 2019 Joshua Jung

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.