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

shuffled-priority-queue

v2.1.0

Published

A priority queue that shuffles elements with the same priority.

Downloads

6,979

Readme

shuffled-priority-queue

A priority queue that shuffles elements with the same priority.

npm install shuffled-priority-queue

Build Status

Usage

const spq = require('shuffled-priority-queue')
const queue = spq()

queue.add({
  priority: 0,
  value: 'hello'
})

queue.add({
  priority: 0,
  value: 'world'
})

queue.add({
  priority: 1,
  value: 'welt'
})

queue.add({
  priority: 2,
  value: 'verden'
})

console.log(queue.shift()) // returns {value: 'verden'}
console.log(queue.shift()) // returns {value: 'welt'}
console.log(queue.shift()) // returns {value: 'hello'} or {value: 'world'}
console.log(queue.shift()) // returns {value: 'hello'} or {value: 'world'}
console.log(queue.shift()) // returns null (empty queue)

API

const queue = spq()

Create a new queue.

value = queue.add(value)

Add a new value to the queue. The value is returned for convenience If you set value.priority to a number, it'll be added to the queue at that priority.

queue.remove(value)

Remove a value from the queue.

bool = queue.has(value)

Check if a value is in the queue.

value = queue.shift()

Shift the next value off the queue.

The value returned will have the highest priority off the queue. If multiple values have the same priority a random one is returned.

value = queue.head()

Same as shift() but does not mutate the queue.

value = queue.pop()

Same as shift() but returns a value with the lowest priority.

value = queue.tail()

Same as pop() but does not mutate the queue.

queue.length

Property containing how many items are in the queue

for (const value of queue)

Iterate the queue from highest priority to lowest using the for of syntax

value = queue.next([prevValue])

Iterate the queue from highest priority to lowest.

let prevValue = null

while (prevValue = queue.next(prevValue)) {
  console.log('value:', prevValue)
}

value = queue.prev([prevValue])

Iterate the queue from lowest priority to highest.

let prevValue = null

while (prevValue = queue.prev(prevValue)) {
  console.log('value:', prevValue)
}

License

MIT