GithubHelp home page GithubHelp logo

belafonte / getpaths Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maugenst/getpaths

0.0 0.0 0.0 288 KB

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

License: GNU General Public License v3.0

Shell 1.30% TypeScript 98.70%

getpaths's Introduction

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

getpaths's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.