Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 24, 2020
0 parents commit fec94a0
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
3 changes: 3 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github: sindresorhus
open_collective: sindresorhus
custom: https://sindresorhus.com/donate
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
56 changes: 56 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
declare class Queue<ValueType> implements Iterable<ValueType> {
/**
The size of the queue.
*/
readonly size: number;

/**
Tiny queue data structure.
The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.
@example
```
import Queue = require('yocto-queue');
const queue = new Queue();
queue.enqueue('πŸ¦„');
queue.enqueue('🌈');
console.log(queue.size);
//=> 2
console.log(...queue);
//=> 'πŸ¦„ 🌈'
console.log(queue.dequeue());
//=> 'πŸ¦„'
console.log(queue.dequeue());
//=> '🌈'
```
*/
constructor();

[Symbol.iterator](): IterableIterator<ValueType>;

/**
Add a value to the queue.
*/
enqueue(value: ValueType): void;

/**
Remove the next value in the queue.
@returns The removed value or `undefined` if the queue is empty.
*/
dequeue(): ValueType | undefined;

/**
Clear the queue.
*/
clear(): void;
}

export = Queue;
68 changes: 68 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class Node {
/// value;
/// next;

constructor(value) {
this.value = value;

// TODO: Remove this when targeting Node.js 12.
this.next = undefined;
}
}

class Queue {
// TODO: Use private class fields when targeting Node.js 12.
// #_head;
// #_tail;
// #_size;

constructor() {
this.clear();
}

enqueue(value) {
const node = new Node(value);

if (this._head) {
this._tail.next = node;
this._tail = node;
} else {
this._head = node;
this._tail = node;
}

this._size++;
}

dequeue() {
const current = this._head;
if (!current) {
return;
}

this._head = this._head.next;
this._size--;
return current.value;
}

clear() {
this._head = undefined;
this._tail = undefined;
this._size = 0;
}

get size() {
return this._size;
}

* [Symbol.iterator]() {
let current = this._head;

while (current) {
yield current.value;
current = current.next;
}
}
}

module.exports = Queue;
7 changes: 7 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {expectType} from 'tsd';
import Queue = require('.');

const queue = new Queue<string>();
queue.enqueue('πŸ¦„');

expectType<string | undefined>(queue.dequeue());
9 changes: 9 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "yocto-queue",
"version": "0.0.0",
"description": "Tiny queue data structure",
"license": "MIT",
"repository": "sindresorhus/yocto-queue",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"queue",
"data",
"structure",
"algorithm",
"queues",
"queuing",
"list",
"array",
"linkedlist",
"fifo",
"enqueue",
"dequeue",
"data-structure"
],
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.13.1",
"xo": "^0.35.0"
}
}
64 changes: 64 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# yocto-queue [![](https://badgen.net/bundlephobia/minzip/yocto-queue)](https://bundlephobia.com/result?p=yocto-queue)

> Tiny queue data structure
You should use this package instead of an array if you do a lot of `Array#push()` and `Array#shift()` on large arrays, since `Array#shift()` has [linear time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(N)%E2%80%94Linear%20Time) *O(n)* while `Queue#dequeue()` has [constant time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(1)%20%E2%80%94%20Constant%20Time) *O(1)*. That makes a huge difference for large arrays.

> A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. A queue works based on the first-in, first-out ([FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))) principle.
## Install

```
$ npm install yocto-queue
```

## Usage

```js
const Queue = require('yocto-queue');

const queue = new Queue();

queue.enqueue('πŸ¦„');
queue.enqueue('🌈');

console.log(queue.size);
//=> 2

console.log(...queue);
//=> 'πŸ¦„ 🌈'

console.log(queue.dequeue());
//=> 'πŸ¦„'

console.log(queue.dequeue());
//=> '🌈'
```

## API

### `queue = new Queue()`

The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow.

#### `.enqueue(value)`

Add a value to the queue.

#### `.dequeue()`

Remove the next value in the queue.

Returns the removed value or `undefined` if the queue is empty.

#### `.clear()`

Clear the queue.

#### `.size`

The size of the queue.

## Related

- [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple β€œLeast Recently Used” (LRU) cache
58 changes: 58 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import test from 'ava';
import Queue from '.';

test('.enqueue()', t => {
const queue = new Queue();
queue.enqueue('πŸ¦„');
t.is(queue.dequeue(), 'πŸ¦„');
queue.enqueue('🌈');
queue.enqueue('❀️');
t.is(queue.dequeue(), '🌈');
t.is(queue.dequeue(), '❀️');
});

test('.dequeue()', t => {
const queue = new Queue();
t.is(queue.dequeue(), undefined);
t.is(queue.dequeue(), undefined);
queue.enqueue('πŸ¦„');
t.is(queue.dequeue(), 'πŸ¦„');
t.is(queue.dequeue(), undefined);
});

test('.clear()', t => {
const queue = new Queue();
queue.clear();
queue.enqueue(1);
queue.clear();
t.is(queue.size, 0);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.clear();
t.is(queue.size, 0);
});

test('.size', t => {
const queue = new Queue();
t.is(queue.size, 0);
queue.clear();
t.is(queue.size, 0);
queue.enqueue('πŸ¦„');
t.is(queue.size, 1);
queue.enqueue('πŸ¦„');
t.is(queue.size, 2);
queue.dequeue();
t.is(queue.size, 1);
queue.dequeue();
t.is(queue.size, 0);
queue.dequeue();
t.is(queue.size, 0);
});

test('iterable', t => {
const queue = new Queue();
queue.enqueue('πŸ¦„');
queue.enqueue('🌈');
t.deepEqual([...queue], ['πŸ¦„', '🌈']);
});

0 comments on commit fec94a0

Please sign in to comment.