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-property-extractor

v1.0.12

Published

A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.

Downloads

89,354

Readme

Object Property Extractor

Access deep object properties using a string (e.g. "user.country.name")

A lightweight (no dependencies) tool to extract deeply nested values from JS Objects (or Arrays), with optional Fallback.

Similar to Lodash' get function, but with some additional functionality.

Why?

Consider the object

const data = {
  user: {
    name: { first: 'Jango', last: 'Fett' },
    children: ['Boba', 'Clone 1', 'Clone 2', ...etc],
    weapons: [
      { name: 'Blaster', description: 'For shooting stuff' },
      { name: 'Seismic charge', description: '...BWAAAAAANG' },
    ],
  },
  ...otherProperties,
}

In Javascript, you call inner object properties via dot notation:

data.user.name.last // Fett

If you want to access a property dynamically, you can do this:

const key = "user" 
return data[key]

However, you can't do this:

const key = "user.name"
return data[key]

This tool allows access to deep properties from a single "property path" string.

Installation

yarn add object-property-extractor
// OR
npm install object-property-extractor

Usage

extract( dataObject, propertyString, [fallback] )

import extract from "object-property-extractor"

// Using the data object above
extract(data, "user.name.first") // Jango

// With fallback when path not found
extract(data, "user.age", "Unknown") // Unknown

// Arrays can be accessed by index, as per normal indexing syntax
extract(data, "user.children[1]") // Boba

Array handling

In addition to accessing array by index (above), if an array consists of objects, then it's possible to extract a single property from each object in the returned array.

For example:

extract(data, "user.weapons.name")
// ["Blaster", "Seismic charge"]

Note that this is essentially a shorthand for:
extract(data, "user.weapons").map((weapon) => weapon.name)

Error handling

If a requested property can't be accessed (e.g. incorrect path), the function will throw an error, unless a fallback is provided. So unless you are catching and handling these errors at a higher level, it is recommended to always provided a fallback (null is an acceptable fallback).

Testing

A jest test suite is included in the repo. To run: yarn test

See /test/test.ts for the test cases.

Bug report / Feature requests

Please make an issue in the Github repo: https://github.com/CarlosNZ/object-property-extractor