npm.io
0.2.2 • Published 7 years ago

fast-undo

Licence
MIT
Version
0.2.2
Deps
0
Size
53 kB
Vulns
0
Weekly
0

fast-undo

Efficient data structure for handling undo states.

Build Status

// create an empty undo history
const history = undo.history()

// insert a few values
history.insert('cat')
history.insert('dog')
history.insert('rabbit')

// undo
history.undo() // cat <| dog |> rabbit

// redo
history.redo() // dog <| rabbit |> EMPTY

// export
history.toJSON() // '{"past":["dog","cat"],"present":"rabbit","future":[]}'

Install as an NPM module:

$ npm install fast-undo

Works as a higher-order reducer with Redux, or similiar:

import { combineReducers } from 'redux';
import { withHistory } from 'fast-undo';

combineReducers({
  undoableReducer: withHistory(myReducer)
});

See the tests.js file for some more inspiration.

API

init

function init<T>(a: T): History<T>;
undo
function undo<T>(a: History<T>): History<T>;
redo
function redo<T>(a: History<T>): History<T>;
insert
function insert<T>(a: History<T>, b: T): History<T>;
prune
function prune<T>(history: History<T>, size?: number): History<T>;
serialize
function serialize<T>(a: History<T>): JSONHistory<T>;
deserialize
function deserialize<T>(a: JSONHistory<T>): History<T>;
history
function history<T>(a: T): {
    undo: () => History<T>;
    redo: () => History<T>;
    insert: (a: T) => History<T>;
    get: () => History<T>;
    toJSON: () => string;
};
withHistory
function withHistory<T>(a: Reducer<T>): Reducer<History<T>>;