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

find-object-paths

v1.1.0

Published

Find paths in objects to given key('s), value('s) or key/value combinations

Downloads

6,248

Readme

NPM

CircleCI Coverage Status

find-object-paths

Easy to use zero dependency get or find paths in objects to given key('s), value('s) or key/value combinations. It is meant to interact nicely with lodash to quickly access or test values in objects, but can be used standalone as well.

Despite other projects you also can specify values to be searched for.

Installation

Install via npm

npm install find-object-paths

Example data (see test-folder)

acmeInc.json

{
  "company": {
    "name": "ACME INC",
    "address": "1st Street, Toontown, TT",
    "founded": "December 31st 1969",
    "hasStocks": true,
    "numberOfEmployees": 2,
    "numberOfActors": 3
  }
  // see full content on github
}

Full Source of acmeInc

Basic usage

import { has, findObjectPaths } from 'find-object-paths';

class TestMe {

    static async main() {
        let acmeInc = {};
        rawFileContent = fs.readFileSync(p.resolve(__dirname, 'acmeInc.json'), 'utf-8');
        acmeInc = JSON.parse(rawFileContent);

        let path = findObjectPaths(acmeInc, {key: 'founded'});
        // company.founded
        path = findObjectPaths(acmeInc, {value: 'December 31st 1969'});
        // company.founded

        const allPaths: string[] = findObjectPaths(acmeInc, {key: 'actorId'}) as string[];
        /* [ 'actors[0].actorId',
             'actors[1].actorId',
             'actors[2].actorId',
             'actors[3].actorId' ]
         */

        const ceoPath = findObjectPaths(acmeInc, {key: 'isCEO', value: true});
         // employees[0].isCEO

        has(acmeInc, 'name', 'Hugo Boss');
        // true

        has(acmeInc, {value: 'Hugo Boss'});
        // true

    }
}

TestMe.main();

See more examples implemented in tests-findObjectPaths.

Convenient Methods

  • has Checks if a key / value combination is available in an object
  • findObjectPathsByKey Find a single or all path(s) matching to a key in an object
  • findObjectPathsByValue Find a single or all path(s) to a value in an object.
  • findObjectPathsByKeyValue Find a or all path(s) to a key with a given value in an object

Lodash Mixin

findObjectPaths can be used as a lodash mixin.

import _ from '../lib/FindObjectPathsMixin';
.
.
.
const path = _.findObjectPaths(acmeInc, {key: 'numberOfActors'});
// company.numberOfActors

path = _.findObjectPaths(acmeInc, {value: 'DVD'});
// distributionChannels[1][2]

if (_.has(acmeInc, _.findObjectPaths(acmeInc, {value: 'DVD'}))) {
    const dvd = _.get(acmeInc, _.findObjectPaths(acmeInc, {value: 'DVD'}));
    // do something
}

Tests

Tests can be found in /test and run by jest. To run the tests call npm test.

Changes

  • added has function to check availability of keys / values in objects or arrays