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-assigner

v1.3.1

Published

A lightweight (no dependencies) tool to assign deeply nested properties in JS Objects (incl. Arrays)

Downloads

91,565

Readme

Object Property Assigner

Companion package to object-property-extractor

A lightweight (no dependencies) tool to assign deeply nested values in JS Objects (or Arrays).

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 assign inner object properties via dot notation:

data.user.name.last = "Hutt" 

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

const key = "user" 
data[key] = { name: "Boba Fett" } // data.user = { name: "Boba Fett" }

However, you can't do this:

const key = "user.name"
data[key] = "Boba Fett"

This tool allows assignation of deep properties using a single "property path" string.

Installation

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

Usage

Note: function returns a shallow copy of the input, the original object is unmodified

assign( dataObject, propertyString, newValue, { options } ) (See below for options details)

import assign from "object-property-assigner"

// Using the data object above
data = assign(data, "user.name.first", "Boba") // data.user.name.first = "Boba"

data = assign(data, "user.weapons[1].description", "Pew Pew") // data.user.weapons[1].description = "Pew Pew"

Array handling

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

For example:

data = assign(data, "user.weapons.name", "Laser Gun")
// sets *all* user.weapons.name to "Laser Gun"

Options

The (optional) options object can contain any or all of the following parameters:

  • remove -- if true, the property will be deleted rather than assigned (in which case the newValue parameter is ignored)
    assign(data, "user.name.first", null, {remove:true}) // delete user.name.first
  • createNew -- (default: true). If a property doesn't exist, it will be created, so set this to false if this behaviour is not desired
    assign(data, "user.kind", "Mandalorian", {createNew: true}) // data.user.kind = "Mandalorian"
    Note: for arrays, if an index is specified higher than the current array length, a new item will be created as the next array item, regardless of how much higher the index is. e.g.
    assign( {myArray: [1, 2, 3]}, "myArray[10]", "New Value")
    // --> {myArray: [1, 2, 3, "New Value"]}
  • insert (number) -- (only for arrays). If this property is set, then the new value will be inserted into the array at this position and all other elements shuffled along. e.g.
    assign( {myArray: [1, 2, 3]}, "myArray[1]", "New Value")
    // --> {myArray: [1, "New Value", 2, 3]}
  • insertBefore (string) -- (only for objects). If this property is set, then the new value will be inserted before the key specified in insertBefore. e.g.
    assign( {one: 1, two: 2, three: 3}, "myNewValue", 69, {insertBefore: "three"} )
    // --> {one: 1, two: 2, myNewValue: 69, three: 3}
    Please note: Javascript does not guarantee object property order, so this option may yield unpredictable results, especially if using non-standard object keys (e.g. numbers). See here for an explanation of how key ordering is handled.
  • insertAfter (string) -- (only for objects). Same as insertBefore, but inserts after a particular object key. Same caveats apply.
  • noError -- (default: false). If a property doesn't exist and createNew == false, then an error will be thrown. If you'd rather it just silently ignored the missing property, then set this parameter to true. Note that this is only for errors due to invalid property strings -- other errors might still be thrown.

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-assigner