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-json

v0.5.1

Published

[![Build Status](https://travis-ci.org/rubeniskov/traverse-json.svg?branch=master)](https://travis-ci.org/rubeniskov/traverse-json) ![npm-publish](https://github.com/rubeniskov/traverse-json/workflows/npm-publish/badge.svg) [![Downloads](https://img.shiel

Downloads

111

Readme

traverse-json

Build Status npm-publish Downloads

A complete traverse json function with iterable interface.

Motivation

Many time I've encontered with the difficult task of traverse a object with nested properties by filtering some of them using a single function, so a traverse-json solves this using multiple options for traversing.

Mutation

For mutation this library is part of mutant-json which wraps this traverse-json to take the advantages of filtering options.

Installation

Npm:

npm install traverse-json --save

Yarn:

yarn add traverse-json

Functions

Typedefs

traverseJson(obj, [opts]) ⇒ TraverseIterator

Creates a function which traverses an object by its keys and values recursively, returning the iterator result with the full path and its value.

By default options will be parsed as { test: opts } if string detected

Kind: global function

| Param | Type | | --- | --- | | obj | Object | | [opts] | String | TraverseJsonOptions |

Example

const traverseJson = require('traverse-json');

const options = {...};

const iterator = traverseJson({
  foo: 0,
  nested: {
    depth: 1,
    nested: {
      depth: 2,
      nested: {
        depth: 3,
        nested: {
          depth: 4,
        },
      },
    },
  },
  bar: 1,
}, options);

for (;;) {
  const { done, value } = iterator();
  if (done)
     break;
   console.log(value);
}

Outputs

Default options

{}

[ '/foo', 0 ]
[ '/nested/depth', 1 ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]
[ '/bar', 1 ]

Return eather the nested and flatten values

{ nested: true }

[ '/foo', 0 ]
[ '/nested',
  { depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/nested/depth', 1 ]
[ '/nested/nested',
  { depth: 2, nested: { depth: 3, nested: [Object] } } ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested', { depth: 4 } ]
[ '/nested/nested/nested/nested/depth', 4 ]
[ '/bar', 1 ]

Only traverse on depth 1

{ recursive: false }

[ '/foo', 0 ]
[ '/nested',
  { depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/bar', 1 ]

Skips even entries

{ step: 2 }

[ '/foo', 0 ]
[ '/bar', 1 ]

Match only the paths ending with depth

{ test: /depth$/ }

[ '/nested/depth', 1 ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]

Return eather the nested and flatten values ending with nested

{ test: /nested$/, nested: true }

[ '/nested',
  { depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/nested/nested',
  { depth: 2, nested: { depth: 3, nested: [Object] } } ]
[ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ]
[ '/nested/nested/nested/nested', { depth: 4 } ]

Match only the paths ending with foo or depth

{ test: "**/{depth,foo}" }

[ '/foo', 0 ]
[ '/nested/depth', 1 ]
[ '/nested/nested/depth', 2 ]
[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]

Match entries which has a number value equal or greater than 3

{ test: ([,value]) => typeof value === 'number' && value >= 3 }

[ '/nested/nested/nested/depth', 3 ]
[ '/nested/nested/nested/nested/depth', 4 ]

Traverse recursively through the same key

{ test: "@nested" }

[ '/nested',
  { depth: 1, nested: { depth: 2, nested: [Object] } } ]
[ '/nested/nested',
  { depth: 2, nested: { depth: 3, nested: [Object] } } ]
[ '/nested/nested/nested', { depth: 3, nested: { depth: 4 } } ]
[ '/nested/nested/nested/nested', { depth: 4 } ]

createIterator(obj, [opts]) ⇒ Iterable

Returns a traverseJson iterable, usefull for use it in a for loop.

Kind: global function

| Param | Type | | --- | --- | | obj | Object | | [opts] | TraverseJsonOptions |

Example

const { createIterator } = require('traverse-json');
const options = {...}
const ientries = createIterator({
  foo: 0,
  nested: {
    depth: 1,
    nested: {
      depth: 2,
       nested: {
         depth: 3,
         nested: {
           depth: 4,
         },
       },
     },
   },
  bar: 1,
}, options);

for (let [k, v] of ientries) {
  console.log(k, v);
}

Output

/foo 0
/nested/depth 1
/nested/nested/depth 2
/nested/nested/nested/depth 3
/nested/nested/nested/nested/depth 4
/bar 1

TraverseJsonOptions : Object

Kind: global typedef
Properties

| Name | Type | Default | Description | | --- | --- | --- | --- | | [opts.recursive] | Boolean | true | enable/disable nested arrays and objects recursion | | [opts.nested] | Boolean | false | also emit nested array or objects | | [opts.step] | Boolean | 1 | the step to increment, default 1 | | [opts.test] | String | function | RegeExp | false | regexp, string minimatch or function to filter properties |

TraverseJsonEntry : Array.<String, any>

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | 0 | string | Object JSONPointer | | 1 | any | Value |

TraverseIteratorResult : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | value | TraverseJsonEntry | key value pair with the key as a full path of the property following the json standard path format | | done | Boolean | |

TraverseIterator ⇒ TraverseIteratorResult

Function iterator, see examples

Kind: global typedef

| Param | Description | | --- | --- | | extra | a object or array to extends the current iteration |