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

updatex

v0.1.0

Published

Immutable and predictive to update json data

Downloads

1

Readme

Immutable and predictive to update json data

Example

import updatex from 'updatex';

const obj = { a: { b: { c: {d: 1} } } };

const obj2 = updatex(obj, (newObj) => {
  const b = newObj.select('a.b');
  b.name = 'tj';

  b.c.d = 2; // will throw in development enviroment, as b.c is frozen
});

Why?

  1. There are many immutable libraries like immutable, seamless-immutable. But they may either change your fundamental data structure, or need you use extraneous methods to mutate the data. For a simple usage, spread syntax(...) is efficient, then you can use normal mutation ways like obj.a = x and arr.push().
  2. But it's a big pain to write in a deep structure, and it provides a large surface area for bugs. So we need freeze the original value to prevent accidental changes on it, especially in development environment.

These two reasons above bring updatex.

Features

  • Internally use spread syntax(...) to copy value, then you can directy mutate the value
  • Auto freeze the original value in development environment, but off in production environment
  • Predictive mutation by declare the mutation-probable path ahead(obj.select('a.b.c'))
  • Auto check and warn over-select at the end
  • No more repeated data clone, you are forced to do mutations in a batch mode
  • No need to reassign the new value(obj = obj.set(k, v)) and return it at last

Installation

yarn add updatex

or if your'd like to use npm:

npm install updatex -S

API

updatex(input: object|array, updater: (obj)=>void)

Do mutation on input data. Only works with plain object and array, others will return the input directly. All updates should be done in updater function.

select(path: string|array)

Declare the path you will mutate on, all nodes on the path will be cloned. Then you can use assignment syntax(=) and other mutation methods to manipulate the nodes.

updatex(state, (newState) => {
  const user = newState.select('users.0');
  user.name = 'Tian';

  newState.users.push({ name: 'Jian' });
});

config(name, value)

Set the config. Currently only one config: freeze.

  • freeze: Whether using Object.freeze to make the input and output readonly.
    • Default true in development environment, false in production enviroment(process.env.NODE_ENV==='production')
updatex.config('freeze', false);

Contributing

Checkout the CONTRIBUTING.md if you want to help

License

Licensed under MIT

Copyright (c) 2017 Tian Jian