Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

circular-buffer

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

circular-buffer - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

2

package.json
{
"name": "circular-buffer",
"version": "1.0.1",
"version": "1.0.2",
"description": "A NodeJS simple circular buffer implementation supporting indexing ",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -10,2 +10,4 @@ # NodeJS Circular Buffer

The implementation can function both as a 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

@@ -23,12 +25,15 @@

console.log(buf.size()); // -> 2
buf.enq(3);
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.get(0); // -> 4 (last added item is at start of buffer)
buf.get(0,2); // -> [4,3,2] (2-parameter get takes start and end)
buf.toarray(); // -> [4,3,2] (equivalent to buf.get(0,buf.size() - 1) )
console.log(buf.deq()); // -> 4
buf.toarray(); // -> [3,2]
buf.deq(); // -> 3
buf.deq(); // -> 2
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(); // -> []

@@ -45,11 +50,11 @@ buf.deq(); // -> throws RangeError("CircularBuffer dequeue on empty buffer")

- `enq(value)`
- Enqueue `value` at the start of the buffer
- Enqueue `value` at the front of the buffer
- `deq()` -> `value`
- Dequeue an item from the start of the buffer; returns that item. Throws `RangeError` when the buffer is empty on invocation.
- Dequeue an item from the back 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 start of the buffer (last-enqueued item), `buf.size()-1` is the oldest item in the buffer.
- 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. The newest item is first in the array, and it is the same as `buf.get(start)`.
- 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 order.
- Equivalent to `buf.get(0,buf.size() - 1)`: exports all items in the buffer in front-to-back order.

@@ -56,0 +61,0 @@ ## Testing

@@ -133,2 +133,30 @@ var assert = require("chai").assert;

it("should handle the README example correctly", function () {
var buf = new CircularBuffer(3);
assert.deepEqual(buf.capacity(), 3);
buf.enq(1);
buf.enq(2);
assert.deepEqual(buf.size(), 2);
assert.deepEqual(buf.toarray(), [2,1]);
buf.push(3);
assert.deepEqual(buf.toarray(), [2,1,3]);
buf.enq(4);
assert.deepEqual(buf.size(), 3);
assert.deepEqual(buf.toarray(), [4,2,1]);
assert.deepEqual(buf.get(0), 4);
assert.deepEqual(buf.get(0,2), [4,2,1]);
assert.deepEqual(buf.toarray(), [4,2,1]);
assert.deepEqual(buf.deq(), 1);
assert.deepEqual(buf.toarray(), [4,2]);
assert.deepEqual(buf.pop(), 2);
assert.deepEqual(buf.deq(), 4);
assert.deepEqual(buf.toarray(), []);
try {
buf.deq();
} catch (e) {
return;
}
assert.fail("No error after dequeueing empty buffer", "Error after dequeueing empty buffer");
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc