Git Product home page Git Product logo

spinning-reservoir's Introduction

Spinning Reservoir

Spinning Reservoir is a TypeScript class that implements a circular buffer with a fixed maximum size. When the buffer is full and a new item is added, the oldest item in the buffer is discarded.

Installation

Install the package with npm:

npm install spinning-reservoir

API

constructor(maxSize: number)

Creates a new SpinningReservoir with the specified maximum size.

new SpinningReservoir(20)

push(value: T): SpinningReservoir<T>

Adds a new item to the buffer. If the buffer is full, the oldest item is discarded.

const buffer = new SpinningReservoir()

console.log(buffer.size) // 0

buffer.push('hi')
buffer.push('there')

console.log(buffer.size) // 2

clear(): SpinningReservoir<T>

Clears all items from the buffer.

const buffer = new SpinningReservoir()

buffer.push('hi')
console.log(buffer.size) // 1

buffer.clear()
console.log(buffer.size) // 0

get(index: number): T | undefined

Returns the item at the specified index, or undefined if the index is out of bounds.

const buffer = new SpinningReservoir()

buffer.push('hi')
buffer.push('there')

console.log(buffer.get(0)) // 'hi'
console.log(buffer.get(1)) // 'there'

size: number

The current number of items in the buffer.

const buffer = new SpinningReservoir()

console.log(buffer.size) // 0

buffer.push('hi')
buffer.push('there')

console.log(buffer.size) // 2

maxSizeValue: number

The maximum size of the buffer. Can be changed after the buffer is created, but must be an integer greater than 0.

const buffer = new SpinningReservoir()

console.log(buffer.maxSizeValue) // 10

buffer.maxSizeValue = 20

console.log(buffer.maxSizeValue) // 20

[Symbol.iterator](): Iterator<T>

Returns an iterator that iterates over the items in the buffer in the order they were added.

const buffer = new SpinningReservoir<string>()

buffer.push('hi')
buffer.push('there')

for (const value of buffer) {
  console.log(value)
}

const it = buffer[Symbol.iterator]()
let r = it.next()
while (!r.done) {
  console.log(r.value)
  r = it.next()
}

spinning-reservoir's People

Contributors

areudev avatar

Watchers

 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.