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

@biswaviraj/time-travel

v2.0.5

Published

Simple and lightweight undo redo package

Downloads

15

Readme

Time Travel

It is a simple TypeScript library for managing undo/redo functionality with a time-travel mechanism. It's designed to keep track of changes in state, allowing you to navigate back and forth through history with ease.

npm version Minified + Gzipped size Minified size

Installation

npm install @biswaviraj/time-travel

Features

  • Undo/Redo Functionality: Easily navigate through past states and reapply changes.
  • History Management: Manage a configurable history limit to control memory usage.
  • Simple API: Intuitive methods for interacting with the history and state.

Usage

Here's how you can use the time travel package in your TypeScript project:

Basic Example

import { timeTravel } from "@biswaviraj/time-travel";

// Create a time travel instance with an initial value
const history = timeTravel<number>(0, { limit: 5 });

// Add new values
history.add(1);
history.add(2);
history.add(3);

// Get the current value
console.log(history.get()); // Output: 3

// Undo the last change
history.undo();
console.log(history.get()); // Output: 2

// Redo the undone change
history.redo();
console.log(history.get()); // Output: 3

Example with Arrays

import { timeTravel } from "@biswaviraj/time-travel";

// Create a time travel instance with an initial array value
const history = timeTravel<number>([1, 2, 3], { limit: 5 });

// Add new array values
history.add([4, 5]);
history.add(6); // can add single values as well
history.add([7]);

// Get the current value
console.log(history.get()); // Output: 7

// Undo the last change
history.undo();
console.log(history.get()); // Output: 6

// Redo the undone change
history.redo();
console.log(history.get()); // Output: 7

Example with Array of Objects

import { timeTravel } from "@biswaviraj/time-travel";

// Define a type for our objects
type Item = { id: number; name: string };

// Create a time travel instance with an initial array of objects
const history = timeTravel<Item>(
  [
    { id: 1, name: "Item 1" },
    { id: 2, name: "Item 2" },
  ],
  { limit: 5 }
);

// Add new array of objects
history.add({ id: 3, name: "Item 3" });

history.add([
  { id: 4, name: "Item 4" },
  { id: 5, name: "Item 5" },
]);

// Get the current state
console.log(history.get());
// Output: { id: 5, name: 'Item 5' }

// Undo the last change
history.undo();
console.log(history.get());
// Output: { id: 4, name: 'Item 4' }

// Redo the undone change
history.redo();
console.log(history.get());
// Output: { id: 5, name: 'Item 5' }

API

timeTravel<T>(initialValue: T | T[], options?: { limit?: number }) Creates a time travel instance.

  • initialValue: The initial state, which can be a single value or an array of values.
  • options: Optional configuration object.
    • limit: The maximum number of past and future states to keep. Defaults to 10.

The time travel library provides the following methods for managing history:

  • add(value: T): void: Add a new value to the history.

  • get(): T: Get the current value from the history.

  • undo(): void: Undo the last change and move back in history.

  • redo(): void: Redo the last undone change and move forward in history.

TypeScript Support

The library is written in TypeScript and provides type definitions out of the box. This ensures you get proper type checking and autocompletion when using the library in your TypeScript projects.

Contributing

Feel free to contribute to the project by submitting issues or pull requests. Please follow the standard GitHub workflow for contributions.

For more information or to report issues, please visit the GitHub repository.