npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

round-robin-js

v3.0.5

Published

an implementation of round robin as a data structure

Downloads

5,411

Readme

round-robin-js

npm npm npm

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

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