Giter VIP home page Giter VIP logo

immer-undo's Introduction

immer-undo v0.1.0

Revert any number of immer patches.

 

revertPatch(base, patch)

Mutate the given object/array by reversing the given patch.

Errors are thrown when patches are reverted in the wrong order.

import { Patch } from 'immer'
import { revertPatch } from 'immer-undo'

// Revert a single patch
let state = revertPatch(
  { a: 1 },
  {
    op: 'remove',
    path: ['b'],
    origValue: 1,
  }
)
state.b == 1 // => true

// Revert all patches using `reduceRight`
let history: Patch[] = [
  { op: 'add', path: ['a'], value: 1 },
  { op: 'replace', path: ['a'], value: 2, origValue: 1 },
]
state = history.reduceRight(revertPatch, { a: 2 })
'a' in state // => false

 

createPatchHistory(history)

Creates an undo/redo function pair for managing patch history.

import { createPatchHistory } from 'immer-undo'

let { history, undo, redo } = createPatchHistory()
history.push({ op: 'add', path: ['a'], value: 1 })

let state = { a: 1 }
state = undo(state)
'a' in state // => false

state = redo(state)
state.a == 1 // => true

Tip: Pass a number to undo/redo to undo/redo many patches at once.

 

generateInversePatch(patch)

Generate a new patch that's the exact opposite of the given patch.

import { generateInversePatch } from 'immer-undo'

let patch = generateInversePatch({ op: 'add', path: ['a'], value: 1 })
patch.op == 'remove'
patch.path[0] == 'a'
patch.origValue == 1

immer-undo's People

Contributors

aleclarson avatar renovate-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

immer-undo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.