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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@letumfalx/get-set-prop

v0.1.1

Published

A function used to get or set value on a deep nested object using dot notation.

Downloads

4

Readme

Get-Set Prop

A function used to get or set value on a deep nested object using dot notation.

Installation

npm install @letumfalx/get-set-prop

Get Function

The get function split the key, then traverse through the given object to get the target value. This will check if the prop key is owned by object instead of checking if the value is undefined. If the prop key is not found, this function will return the default value given.

Usage

getProp(object, key, defaultValue = null, options = {});

Basic Usage

var getProp = require('@letumfalx/get-set-prop').getProp;

var referenceObject = {
  key1: {
    nestedOne: 1,
    nested: [
      'array_value_1'
    ]
  },
  key2: null,
  key3: undefined
};

getProp(referenceObject, 'key1.nestedOne', null); // returns 1
getProp(referenceObject, 'key1.nested.0', null); // returns 'array_value_1'
getProp(referenceObject, 'key1.nested', null); // returns [ 'array_value_1' ]
getProp(referenceObject, 'key2', 'not null'); // returns null
getProp(referenceObject, 'key3', 'not undefined'); // returns undefined
getProp(referenceObject, 'key4', null); // returns null
getProp(referenceObject, 'key1.nested.1', null); // returns null

Options

There are additional options you can pass as the fourth/last parameter:

{
  separator: String = '.',
  enumerableOnly: Boolean = true
}
separator

This is the string to use for splitting the key. If you are using the default . on any of your key, you set other character that is most likely not used as character for the key of your object like |.

var getProp = require('@letumfalx/get-set-prop').getProp;

var referenceObject = {
  'a.1': {
    'b.1': 2
  }
};

getProp(referenceObject, 'a.1|b.1', null, { separator: '|' }); // returns 2
getProp(referenceObject, 'a.1|b.2', null, { separator: '|' }); // returns null
enumerableOnly

Set this to true if you want to search only for enumerable values (visible in foreach), otherwise set this to false. This defaults to true. This is usually used for getting the length of an array as the length is not enumerable but is owned by the array.

var getProp = require('../../src').getProp;

var referenceObject = {
  a: [
    1,
    2
  ],
  b: 5
};

Object.defineProperty(referenceObject, 'b', { enumerable: false });

getProp(referenceObject, 'a.length', null, { enumerableOnly: false }); // returns 2
getProp(referenceObject, 'a.length', null, { enumerableOnly: true }); // returns null
getProp(referenceObject, 'b', null, { enumerableOnly: false }); // returns 5
getProp(referenceObject, 'b', null, { enumerableOnly: true }); // returns null

Set Function

The set function split the key, then traverse through the given value to the target property key. If it cannot go deeper due to encountering non-object value, it will throw a TypeError. If ever it encounters a undefined, null, or non-existing key, it will set its value to an empty object ( {} ) so we can continue to traverse the object up to the last property key. If traversing is successful, will set the last property key to the value given.

Usage

setProp(object, key, value, options = {});
var setProp = require('../../src').setProp;

var referenceObject = {
  nested: {
    key1: 999,
    key2: null,
    key3: undefined,
    key4: [
      {
        arr1: 888
      }
    ],
    key5: {
      value: {
        valueInside: 777
      }
    }
  }
};

setProp(referenceObject, 'nested.key1', 1); // mod1
setProp(referenceObject, 'nested.key2.value2', 2); // mod2
setProp(referenceObject, 'nested.key3.value.3', 3); // mod3
setProp(referenceObject, 'nested.key4.0.arr1', 'arr1'); // mod4
setProp(referenceObject, 'nested.key5', null); // mod5
setProp(referenceObject, 'nested.key4.999', {
  a: {
    b: 2
  }
}); // mod6
setProp(referenceObject, 'nested.not_exists.value.1', 'not_exists'); // mod7

console.log(referenceObject);

Above usage will output:

{
  nested: {
    key1: 1, // change by mod1
    key2: { value2: 2 }, // change by mod2
    key3: { value: { '3': 3 } }, // changed by mod3
    key4: [ { arr1: 'arr1' }, <998 empty items>, { a: { b: 2 } } ], // changed by mod4
    key5: null, // changed by mod5
    not_exists: { value: { '1': 'not_exists' } } // change by mod6
  }
}

Options

There are additional options you can pass as the fourth/last parameter:

{
  separator: String = '.'
}
separator

This is the string to use for splitting the key. If you are using the default . on any of your key, you set other character that is most likely not used as character for the key of your object like |.

var setProp = require('@letumfalx/get-set-prop').setProp;

var referenceObject = {
  'nested.1': {
    'nested.2': {
      'nested.3': 1
    }
  }
};

setProp(referenceObject, 'nested.1|nested.2|nested.3', 2, { separator: '|' });

console.log(referenceObject);