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

@caelus-dts/circular-buffer

v0.1.0

Published

A TypeScript implementation of a circular buffer, a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

Readme

CircularBuffer

A TypeScript implementation of a circular buffer, a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

  • Circular Buffer Property: The buffer does not automatically wrap around when it reaches its capacity. However, if it is configured with overwrite: true, it reuses empty spaces at the start as older data is overwritten.

Installation

  • using npm
npm install @caelus-dts/circular-buffer
  • using yarn
yarn add @caelus-dts/circular-buffer
  • using pnpm
pnpm add @caelus-dts/circular-buffer

Usage

import CircularBuffer from '@caelus-dts/circular-buffer';

// Create a new CircularBuffer with a capacity of 5
const buffer = new CircularBuffer<number>(5);

// Write values to the buffer
buffer.write(1);
buffer.write(2);
buffer.writeAll([3, 4, 5]);

// Read values from the buffer
console.log(buffer.read()); // Output: 1
console.log(buffer.read()); // Output: 2

// Check buffer status
console.log(buffer.isEmpty);   // Output: false
console.log(buffer.isFull);    // Output: true
console.log(buffer.capacity);  // Output: 5
console.log(buffer.size);     // Output: 5

// Convert buffer to array
const array = buffer.toArray(); // array = [1, 2, 3, 4, 5]. buffer is not modified
const array2 = buffer.toArray(true); // array2 = [1, 2, 3, 4, 5], buffer is cleared
console.log(buffer.isEmpty); // Output: true

// Or Clear the buffer using .clear()
buffer.clear();
console.log(buffer.isEmpty); // Output: true



// Create a CircularBuffer from an iterable
const bufferFromArray = CircularBuffer.from([6, 7, 8, 9, 10], 3, { overwrite: true });
console.log(bufferFromArray.toArray()) // Output: [8, 9, 10]


// Dumping elements from the buffer
const dumped = bufferFromArray.dump();
console.log(dumped); // 8
console.log(bufferFromArray.toArray()) // Output: [9, 10, undefined]

// Resizing the buffer
bufferFromArray.resize(8);
console.log(bufferFromArray.capacity); // 8

Configuration

The CircularBuffer constructor accepts an optional configuration object (CBConfig) with the following properties:

  • overwrite (default: false): If true, when the buffer is full, writing new values will overwrite the oldest values. If false, writing new values when the buffer is full has no effect, and a warning is printed to the console if warnOnFull is true.

  • warnOnFull (default: true): If true, a warning message will be printed to the console when attempting to write to a full buffer when the overwrite option is set to false. This has no effect if overwrite is true.

  • notifyOnOverwrite (default: false): If true, and if the overwrite option is also false , a warning will be logged to the console whenever a value is overwritten.

Error Handling

If you attempt to resize the buffer to a smaller capacity without setting the force parameter to true in the resize method, a BufferNewCapacityIsSmallerError (BNCIS) will be thrown.

API Reference

Constructor

new CircularBuffer<T>(capacity: number, config?: CBConfig);
  • capacity: The maximum number of elements the buffer can hold.
  • config: Optional configuration object (see Configuration section above).

Methods

  • capacity: Returns the buffer's capacity.
  • clear(): Clears the buffer.
  • dump(): Removes and returns the next value from the buffer.
  • from<O>(values: Iterable<O>, capacity: number, config?: CBConfig): Static method creating a circular buffer from iterable values.
  • isFull: Returns true if the buffer is full, false otherwise.
  • isEmpty: Returns true if the buffer is empty, false otherwise.
  • read(): Reads and returns the next value from the buffer. Returns undefined if the buffer is empty.
  • resize(newCapacity: number, force?: boolean): Resizes the buffer.
  • size: Returns the current number of elements in the buffer.
  • toArray(clear?: boolean): Converts the buffer to an array.
  • write(value: T): Writes a value to the buffer.
  • writeAll(values: Iterable<T>): Writes multiple values to the buffer from an iterable.

Iterating

CircularBuffer extends IterableElement, making it iterable. You can use for...of loops to iterate over the elements:

for (const value of buffer) {
    console.log(value);
}

Contributing Guidelines

Contributions are welcome! Please fork the repository and submit a pull request.

License Information

MIT License