GithubHelp home page GithubHelp logo

ringbufferjs's Introduction

ringbuffer.js

Build Status

A simple ring buffer data structure for Node.js.

Installation

npm install ringbufferjs

Example

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

ringbufferjs's People

Contributors

bfirsh avatar davidbz avatar janogonzalez avatar rektide avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.