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

dendriform-immer-patch-optimiser

v2.1.3

Published

Immer patch optimisation to add "move" operations.

Downloads

408,335

Readme

dendriform-immer-patch-optimiser

Dendriform uses Immer. Immer is wonderful, especially its ability to produce patches for implementing concurrent editing and undo & redo functionality.

But Dendriform needs to track how array elements are moved around. Luckily the RFC6902 JSON patch standard that Immer's patches are similar to describes "move" operations. Immer does not produce "move" patches, so this plugin post-processes Immer patches to infer array element movement and produce "move" patches.

When using Immer alone, certain types of changes can lead to a large number of patches because Immer's design optimises for good performance at generation time. A nice side effect of this library is that describing movements naturally reduces the number of patches.

Please note that while this library does some immer patch optimisations that could be used in any project, the main goal of this library is to solve problems for Dendriform. As such, it's API may be a little volatile, and it won't necessarily aim to please the use cases of the general public. You are welcome to use the library as is, copy the source code for your own uses, or even republish the code as a new library if you'd like to take on the responsibility of maintaining a more public-serving variant of it.

Current limitations

  • Is not yet guaranteed to work with arrays containing multiple identical (strictly-equal) elements

Examples

Array optimisation

import {produceWithPatches} from 'immer';
import {optimise} from 'dendriform-immer-patch-optimiser';

// enablePatches() is already called by dendriform-immer-patch-optimiser

const base = {foo: ['a','b','c']};
const [result, recordedPatches] = produceWithPatches(base, draft => {
    draft.foo.unshift('d');
});

// result:
// ['d','a','b','c']

// recordedPatches:
// {op: 'replace', path: ['foo', 0], value: 'd'},
// {op: 'replace', path: ['foo', 1], value: 'a'},
// {op: 'replace', path: ['foo', 2], value: 'b'},
// {op: 'add', path: ['foo', 3], value: 'c'}

const optimisedPatches = optimise(base, recordedPatches);

// optimisedPatches:
// {op: 'add', path: ['foo', 0], value: 'd'}

Move patches

import {produceWithPatches} from 'immer';
import {optimise, applyPatches} from 'dendriform-immer-patch-optimiser';

// enablePatches() is already called by dendriform-immer-patch-optimiser

const base = {foo: ['a','b','c']};
const [result, recordedPatches] = produceWithPatches(base, draft => {
    draft.foo.shift();
});

// result:
// ['b','c']

// recordedPatches:
// {op: 'replace', path: ['foo', 0], value: 'b'},
// {op: 'replace', path: ['foo', 1], value: 'c'},
// {op: 'replace', path: ['foo', 'length'], value: 2}

const optimisedPatches = optimise(base, recordedPatches);

// optimisedPatches:
// {op: 'move', from: ['foo', 1], path: ['foo', 0]},
// {op: 'move', from: ['foo', 2], path: ['foo', 1]},
// {op: 'replace', path: ['foo', 'length'], value: 2}

// Immer doesn't understand "move" patches, so use applyPatches() exported from dendriform-immer-patch-optimiser

const result2 = applyPatches(base, optimisedPatches);

// result2 is the same as result

Adding, moving and removing at once

import {produceWithPatches} from 'immer';
import {optimise, applyPatches} from 'dendriform-immer-patch-optimiser';

// enablePatches() is already called by dendriform-immer-patch-optimiser

const base = ['a','b','X','Y','c','d','e','Z'];
const [result, recordedPatches] = produceWithPatches(base, draft => {
    draft.splice(2,2);
    draft.pop();
    draft.reverse();
    draft.push('C');
    draft.splice(3, 0, 'B');
    draft.unshift('A');
});

// result:
// ['A','e','d','c','B','b','a','C']

// recordedPatches:
// {op: 'replace', path: [0], value: 'A'},
// {op: 'replace', path: [1], value: 'e'},
// {op: 'replace', path: [2], value: 'd'},
// {op: 'replace', path: [3], value: 'c'},
// {op: 'replace', path: [4], value: 'B'},
// {op: 'replace', path: [5], value: 'b'},
// {op: 'replace', path: [6], value: 'a'},
// {op: 'replace', path: [7], value: 'C'}

const optimisedPatches = optimise(base, recordedPatches);

// optimisedPatches:
// {op: 'move', from: [6], path: [0]},
// {op: 'move', from: [6], path: [1]},
// {op: 'move', from: [6], path: [2]},
// {op: 'move', from: [4], path: [3]},
// {op: 'replace', path: ['length'], value: 5},
// {op: 'add', path: [0], value: 'A'},
// {op: 'add', path: [4], value: 'B'},
// {op: 'add', path: [7], value: 'C'}

// Immer doesn't understand "move" patches, so use applyPatches() exported from dendriform-immer-patch-optimiser

const result2 = applyPatches(base, optimisedPatches);

// result2 is the same as result