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

smart-mole

v1.0.1

Published

make it easy to dig nested object returned by api.

Downloads

1

Readme

smart-mole

npm version GitHub issues GitHub license

Introduction

JavaScript library to dig nested object returned from api.

If api endpoint returns,

{
  animal: {
    mammal: {
      moles: [
        {
          name: 'parent1',
          children: [{ name: 'child1-1' }, { name: 'child1-2' }]
         },
        {
          name: 'parent2',
          children: [{ name: 'child2-1' }, { name: 'child2-2' }]
        }
      ]
    }
  }
}

and you want to get ['child1-2', 'child1-2', 'child2-1', 'child2-2'], with pure javascript,

object.animal.mammal.moles.map(mole => mole.children.name) // => ['child1-2', 'child1-2', 'child2-1', 'child2-2']

This code does not show the composition of the object, so it is difficult to understand how is the object.

On the other hand, with this library,

import { dig, $$target, $$array } from 'smart-mole'

dig(obj, {
  animal: {
    mammal: {
      moles: $$array({
        children: $$array({ name: $$target })
      })
    }
  }
})
// => ['child1-2', 'child1-2', 'child2-1', 'child2-2']

Quick start

Install

npm i smart-mole

or

yarn add smart-mole

How to use

import { dig } from smart-mole

dig(<target_object>, <target_map>, <target_marker>)

argument|description --|-- target_object|object or array you want to dig target_map|object or array presenting target location, by marking target with target_marker target_marker|optional, default is $$target which is a constant of **target**

Note that target_marker should NOT be included in any keys of <target_object>. if included, it will cause a bug. Bad example:

const object = { '**target**': { bar: 1 } }
dig(object, { '**target**': { bar: $$target } }

You should change target_marker like this.

dig(object, { '*foo': { bar: '***' } }, '***')

Algorithm

  1. Convert target_map to JSON string.
JSON.stringify(target_map)
// => {"animal":{"mammal":{"moles":[{"name":"_"},{"name":"*"}]}}}
  1. From the result of 1, search a object or array that includes target_marker.
{"animal":{"mammal":{"moles":[{"name":"_"},{"name":"*"}]}}}
//                                         ^^^^^^^^^^^^
=> {"name":"*"}
  1. From the result of 2. get the key and memorize it.
{"name":"*"}
=> 'name'
// keys <- 'name'
  1. replace the result of 2. by target_marker.
{"animal":{"mammal":{"moles":[{"name":"_"},{"name":"*"}]}}}
//                                         ^^^^^^^^^^^^
// replace => {"animal":{"mammal":{"moles":[{"name":"_"},"*"]}}}
  1. From the result of 4, search a object or array that includes target_marker(like 2.)
{"animal":{"mammal":{"moles":[{"name":"_"},"*"]}}}
//                           ^^^^^^^^^^^^^^^^^^
  1. get the key(or index) and memorize it.
[{"name":"_"},"*"]
=> 1 (index)
// keys <- 1
// keys: ['name', 1]
  1. repeat until the result of replacement is same as target_marker.
keys: ['name', 1, 'moles', 'mammal', 'animal']
  1. Using keys, dig a target object
let value = object
for (const key of keys.reverse()) {
 value = value[key]
}
value // => 'Don Resetti' (same as object.animal.mammal.moles[1].name)

Caution

You should just use Destructuring assignment in some case.

/*
object is {
  animal: {
    mamal: {
      moles: [
        { name: 'Mr. Resetti' },
        { name: 'Don Resetti' }
      ]
    }
  }
}
*/
// you can get 'Don Resetti' by the folloing method
const { animal: { mamal: { moles: [, { name }] } } } = object
console.log(mole) // => 'Don Resetti'

License

MIT