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

@llhovind/golden-retriever

v1.0.0

Published

Resolver that finds properties from [deeply] nested objects/arrays given a path or pattern. It also collects properties from multiple branches, without throwing Type Errors.

Downloads

3

Readme

golden-retriever

Resolver that finds properties from [deeply] nested objects/arrays, including self referencing ones, given a path or pattern. It also collects properties from multiple branches, without throwing Type Errors.

Install

npm install golden-retriever --save

Example

import resolve from 'golden-retriever';

resolve('regions.east.manager.name', someObj);

which can change this:

    if ( company && company.regions && company.regions.east && company.regions.east.manager) {...}

to

    if ( resolve('regions.east.manager', company)) {...}

Usage

    given: const company = {...}

Retrieve a single property:

resolve('regions.east', company)

Retrieve a deeply nested property

resolve('regions.east.manager.name', company)
or
resolve('regions.east.manager["name"]', company)

bracket notation for property names is permitted as per JS syntax

Retrieve a property on any first level nested object properties

resolve('regions.*.manager.name', company)
or
resolve('regions[].manager.name', company)
or
resolve('regions[*].manager.name', company)

this will return an array of any 'manager.name' it finds in the first level of depth or 'manager.name' from any objects it finds in an array (if 'regions' is an array)

Retrieve a property on any level of nested objects

resolve('regions.**.manager.name', company)

this will return any 'manager.name' it finds at any depth including looking thru objects in arrays

Retrieve the first element in an array

resolve('regions.east.orders[0]', company)
or
resolve('regions.east.orders[first]', company)

Note that the word 'first' is NOT quoted. Quoting it would change the syntax to mean an indexed property.

Retrieve the last element in an array

resolve('regions.east.orders[last]', company)

Note that the word 'last' is NOT quoted. Quoting it would change the syntax to mean an indexed property.

Retrieve a path with an optional property

resolve('regions.*.?divisions[].name', company)

This will retrieve the 'name' property from any 'regions' as well as any 'regions's 'divisions'.

Return Values

'resolve()' will return a property's value if found, "undefined" if nothing was found or the path doesn't exist. And, it will return an array of values if properties where found on multiple branches, like when using wildcards. If the property you look for is an array, and it's found multiple times, 'resolve()' will return an array of arrays.

Notes

Self referencing graphs

Any graph (nested object structure with self references) will not be a problem. Golden-Retriever uses a Set to track all objects visited and will not revisit an object during an invocation.

This does have a uniques feature though. When calling:

resolve('**.orders[].customer', company)

it will return all the 'customer' property values regardless if many are the same exact object, because it's returning a property's value and not looking at the property. This can be compensated for by using the following syntax:

resolve('**.orders[].customer.*', company)

this will cause the resolver to look at the 'customer' value and thus only look at unique 'customer' objects. Aside from the later being de-duped, they both return the same results.

Living dangerously

You can attach 'resolve()' to the Object prototype for convenience as such:

    Object.prototype.resolve = resolve

this will change it's use to:

company.resolve('regions.east.manager.name')

Always use caution when extending built-in objects.