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

jmeta

v1.1.0

Published

JSON metadata generator, outputs lodash compatible get/set strings

Downloads

5

Readme

jMeta Build Status Coverage Status

A JSON mapping utility to help generate meta data of a JSON object. Maps depth and paths to found keys.

Generated paths are compatible with Lodash's _.get and _.set to help manipulate deep data sets.

The generated data is in an accessible Map object which you can run generator functions over as necessary or use the built in keys() and paths() to retrieve information in a more familiar way.

Install

npm install jmeta or yarn add jmeta

Basic Usage

  const JMeta = require('jmeta')
  const _ = require('lodash')

  const data = {
    a: {
      b: {
        c: [
          [ { d: true }, 'ignored', { a: 'duplicate' } ]
        ]
      }
    }
  }
  const jmeta = new JMeta(data)

  console.log(jmeta.paths())     // Outputs: [ 'a', 'a.b.c[0][2].a', 'a.b', 'a.b.c', 'a.b.c[0][0].d' ]
  console.log(jmeta.keys())      // Outputs: [ 'a', 'b', 'c', 'd' ] NOTE: Unique keys
  console.log(jmeta.size)        // Outputs: 5 (NOTE: Accounts for duplicate found keys)
  console.log(jmeta.duplicates)  // Outputs: [ 'a' ]

  let foo = _.get(data, jmeta.paths()[1]) // 'a.b.c[0][2].a'
  console.log(foo)                        // Outputs: 'duplicate'

  // Access the map object directly
  console.log(jmeta.map.get('a')) // Outputs: [ { depth: 1, path: 'a' }, { depth: 6, path: 'a.b.c[0][2].a' } ]

  // You also have the option to get only the "leaves" of the tree. This will return all the final paths and not their connecting branches to them.
  const jmeta = new JMeta(data, { finalPathsOnly: true })

  console.log(jmeta.paths()) // Outputs [ 'a.b.c[0][0].d', 'a.b.c[0][2].a' ]

Paths()

Takes an options object as its only parameter to specify some basic filtering. The options object supports the following filters: { depth: <number>, includes: <string>, key: <string> }. Filtering options can be combined in any way to reduce results accordingly.

NOTE: Paths() will always return an array, on no results found the return will simply be an empty array [].

Filtering Paths

  const JMeta = require('jmeta')
  const _ = require('lodash')

  const data = {
    location: {
      france: [ { name: 'Tom' }, { name: 'Mary' } ],
      italy: [ { name: 'Mike' } ]
    }
  }
  const jmeta = new JMeta(data)

  // DEPTH FILTERING
  console.log(jmeta.paths({ depth: 2 })) // Outputs: ['location.france', 'location.italy']
  console.log(jmeta.paths({ depth: 4 })) // Outputs: ['location.france[0].name', 'location.france[1].name', 'location.italy[0].name']

  // PATH INCLUDES FILTERING
  console.log(jmeta.paths({ includes: 'france' }))    // Outputs: ['location.france', 'location.france[0].name', 'location.france[1].name']
  console.log(jmeta.paths({ includes: 'france[0]' })) // Outputs: ['location.france[0].name']

  // KEY FILTERING
  console.log(jmeta.paths({ key: 'france' })) // Outputs: ['location.france']
  console.log(jmeta.paths({ key: 'name' })  // Outputs: ['location.france[0].name', 'location.france[1].name', 'location.italy[0].name']

Keys()

Takes an options object as its only parameter to specify some basic filtering The options object supports the following filters: { depth: <number> }.

  const JMeta = require('jmeta')
  const _ = require('lodash')

  const data = {
    location: {
      france: [ { person: 'Tom' }, { person: 'Mary' } ],
      italy: [ { person: 'Mike' } ]
    }
  }
  const jmeta = new JMeta(data)

  console.log(jmeta.keys())               // Outputs: [ 'location', 'france', 'person', 'italy' ] NOTE: Returns unique found keys only
  console.log(jmeta.keys({ depth: 2 }))   // Outputs: [ 'france', 'italy' ]
  console.log(jmeta.keys({ depth: 4 }))   // Outputs: [ 'person' ]