# circular-buffer

> A NodeJS simple circular buffer implementation supporting indexing

Latest version **1.0.3** (published 2020-10-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install circular-buffer
pnpm add circular-buffer
yarn add circular-buffer
bun add circular-buffer
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2020-10-30 |
| First published | 2015-04-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Tom Smeding |
| Maintainers | tomsmeding |
| Keywords | circular-buffer, data-structure |

## Links

- npm: https://www.npmjs.com/package/circular-buffer
- Repository: https://github.com/tomsmeding/circular-buffer
- Issues: https://github.com/tomsmeding/circular-buffer/issues
- npm.io page: https://npm.io/package/circular-buffer

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads
- [datasets-fi-postalcodes](https://npm.io/package/datasets-fi-postalcodes.md) — 1.1K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2020-10-30
- 1.0.2 — 2016-10-12
- 1.0.1 — 2016-08-10
- 0.0.6 — 2015-04-22
- 0.0.5 — 2015-04-22
- 0.0.4 — 2015-04-21
- 0.0.3 — 2015-04-21
- 0.0.2 — 2015-04-20
- 0.0.1 — 2015-04-20

## README

# NodeJS Circular Buffer
<p>
	<a href="https://travis-ci.org/tomsmeding/circular-buffer">
		<img src="https://api.travis-ci.org/tomsmeding/circular-buffer.png?branch=master" alt="Travis CI Badge"/>
	</a>
</p>

This is a simple [circular buffer](http://en.wikipedia.org/wiki/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.

```node
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.

---
_Source: https://npm.io/package/circular-buffer · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
