# CBuffer

> Circular Buffer JavaScript implementation

Latest version **2.2.0** (published 2020-09-17) · MIT license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2020-09-17 |
| First published | 2012-03-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 42.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 212 |
| Author | Trevor Norris |
| Maintainers | trev.norris |
| Keywords | circular buffer, ring, circular, buffer, data structure |

## Links

- npm: https://www.npmjs.com/package/CBuffer
- Repository: https://github.com/trevnorris/cbuffer
- Issues: https://github.com/trevnorris/cbuffer/issues
- npm.io page: https://npm.io/package/CBuffer

## Alternatives

- [@luma.gl/experimental](https://npm.io/package/@luma.gl/experimental.md) — 77.1K weekly downloads
- [persona-harness](https://npm.io/package/persona-harness.md) — 4.7K weekly downloads
- [@tsparticles/effect-bubble](https://npm.io/package/@tsparticles/effect-bubble.md) — 4.6K weekly downloads
- [f3d](https://npm.io/package/f3d.md) — 730 weekly downloads
- [spark-html-motion](https://npm.io/package/spark-html-motion.md) — 298 weekly downloads

## Recent versions

- 2.2.0 (latest) — 2020-09-17
- 2.1.0 — 2020-04-21
- 2.0.0 — 2016-07-12
- 1.1.1 — 2015-08-17
- 1.1.0 — 2014-12-19
- 1.0.0 — 2014-12-03
- 0.1.5 — 2014-08-12
- 0.1.4 — 2012-11-26
- 0.1.3 — 2012-10-08
- 0.1.2 — 2012-10-08
- 0.1.1 — 2012-08-06
- 0.1.0 — 2012-08-06
- 0.0.10 — 2012-06-12
- 0.0.9 — 2012-04-17
- 0.0.8 — 2012-04-17
- … 7 more at https://npm.io/package/CBuffer/versions

## README

## CBuffer: JavaScript [Circular Buffer](http://en.wikipedia.org/wiki/Circular_buffer) Utility

[![npm](https://img.shields.io/npm/v/CBuffer.svg)](https://www.npmjs.com/package/CBuffer)
[![Build Status](https://travis-ci.org/trevnorris/cbuffer.svg?branch=master)](https://travis-ci.org/trevnorris/cbuffer)
[![npm license](https://img.shields.io/npm/l/CBuffer.svg)](./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.

```javascript
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:

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

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