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

object-browser

v1.0.9

Published

A utility to access object value information using dot and array notations

Downloads

318

Readme

object-browser

Introduction

Object browser is a tool to access and retrieve intra-object values. It can work exactly like lodash's _.get method and was designed to mimic mongodb's ability to access documents nested in an array. The software was built to work with mongo-query-compiler utility.

Examples

/* sample object to be browsed */

import {browser} from 'object-browser';

let o = { 
  item: {
    type: 'book',
    description: {
      short: 'Some book'
    }
  },
  tags: ['book', 'technical'],
  reviews: undefined,
  store: [{
    number: '222', 
    qty: 51,
    region: 'NE',
    employees: [{
      name: {
        first: 'John',
        last: 'Doe'
      },
      age: 22
    }, {
      name: {
        first: 'Jack',
        last: 'Smith'
      },
      age: 27
    }]
  }, { 
    number: '212', 
    qty: 11,
    employees: [{
      name: {
        first: 'Kate',
        last: 'Jones'
      }
    }]
  }] 
};

Standard object value access

let shortDescription = browser(o, 'item.description.short');
//shortDescription = 'Some book'

Standard array value access

let tag = browser(o, 'tags.1')
//tag = 'technical'

Combined object and array value access

let firstName = browser(o, 'store.0.employees.0.name.first');
//firstName = 'John'

Additionally, access path may be specified as an array of path parts, rather than a dot-notated string.

let firstName = browser(o, ['store', '0', 'employees', '0', 'name', 'first']);
//firstName = 'John'

Access of values of objects nested in an array

let storeNumbers = browser(o, 'store.number');
//storeNumbers = ['222', '212']

Access of values of objects nested across multiple arrays

let employeeNames = browser(o, 'store.employees.name');
//employeeNames = []

store and its nested employees are both arrays and sometimes it may be meaningful to return an array of arrays (e.g., employee records for all stores). This is why object-browser's default behavior is to return an empty array when access is "requested" across several nested arrays. To allow access across multiple nested arrays, the flatten configuration flag should be set.

let employeeFirstNames = browser(o, 'store.employee.name.first', {flatten: true});
//employeeFirstNames = ['John', 'Jack', 'Kate']

Access of a browsed object itself

object-browser is a dependency for mongo-query-compiler. Sometimes the latter has to handle a use-case where an array of primitives is queried against a certain criteria. In that case, mongo-query-compiler has to run assessments on the "object" itself. To access the object itself, a path value of __self can be used.

let __self = browser(o, '__self');
//__self = o

Setting meta-data requests

A developer may instruct object-browser to indicate whether an accessed value exists and whether it has been combined into an array by pulling values of array-nested documents together.

let query = browser(o, 'info.description.full', {combined: true, exists: true});
//query = {value: undefined, combined: false, exists: false}

Note: existence of an object value is confirmed if an object has property that is being accessed. The value itself may be undefined.

let query = browser(o, 'reviews', {exists: true});
//query = {value: undefined, exists: true}