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

delver

v1.1.5

Published

Read and update nested objects using simple patterns.

Downloads

81

Readme

Delver

Read and update nested objects using simple patterns.

Examples

get

const Delver = require( 'delver' );

let obj = {
    foo: {
        bar: 'value',
        arr: [ 1, 2, 3 ]
    }
};

let delver = new Delver( obj );

console.log( delver.get( 'foo.bar' ) );
console.log( delver.get( 'foo.bar2', 'default' ) );
console.log( delver.get( 'foo.arr[1]' ) );

// You don't actually need a Delver instance.
console.log( Delver.get( obj, 'foo.bar' ) );
console.log( Delver.get( obj, 'foo.bar2' ) );
console.log( Delver.get( obj, 'foo.arr[1]' ) );

output:

value
default
2
value
undefined
2

set

const Delver = require( 'delver' );

let obj = {};
let delver = new Delver( obj );

delver.set( 'foo.bar', 'value' );
console.log( delver.get( 'foo.bar' ) );

delver.set( 'foo.baz[]', 'value1' );
delver.set( 'foo.baz[]', 'value2' );

// You don't actually need a Delver instance.
Delver.set( obj, 'foo.baz[]', 'value3' );

console.log( delver.get( 'foo.baz' ) );

output:

value
[ 'value1', 'value2', 'value3' ]

Why would I need this?

One example is to allow for more succinct REST requests.

Let's say you have an object of type 'foo' with id '123'. It looks like this:

{
    "name": "bloop",
    "media": {
        "photos": [
            {
                "url": "http://somewhere.com/images/turtle.jpg",
                "preview": "http://somewhere.com/images/turtle-preview.jpg",
                "width": 50,
                "height": 50
            },
            {
                "url": "http://somewhere.com/images/snake.jpg",
                "preview": "http://somewhere.com/images/snake-preview.jpg",
                "width": 50,
                "height": 50
            }
        ]
    }
}

Now, let's say you want to update the width of the snake picture to be 100 instead of 50. Because your server-side setup doesn't allow dot notation access, maybe you need to send back the entire array of photos with the new updated value:

curl http://somewhere.com/foo/123
  -X PUT
  -H 'Content-Type: application/json'
  -H 'Accept: application/json'
  --data-binary '{
    "media": {
        "photos": [
            {
                "url": "http://somewhere.com/images/turtle.jpg",
                "preview": "http://somewhere.com/images/turtle-preview.jpg",
                "width": 50,
                "height": 50
            },
            {
                "url": "http://somewhere.com/images/snake.jpg",
                "preview": "http://somewhere.com/images/snake-preview.jpg",
                "width": 100,
                "height": 50
            }
        ]
    }
  }'

But with Delver on the server, you could send:

curl http://somewhere.com/foo/123
  -X PUT
  -H 'Content-Type: application/json'
  -H 'Accept: application/json'
  --data-binary '{
    "media.photos[1].width": 100
  }'

And then in your node.js server, Delver will let you find the appropriate field in the 'foo' object and update it directly.

Methods

new Delver( object )

Creates a Delver instance with an object to operate on.

.get( key, defaultValue )

Returns the internal object's value at the given key if found, else returns defaultValue.

If called with no arguments, will return the bare internal object.

.set( key, value )

Sets the internal object's value to value at the given key and overrides it if it already exists.

Delver.get( object, key, defaultValue] )

Returns the given object's value at the given key if found, else returns defaultValue.

Delver.set( object, key, value )

Sets the given object's value to value at the given key and overrides it if it already exists.

Install

With npm do:

npm install delver

License

MIT

Credits

Delver is based off a fork of Tim Taubert's node-accessors. Many thanks to him for a great little library.

Delver was created to enable a couple of additional features:

  • array level element access, eg:
    Delver.get( obj, 'foo.bar[3]' ); // get element with index 3 of array 'bar'
    Delver.set( obj, 'foo.bar[4].baz', 'yak' ); // set field 'baz' of element with index 4 of array 'bar'
  • allow for a less strict traversal of objects
    • specifically, I needed to support mongoose objects which have accessors instead of hasOwnProperty()-checkable members
  • add ability to enable/disable automatic path creation