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

change-object

v1.0.2

Published

Update objects using a declarative API. Inline imperative updates also available.

Downloads

8

Readme

change-object

License: MIT Test Size Issues

change-object provides an intuitive API to declaratively (or imperatively) update JavaScript objects, when you may not know their internal structure ahead of time.

Installation

$ npm install change-object

Usage

const changeObject = require("change-object")

const myData = {
  friends: [{ name: "Jackson" }]
}

changeObject(myData, {
  set: {
    likesJS: true,
    "friends.0.age": 19
  },
  insert: {
    friends: [{ name: "Lance" }, { name: "Stephanie" }]
  }
})

Which results in the myData variable being modified:

{
  likesJS: true,
  friends: [
    { name: "Jackson", age: 19 },
    { name: "Lance" },
    { name: "Stephanie" }
  ]
}

Operations and Nested Access

Nested Access Syntax

If you'd like to modify a property nested within another object, this is simple. You use change-object's special nested access syntax.

topLevelProperty->child property->0->color

As you can see, the access operator/delimiter is ->, and your property names can have any set of characters, so long as they don't contain the -> operator.

To solidify this example, the above string would be targeting the highlighted color property:

{
  topLevelProperty: {
    "child property": [
      { color: "blue" },	// this color property
      { color: "red" },
      { color: "green" }
    ]
  }
}

Note: If there happened to already exist a property named topLevelProperty->child property->0->color on the object, this would override the nested access system and would set the value of that property instead.

Set Operation

The set operation sets the value of the given key (even nested keys) to the value provided.

Example using simple and nested access syntax:

changeObject(myObject, {
  set: {
    myTopLevelBooleanBroperty: true,
    "topLevelObject->favoriteFood": "noodles"
  }
})

Unset Operation

The unset operation works exactly like the set operation, except that it removes properties from objects.

Example using nested access syntax:

changeObject(myObject, {
  unset: {
    "topLevelObject->favoriteFood": "noodles"
  }
})

Insert Operation

The insert operation adds elements to arrays.

Example using simple and nested syntax:

const myObject = {
  onMyPhone: ["YouTube", "Facebook"],
  inMyBag: {
    electronics: ["Laptop"]
  }
}

Insert some items:

changeObject(myObject, {
  insert: {
   onMyPhone: ["Wikipedia"],
   "inMyBag->electronics": ["External Drive"]
  }
})

This results in a modification of myObject:

{
  onMyPhone: ["YouTube", "Wikipedia"],
  inMyBag: {
    electronics: ["Laptop", "External Drive"]
  }
}

Remove Operation

The remove operation conditionally removes properties or elements from Objects or Arrays respectively. You provide a function, which returns true when a property/element should be removed.

Example using simple and nested syntax:

const myObject = {
  onMyPhone: ["YouTube", "Facebook"],
  inMyBag: {
   junk: ["Broken Cable", "New Cable"]
  }
}

Let's remove some items:

changeObject(myObject, {
  remove: {
   onMyPhone: (key, value) => (value === "Facebook"),
   "inMyBag->junk": (index, value) => (value.split("Broken").length != 0)
  }
})

Modify Operation

This allows you to apply arbitrary modifications to parts of your object. You specify the part of your object you'd like to modify, and a function which will modify the object.

Let's see an example.

const plane = {
  passengers: [
    { name: "Jackson", annoying: false, money: 0 }, 
    { name: "Peter",   annnoying: true, money: 0 }
  ]
}

Let's apply a mutation:

changeObject(plane, {
  modify: {
   passengers: (passengers, parent) => {
     passengers.forEach(p => {
       if (p.annoying) { p.money -= 20 }
       else { p.money += 20 }
     })
   }
  }
})

We find the object is now modified:

{
  passengers: [
    { name: "Jackson", annoying: false, money: 20  }, 
    { name: "Peter",   annnoying: true, money: -20 }
  ]
}

Future features

  • Anywhere you pass a function, you should also be able to pass a function string.
  • The functions (or function strings) you provide should be able to be async functions, which will be automatically detected, and changeObject( ... ) will return a promise.

License

Copyright (c) 2019 Will Brickner

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.