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

getsetprop

v1.1.0

Published

Deeply nested object property getter and setter

Downloads

5

Readme

getsetprop

Deeply nested object property getter and setter for JavaScript

Signature

For Getter

Returns value in object at specified path.

var get = require('getsetprop').get;
var value = get(object, path);

For Setter

Sets value at specified path in object.

var set = require('getsetprop').set;
set(object, path, value);

Terminology

For getter

  • string path: access path for getters ('a.b.c')

For setter

  • primitive value: primitive value to be set at specified access path ('val', 2, etc.)
  • object path: access path for setters also containing new value ({ a: { b: 'val' } }).
  • object value: object containing both path and new value ({ a: { b: 'val' } })

Rules

  • object path can only be used in setter
  • object path cannot also specify primitive value (or object value)
  • string path needs primitive value or object value in setter
  • string path can be combined with object value to specify further nested path and new value at final path
  • cannot set objects as values at the moment (may reconsider this in future if sufficient use case found)

Usage Examples

For Getter

  • Gets nested value from string paths
    var obj = { a: { b: { c: 'value' } } };
    assert(get(obj, 'a.b.c') === 'value');
  • Does not allow 3rd argument (value)
    var obj = { a: { b: { c: 'value' } } };
    try {
      get(obj, 'a.b.c', 'val');
    } catch(e) {
      done();
    }
  • Does not allow object paths
    var obj = { a: { b: { c: 'value', d: 'haha' } } };
    try {
      get(obj, { a: { b: 'c' } });
    } catch (e) {
      done();
    }

For Setter

  • Sets value for nested object by string path
    var obj = { a: { b: { c: 'value' } } };
    set(obj, 'a.b.c', 'lol');
    assert(obj.a.b.c === 'lol');
  • Requires value to be set
    var obj = { a: { b: { c: 'value' } } };
    try {
      set(obj, 'a.b.c', undefined);
    } catch(e) {
      done();
    }
  • Allows undefined value with object paths
    var obj = { a: { b: { c: 'value' } } };
    set(obj, { a: { b: { c: 'lol' } } });
    assert(obj.a.b.c === 'lol');
  • Does not allow defined value for object paths
    var obj = { a: { b: { c: 'value' } } };
    try {
      set(obj, { a: { b: 'c' } }, 'haha');
    } catch(e) {
      done();
    }
  • Can set primitives as value in place of objects
    var obj = { a: { b: { c: 'value' } } };
    set(obj, { a: { b: 'lol' } });
    assert(obj.a.b === 'lol');
  • Sets nested value via object path
    var obj = { a: { b: { c: 'value', d: 'haha' } } };
    set(obj, { a: { b: { c: 'lol' } } });
    assert(obj.a.b.c === 'lol');
    assert(obj.a.b.d === 'haha');
  • Maintains object references, only changing value
    var obj = { a: { b: { c: 'value', d: 'haha' } } };
    var b = obj.a.b;
    var path = { a: { b: { c: 'lol' } } };
    set(obj, path);
    assert(obj.a.b === b);
  • Sets nested value via value object
    var obj = { a: { b: { c: 'value', d: 'haha' } } };
    set(obj, 'a', { b: { c: 'lol' } });
    assert(obj.a.b.c === 'lol');
    assert(obj.a.b.d === 'haha');
  • Allows mixing object paths and object values
    var obj = { a: { b: { c: 'value', d: 'haha' } } };
    set(obj, 'a.b', { c: 'lol' });
    assert(obj.a.b.c === 'lol');
    assert(obj.a.b.d === 'haha');