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

CBuffer

v2.2.0

Published

Circular Buffer JavaScript implementation

Downloads

60,339

Readme

CBuffer: JavaScript Circular Buffer Utility

npm Build Status npm license

The end goal of this project is to implement the entire JavaScript Array.prototype, and some additional utility methods, as a circular buffer, a ring buffer structure.

Note: This is called a circular buffer because of what this library accomplishes, but is implemented as an Array. This may be confusing for Node users, which may want to use a true Buffer.

While the entire Array.prototype API is on the roadmap, it's not all quite here. Below is the currently implemented API.

Usage

It's simple. Just use it like you would use an Array.

new CBuffer(10);      // empty buffer with size of 10
new CBuffer(1,2,3,4); // buffer with size 4
CBuffer(5);           // For those who are really lazy, new is optional

Included are several non-standard niceties. Like if you want to catch when data is overwritten, just assign a function to the overflow variable and it will be called whenever a value is about to be overwritten and it will pass the value as the first argument:

var myBuff = CBuffer(4);
myBuff.overflow = function(data) {
    console.log(data);
};

myBuff.push(1,2,3,4); // nothing shows up yet
myBuff.push(5);       // log: 1

API

Mutator Methods

  • pop - Removes the last element from a circular buffer and returns that element.
  • push - Adds one or more elements to the end of a circular buffer and returns the new length.
  • reverse - Reverses the order of the elements of a circular buffer.
  • rotateLeft - Rotates all elements left 1, or n, times.
  • rotateRight - Rotates all elements right 1, or n, times.
  • shift - Removes the first element from a circular buffer and returns that element.
  • sort - Sorts the elements of a circular buffer. Unlike native sort, the default comparitor sorts by a > b.
  • unshift - Adds one or more elements to the front of a circular buffer and returns the new length.

Accessor Methods

  • indexOf - Returns the first (least) index of an element within the circular buffer equal to the specified value, or -1 if none is found.
  • lastIndexOf - Returns the last (greatest) index of an element within the circular buffer equal to the specified value, or -1 if none is found.
  • sortedIndex - Returns the position some value would be inserted into a sorted circular buffer ranked by an optional comparitor.

Iteration Methods

  • every - Returns true if every element in the circular buffer satisfies the provided testing function.
  • forEach - Calls a function for each element in the circular buffer.
  • some - Returns true if at least one element in the circular buffer satisfies the provided testing function.

Utility Methods

  • empty - Equivalent to setting Array.length = 0.
  • fill - Fill with passed argument. Also supports functions.
  • first - Returns first value in circular buffer.
  • last - Returns last value in circular buffer.
  • get - Get value at specific index.
  • set - Set value as specific index.
  • toArray - Return clean ordered array of buffer.
  • overflow - Set to function and will be called when data is about to be overwritten.
  • slice - Return a slice of the buffer as an array.