# shuffled-priority-queue

> A priority queue that shuffles elements with the same priority.

Latest version **2.1.0** (published 2018-10-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install shuffled-priority-queue
pnpm add shuffled-priority-queue
yarn add shuffled-priority-queue
bun add shuffled-priority-queue
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-10-05 |
| First published | 2017-05-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 23 |
| Author | Mathias Buus |
| Maintainers | mafintosh |

## Links

- npm: https://www.npmjs.com/package/shuffled-priority-queue
- Repository: https://github.com/mafintosh/shuffled-priority-queue
- Issues: https://github.com/mafintosh/shuffled-priority-queue/issues
- npm.io page: https://npm.io/package/shuffled-priority-queue

## Dependencies (1)

- [unordered-set](https://npm.io/package/unordered-set.md) ^2.0.1

## Recent versions

- 2.1.0 (latest) — 2018-10-05
- 2.0.0 — 2018-10-04
- 1.0.0 — 2017-05-27
- 0.0.0 — 2017-05-27

## README

# shuffled-priority-queue

A priority queue that shuffles elements with the same priority.

```
npm install shuffled-priority-queue
```

[![Build Status](https://travis-ci.org/mafintosh/shuffled-priority-queue.svg?branch=master)](https://travis-ci.org/mafintosh/shuffled-priority-queue)

## Usage

``` js
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.

``` js
let prevValue = null

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

#### `value = queue.prev([prevValue])`

Iterate the queue from lowest priority to highest.

``` js
let prevValue = null

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

## License

MIT

---
_Source: https://npm.io/package/shuffled-priority-queue · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
