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

path-next

v1.1.7

Published

provides JavaScript functions for getting the next version of an object after making a change at a given path

Downloads

81

Readme

path-next

This is a set of functions that take an object and a path to a property in the object and return a new object with a specific kind of change made at that path.

The original object is not modified. Modified parts of the object tree are cloned. Other parts are shared between old and new objects.

While this library is not specific to React, it is particularly useful in React because there are many cases where the functions provided here can simplify the code.

For example, suppose setState is being used to increment the score in a game that is at the state path user.game.score. Without using this library one might write code like the following:

this.setState(state => {
  const {user} = state;
  const {game} = user;
  const {score} = game;
  return {
    ...state,
    user: {
      ...user,
      game: {
        ...game,
        score: score + 1
      }
    }
  };
});

Using this library the code above can be simplified to the following:

this.setState(state =>
  transformPath(state, 'user.game.score', score => score + 1)
);

The functions provided are described below in alphabetical order.

deepFreeze

This freezes a given object and all objects inside it. Attempts to modify any properties in this object, no matter how deeply nested, will result in an error in strict mode or be silently ignored in non-strict mode. This is useful to catch attempts to modify objects that should be immutable.

deepFreeze(person);

deletePath

This deletes the property at a given path within an object.

deletePath(person, 'address.city');

filterPath

This removes elements from an array found at a given path within an object.

// Remove the color green if it exists.
const newPerson = filterPath(
  person,
  'favorites.colors',
  color => color !== 'green'
);

getPath

This retrieves the value of a property at a given path.

const colors = getPath(person, 'favorites.colors');

mapPath

This modifies the values in an array at a given path.

const newPerson = mapPath(person, 'favorite.colors', color =>
  color.toUpperCase()
);

pushPath

This appends values to an array at a given path.

// Add the colors yellow and orange the existing array of colors.
const newPerson = mapPath(person, 'favorite.colors', 'yellow', 'orange');

setPath

This changes the value at a given path.

const newPerson = setPath(person, 'address.city', 'New York');

transformPath

This changes the value at a given path based on its current value.

const newPerson = transformPath(person, 'age', age => age + 1);

Path Concerns

If the layout of objects is expected to change over time and the need to revisit hard-coded paths in these function calls is a concern, consider using constants for the paths so that only those values need to be changed.

For example, create the file path-constants.js containing:

const export GAME_HIGH_SCORE = 'game.statistics.highScore';
const export USER_CITY = 'user.address.city';

Then in files that use these paths, do something like this:

import {GAME_HIGH_SCORE, USER_CITY} from './path-constants';
setPath(user, USER_CITY, 'St. Louis');
transformPath(gameData, GAME_HIGH_SCORE, score => score + 1);

With this approach, if the paths change it is only necessary to update these constants.