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

adab-imut-updater

v1.0.0

Published

A utility class for immutably updating nested objects in JavaScript.

Downloads

3

Readme

ImmutableUpdater Documentation

The ImmutableUpdater class is a utility for performing immutable updates on nested objects. It allows for setting, getting, deleting, and merging values at specified paths within an object, ensuring that the original object remains unchanged.

Class: ImmutableUpdater

Constructor

constructor(obj)

Creates a new ImmutableUpdater instance.

  • Parameters:
    • obj (Object): The initial object to operate on.

Static Methods

static isObject(value)

Checks if a given value is an object (excluding null).

  • Parameters:
    • value (*): The value to check.
  • Returns:
    • boolean: True if the value is an object, false otherwise.

static cloneDeep(value)

Recursively clones an object or array.

  • Parameters:
    • value (*): The value to clone.
  • Returns:
    • *: The cloned value.

static parsePath(path)

Parses a dot-separated path string into an array of keys.

  • Parameters:
    • path (string): The path string to parse.
  • Returns:
    • Array: An array of keys parsed from the path.
  • Throws:
    • Error: If the path is not a non-empty string.

Instance Methods

set(path, value)

Sets a value at a specified path in the object and returns a new ImmutableUpdater instance with the updated object.

  • Parameters:
    • path (string): The path where to set the value.
    • value (*): The value to set at the specified path.
  • Returns:
    • ImmutableUpdater: A new ImmutableUpdater instance with the updated object.
  • Throws:
    • Error: If the path is not a non-empty string or if the path does not exist in the object.

get(path = '')

Retrieves the value at a specified path in the object.

  • Parameters:
    • path (string, optional): The path from where to retrieve the value.
  • Returns:
    • *: The value at the specified path, or undefined if the path does not exist.
  • Throws:
    • Error: If the path is not a non-empty string.

delete(path)

Deletes the property at a specified path in the object and returns a new ImmutableUpdater instance with the updated object.

  • Parameters:
    • path (string): The path of the property to delete.
  • Returns:
    • ImmutableUpdater: A new ImmutableUpdater instance with the updated object.
  • Throws:
    • Error: If the path is not a non-empty string or if the path does not exist in the object.

merge(path, value)

Merges an object into the property at a specified path in the object and returns a new ImmutableUpdater instance with the updated object.

  • Parameters:
    • path (string): The path where to merge the object.
    • value (Object): The object to merge at the specified path.
  • Returns:
    • ImmutableUpdater: A new ImmutableUpdater instance with the updated object.
  • Throws:
    • Error: If the path is not a non-empty string, if the path does not exist in the object, or if value is not an object.

How to Use

Initialization

To use the ImmutableUpdater, create an instance with the initial object:

const initialObject = { a: { b: { c: 42 } } };
const updater = new ImmutableUpdater(initialObject);

Setting a Value

To set a value at a specified path:

const updated = updater.set('a.b.c', 100);
console.log(updated.obj); // { a: { b: { c: 100 } } }

Getting a Value

To get a value from a specified path:

const value = updater.get('a.b.c');
console.log(value); // 42

Deleting a Property

To delete a property at a specified path:

const updated = updater.delete('a.b.c');
console.log(updated.obj); // { a: { b: {} } }

Merging an Object

To merge an object into the property at a specified path:

const updated = updater.merge('a.b', { d: 50 });
console.log(updated.obj); // { a: { b: { c: 42, d: 50 } } }

Error Handling

The methods in ImmutableUpdater will throw errors under certain conditions:

  • If the path is not a non-empty string.
  • If the specified path does not exist in the object.
  • If the value to merge is not an object.

Make sure to handle these errors appropriately in your application.