Socket
Socket
Sign inDemoInstall

round-robin-js

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

round-robin-js

an implementation of round robin as a data structure


Version published
Weekly downloads
1.8K
increased by33.36%
Maintainers
1
Weekly downloads
 
Created
Source

round-robin-js

npm npm npm

An implementation of the round robin as a data structure. The following strategies are implemented:

SequentialRoundRobinselects the next item based on the order of insertion
RandomRoundRobinselects the next item randomly
PriorityRoundRobinselects the next item based on its priority

Contents

Install

npm install --save round-robin-js

require

const {
  SequentialRoundRobin,
  RandomRoundRobin,
  PriorityRoundRobin
} = require('round-robin-js');

import

import {
  SequentialRoundRobin,
  RandomRoundRobin,
  PriorityRoundRobin,
  RoundRobinItem // the internal item type
} from 'round-robin-js';

API

constructor

All types accept an initial list of values. PriorityRoundRobin requires a compare function to select next item based on priority.

JS
const cpusTable = new SequentialRoundRobin([1, 2, 3]);

const rockPaperScissors = new RandomRoundRobin(['Rock', 'Paper', 'Scissors']);

const availableServers = new PriorityRoundRobin(
  (a, b) => a.load - b.load, // select next available server with lowest load
  [{ hostname: 's1.test.com', load: 40 }, { hostname: 's2.test.com', load: 30 }]
);
TS
const cpusTable = new SequentialRoundRobin<number>([1, 2, 3]);

const rockPaperScissors = new RandomRoundRobin<string>(['Rock', 'Paper', 'Scissors']);

interface IServer {
  hostname: string;
  load: number;
}
const availableServers = new PriorityRoundRobin<IServer>(
  (a: IServer, b: IServer) => a.load - b.load, // select next available server with lowest load
  [{ hostname: 's1.test.com', load: 40 }, { hostname: 's2.test.com', load: 30 }]
);

add

adds a new item to the table.

cpusTable.add(4); // { key: 3, value: 4 }
cpusTable.add(5); // { key: 4, value: 5 }

availableServers.add({ hostname: 's3.test.com', load: 15 }); // { key: 2, value: { hostname: 's3.test.com', load: 15 } }
availableServers.add({ hostname: 's4.test.com', load: 60 }); // { key: 3, value: { hostname: 's4.test.com', load: 60 } }

next

selects and returns the next item in the round.

// first round
cpusTable.next(); // { key: 0, value: 1 }
cpusTable.next(); // { key: 1, value: 2 }
cpusTable.next(); // { key: 2, value: 3 }
cpusTable.next(); // { key: 3, value: 4 }
cpusTable.next(); // { key: 4, value: 5 }
// second round ...
cpusTable.next(); // { key: 0, value: 1 }

// first round
rockPaperScissors.next(); // { key: 1, value: 'Paper' }
rockPaperScissors.next(); // { key: 0, value: 'Rock' }
rockPaperScissors.next(); // { key: 2, value: 'Scissors' }
// second round ...
rockPaperScissors.next(); // { key: 0, value: 'Rock' }

availableServers.next(); // { key: 2, value: { hostname: 's3.test.com', load: 15 } }
availableServers.next(); // { key: 1, value: { hostname: 's2.test.com', load: 30 } }
availableServers.next(); // { key: 0, value: { hostname: 's1.test.com', load: 40 } }
availableServers.next(); // { key: 3, value: { hostname: 's4.test.com', load: 60 } }
// second round ...
availableServers.next(); // { key: 2, value: { hostname: 's3.test.com', load: 15 } }

count

returns the number of items in the table.

cpusTable.count(); // 5
rockPaperScissors.count(); // 3
availableServers.count(); // 4

deleteByKey

deletes an item from the table by its key.

cpusTable.deleteByKey(1); // 2 is deleted
cpusTable.count(); // 4

availableServers.deleteByKey(2); // true / { hostname: 's3.test.com', load: 15 } is deleted
availableServers.count(); // 3

deleteByValue

accepts a callback to delete items that match a criteria from the table and returns the count of deleted.

availableServers.deleteByValue((s) => s.load > 30); // 2
availableServers.next(); // { key: 1, value: { hostname: 's2.test.com', load: 30 } }
availableServers.next(); // { key: 1, value: { hostname: 's2.test.com', load: 30 } }

reset

resets the round selection from the start.

cpusTable.next(); // { key: 1, value: 2 }
cpusTable.next(); // { key: 2, value: 3 }
cpusTable.reset();
cpusTable.next(); // { key: 0, value: 1 }
cpusTable.next(); // { key: 1, value: 2 }

availableServers.next(); // { key: 1, value: { hostname: 's2.test.com', load: 30 } }
availableServers.add({ hostname: 's99.test.com', load: 10 });
availableServers.next(); // { key: 4, value: { hostname: 's99.test.com', load: 10 } }
availableServers.reset();
availableServers.next(); // { key: 4, value: { hostname: 's99.test.com', load: 10 } }

clear

clears all values in the table.

cpusTable.clear();
cpusTable.count(); // 0
cpusTable.next(); // null

rockPaperScissors.clear();
rockPaperScissors.count(); // 0
rockPaperScissors.next(); // null

availableServers.clear();
availableServers.count(); // 0
availableServers.next(); // null

Build

grunt build

License

The MIT License. Full License is here

Keywords

FAQs

Package last updated on 10 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc