# ringbufferjs

> a simple ring buffer data structure

Latest version **2.0.0** (published 2020-05-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install ringbufferjs
pnpm add ringbufferjs
yarn add ringbufferjs
bun add ringbufferjs
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2020-05-07 |
| First published | 2013-03-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/ringbufferjs) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jano González |
| Maintainers | janogonzalez |
| Keywords | ring, buffer, circular, queue, ring buffer, circular buffer, data structure |

## Links

- npm: https://www.npmjs.com/package/ringbufferjs
- Repository: https://github.com/janogonzalez/ringbufferjs
- Issues: https://github.com/janogonzalez/ringbufferjs/issues
- npm.io page: https://npm.io/package/ringbufferjs

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2020-05-07
- 1.1.0 — 2017-09-11
- 1.0.0 — 2017-05-04
- 0.0.1 — 2013-03-04

## README

# ringbuffer.js

[![Build Status](https://travis-ci.com/janogonzalez/ringbufferjs.svg?branch=master)](
  https://travis-ci.com/janogonzalez/ringbufferjs)

A simple ring buffer data structure for Node.js.

## Installation

```
$ npm install ringbufferjs
```

## Example

```js
var RingBuffer = require('ringbufferjs');

var ring = new RingBuffer(2);

ring.enq(10);
ring.enq(5);
ring.enq(1); // 10 will be discarded
ring.size(); // 2
ring.peek(); // 5
ring.deq(); // 5
ring.size(); // 1
```

## API

### `RingBuffer()`

Initializes a new empty `RingBuffer` with the default capacity (50).

### `RingBuffer(capacity)`

Initializes a new empty `RingBuffer` with the given `capacity`.

### `RingBuffer(capacity, evictedCb)`

Initializes a new empty `RingBuffer` with the given `capacity` which will
execute the provided `evictedCb` when an element is evicted.

### `RingBuffer#capacity()`

Returns the capacity of the ring buffer.

### `RingBuffer#deq()`

Dequeues the top element of the ring buffer.
Throws an `Error` when the buffer is empty.

### `RingBuffer#deqN(count)`

Dequeues `count` elements from the top of the ring buffer and returns them.
Throws an `Error` if there are not enough elements in the buffer.

### `RingBuffer#enq(element)`

Enqueues the `element` at the end of the ring buffer and returns its new size.
When the buffer is full the oldest element is discarded.

### `RingBuffer#isEmpty()`

Returns whether the ring buffer is empty or not.

### `RingBuffer#isFull()`

Returns whether the ring buffer is full or not.

### `RingBuffer#peek()`

Peeks at the top element of the ring buffer.
Throws an `Error` when the buffer is empty.

### `RingBuffer#peekN(count)`

Returns `count` elements from the top of the ring buffer.
Throws an `Error` if there are not enough elements in the buffer.

### `RingBuffer#size()`

Returns the size of the ring buffer.

## Testing

```
$ npm install
$ npm test
```

## Licence

MIT

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