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

deep-chain

v1.0.0

Published

Helper to retrieve nested property value.

Downloads

7

Readme

deep-chain

deep-chain is a helper function that makes it easier to read properties from deeply nested data structures. It operates in a safe way, meaning that it doesn't throw errors at all, even when reading property from null or undefined entry. Additionally, it allows to find certain array element by specifying an optional predicate function or matching object.
See Usage section for more details.

Installation

$ npm install deep-chain

Usage

Consider the following example. We have a bookstore that sells many books and each book has a single author.

import { deep } from 'deep-chain';

const bookstore = {
  books: [
    {
      id: 1,
      title: 'Romeo and Juliet',
      author: {
        id: 1,
        name: 'William Shakespeare'
      }
    },
    {
      id: 2,
      title: 'The Double',
      author: {
        id: 2,
        name: 'Fyodor Dostoevsky'
      }
    },
    {
      id: 3,
      title: 'Fahrenheit 451',
      author: {
        id: 3,
        name: 'Ray Bradbury'
      }
    }
  ]
};

// Allows to read nested property.
deep(bookstore).books[1].author.name.retrieve(); // -> 'Fyodor Dostoevsky'

// Does not throw when reading from non-existent properties.
deep(bookstore).books[100].author.name.retrieve(); // -> undefined

// Allows to find array element by matching object.
deep(bookstore).books({ id: 3 }).name.retrieve(); // -> 'Fahrenheit 451'

// Allows to find array element by predicate function.
deep(bookstore)
  .books((book) => book.author.name === 'William Shakespeare')
  .name
  .retrieve(); // -> 'Romeo and Juliet'

Retrieving properties with reserved names

Currently these property names are reserved and return functions to perform certain action:

  • get
  • retrieve
  • filter
  • find

These functions don't have retrieve method at the moment, therefore it's not possible to retrieve a property with reserved name using this short syntax:

import { deep } from 'deep-chain';

const nestedObject = {
  test: {
    get:      1,
    retrieve: 2,
    filter:   3,
    find:     4
  }
};

deep(nestedObject).test.get.retrieve();      // -> Will throw TypeError
deep(nestedObject).test.retrieve.retrieve(); // -> Will throw TypeError
deep(nestedObject).test.filter.retrieve();   // -> Will throw TypeError
deep(nestedObject).test.find.retrieve();     // -> Will throw TypeError

However, you can retrieve them by using get method like that:

deep(nestedObject).test.get('get').retrieve();      // -> 1
deep(nestedObject).test.get('retrieve').retrieve(); // -> 2
deep(nestedObject).test.get('filter').retrieve();   // -> 3
deep(nestedObject).test.get('find').retrieve();     // -> 4