fast-undo
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

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>>;

Package Sidebar

Install

npm i fast-undo

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

54.7 kB

Total Files

23

Last publish

Collaborators

  • gamble