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

circular-buffer

v1.0.3

Published

A NodeJS simple circular buffer implementation supporting indexing

Downloads

22,793

Readme

NodeJS Circular Buffer

This is a simple circular buffer implementation for NodeJS.

The implementation can function both as a forgetful queue (which it's most suited for), and as a (forgetful) stack. Queue functionality uses enq() and deq(); stack functionality uses push() and pop(). Values are enqueued at the front of the buffer and dequeued at the back of the buffer; pushing and popping is at the back of the buffer. Indexing is front-to-back: the last-enqueued item has lowest index, which is also the first-pushed item.

Usage

Below is a sample session with a circular buffer with this package. It should answer most questions.

var CircularBuffer = require("circular-buffer");

var buf = new CircularBuffer(3);
console.log(buf.capacity()); // -> 3
buf.enq(1);
buf.enq(2);
console.log(buf.size()); // -> 2
buf.toarray(); // -> [2,1]
buf.push(3);
buf.toarray(); // -> [2,1,3]
buf.enq(4);
console.log(buf.size()); // -> 3  (despite having added a fourth item!)
buf.toarray(); // -> [4,2,1]
buf.get(0); // -> 4  (last enqueued item is at start of buffer)
buf.get(0,2); // -> [4,2,1]  (2-parameter get takes start and end)
buf.toarray(); // -> [4,2,1]  (equivalent to buf.get(0,buf.size() - 1) )
console.log(buf.deq()); // -> 1
buf.toarray(); // -> [4,2]
buf.pop(); // -> 2  (deq and pop are functionally the same)
buf.deq(); // -> 4
buf.toarray(); // -> []
buf.deq(); // -> throws RangeError("CircularBuffer dequeue on empty buffer")

Functions

  • size() -> integer
    • Returns the current number of items in the buffer.
  • capacity() -> integer
    • Returns the maximum number of items in the buffer (specified when creating it).
  • enq(value)
    • Enqueue value at the front of the buffer
  • deq() -> value
    • Dequeue an item from the back of the buffer; returns that item. Throws RangeError if the buffer is empty on invocation.
  • push(value)
    • Push value at the back of the buffer
  • pop() -> value
    • Equivalent to deq().
  • shift() -> value
    • Removes an item from the front of the buffer; returns that item. Throws RangeError if the buffer is empty on invocation.
  • get(idx) -> value
    • Get the value at index idx. 0 is the front of the buffer (last-enqueued item, or first-pushed item), buf.size()-1 is the back of the buffer.
  • get(start,end) -> [value]
    • Gets the values from index start up to and including index end; returns an array, in front-to-back order. Equivalent to [buf.get(start),buf.get(start+1),/*etc*/,buf.get(end)].
  • toarray() -> [value]
    • Equivalent to buf.get(0,buf.size() - 1): exports all items in the buffer in front-to-back order.

Testing

To test the package simply run npm update && npm test in the package's root folder.