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 🙏

© 2026 – Pkg Stats / Ryan Hefner

circular-history

v1.0.8

Published

Data structure with a fixed number of items of a specific type. Supports adding elements, moving backward and forward with wrapping. Suitable for undo/redo implementations such as history management.

Readme

Circular History

Data structure with a fixed number of items of a specific type. Supports adding elements, moving backward and forward with wrapping. Suitable for undo/redo implementations such as history management.

Usage

1. Create an instance of CircularHistory with a specified capacity and data type.

var history = new CircularHistory(5, "string");

2. Commit new items to the history.

history.commit("First");
history.commit("Second");

Keep in mind that after moving backward, committing a new item will overwrite the items ahead of the current index. It was designed this way to facilitate undo-redo functionality by discarding the "redo" history when a new action is taken.

When the capacity is reached, the oldest items will be overwritten in a circular manner.

3. Get the current item.

var currentItem = history.current();

Returns either the current item or CircularHistory.FLAGS.empty if current index is -1 which means there are no committed items yet or you went backwards beyond the first committed item (basically an empty state).

4. Move backward and forward in the history.

history.moveBackward();
history.moveForward();

Moving backward and forward will adjust the current index accordingly and allow you to navigate within specific range which is determined by the number of committed items before navigation. If the number of committed items exceeds the capacity, the range will be limited to the capacity.

5. Clear the history.

history.clear();

6. Get the history array.

var historyArray = history.dump();

This will return an array of all committed items in the history. If capacity has not been reached, the array will contain items with undefined values for uncommitted slots.

If you want to get only the committed items, you can pass true as an argument.

var committedItems = history.dump(true);

7. Get the current index.

var currentIndex = history.getCurrentIndex();

8. Determine if start/end has been reached

var isAtStart = history.isStartReached();
var isAtEnd = history.isEndReached();

Running tests

  1. pnpm install

  2. pnpm test

Usage example

Imagine that you have a drawing application. You have a layer where you can draw shapes and you can create a snapshot of the layer's state to keep track of changes.

var history = new CircularHistory(50, "string");

function commitHistorySnapshot() {
  const snapshot = drawLayer.toJSON();
  if (!snapshot) return;
  history.commit(snapshot);
}

function restoreHistorySnapshot(snapshot) {
  if (!snapshot) return;

  if (snapshot === CircularHistory.FLAGS.empty) {
    drawLayer.clear();
    drawLayer.redraw();
    return;
  }

  drawLayer.unmount();

  const restoredLayer = new Layer(snapshot);
  drawLayer = restoredLayer;

  restoredLayer.redraw();
}

function undo() {
  history.moveBackward();
  restoreHistorySnapshot(history.current());
}

function redo() {
  history.moveForward();
  restoreHistorySnapshot(history.current());
}