round-robin-js
An implementation of the round robin as a data structure. Two strategies are implemented to select the next item in the round, a Sequential one that selects the next item based on the order of insertion, and a Random one that selects the next item randomly.
Contents
Install
npm install --save round-robin-js
API
import
JS
const { SequentialRoundRobin, RandomRoundRobin } = require('round-robin-js');
import { SequentialRoundRobin, RandomRoundRobin } from 'round-robin-js';
TS
import {
SequentialRoundRobin,
RandomRoundRobin,
RoundRobinItem
} from 'round-robin-js';
constructor
accepts an initial list of values.
JS
const sequentialTable = new SequentialRoundRobin(['T1', 'T2', 'T3']);
const randomTable = new RandomRoundRobin([5, 10, 15]);
TS
const sequentialTable = new SequentialRoundRobin<string>(['T1', 'T2', 'T3']);
const randomTable = new RandomRoundRobin<number>([5, 10, 15]);
add(value)
adds a new item to the table.
JS
params | return |
---|
value: any | object |
const { key, value } = sequentialTable.add('T4');
console.log(key, value);
const { key, value } = randomTable.add(25);
console.log(key, value);
TS
params | return |
---|
value: T | RoundRobinItem<T> |
const item: RoundRobinItem = sequentialTable.add('T4');
console.log(item);
const item: RoundRobinItem = randomTable.add(25);
console.log(item);
count()
returns the number of items in the table.
console.log(sequentialTable.count());
console.log(randomTable.count());
next()
returns the next item in the round.
return |
---|
object (RoundRobinItem<T>) |
console.log(sequentialTable.next());
console.log(sequentialTable.next());
console.log(sequentialTable.next());
console.log(sequentialTable.next());
console.log(sequentialTable.next());
console.log(randomTable.next());
console.log(randomTable.next());
console.log(randomTable.next());
console.log(randomTable.next());
console.log(randomTable.next());
deleteByKey(key)
deletes an item by its key from the table.
sequentialTable.deleteByKey(0);
sequentialTable.deleteByKey(2);
console.log(sequentialTable.next());
console.log(sequentialTable.next());
console.log(sequentialTable.next());
randomTable.deleteByKey(0);
randomTable.deleteByKey(2);
console.log(randomTable.next());
console.log(randomTable.next());
console.log(randomTable.next());
deleteByValue(cb)
deletes items with values that match a criteria from the table and returns the number of deleted items.
params | return |
---|
cb: (value: T) => boolean | number |
const seqTable = new SequentialRoundRobin<number>([2, 3, 5, 6, 7, 10]);
const ranTable = new RandomRoundRobin<{ id: string }>([
{ id: '123' },
{ id: 'id456' },
{ id: '456' },
{ id: 'id780' }
]);
const d1 = seqTable.deleteByValue((n) => n % 2 === 1);
console.log(seqTable.next(), seqTable.next(), seqTable.next())
const d2 = ranTable.deleteByValue((obj) => obj.id.indexOf('id') === 0);
console.log(ranTable.next(), ranTable.next())
reset()
resets the table with the intial values.
sequentialTable.reset();
console.log(sequentialTable.count());
randomTable.reset();
console.log(randomTable.count());
clear()
clears all values in the table.
sequentialTable.clear();
console.log(sequentialTable.count());
randomTable.clear();
console.log(randomTable.count());
Build
grunt build
License
The MIT License. Full License is here