{"id":20745329,"url":"https://github.com/klaudiosinani/kiu","last_synced_at":"2025-04-24T06:07:53.897Z","repository":{"id":112528988,"uuid":"195568344","full_name":"klaudiosinani/kiu","owner":"klaudiosinani","description":"FIFO Queues for ES6","archived":false,"fork":false,"pushed_at":"2019-10-05T14:38:52.000Z","size":90,"stargazers_count":13,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T06:07:46.842Z","etag":null,"topics":["data","es6","fifo","queue","structure","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klaudiosinani.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","funding":null,"license":"license.md","code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"klaudiosinani","buy_me_a_coffee":"klaudiosinani","custom":"https://klaudiosinani.com/thanks"}},"created_at":"2019-07-06T17:53:42.000Z","updated_at":"2024-05-19T06:26:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"9f163f94-8eae-4d52-bb2f-c30eda51842f","html_url":"https://github.com/klaudiosinani/kiu","commit_stats":null,"previous_names":["klaussinani/kiu"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fkiu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fkiu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fkiu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fkiu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klaudiosinani","download_url":"https://codeload.github.com/klaudiosinani/kiu/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250573352,"owners_count":21452352,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data","es6","fifo","queue","structure","typescript"],"created_at":"2024-11-17T07:19:43.907Z","updated_at":"2025-04-24T06:07:53.891Z","avatar_url":"https://github.com/klaudiosinani.png","language":"JavaScript","funding_links":["https://github.com/sponsors/klaudiosinani","https://buymeacoffee.com/klaudiosinani","https://klaudiosinani.com/thanks"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  Kiu\n\u003c/h1\u003e\n\n\u003ch4 align=\"center\"\u003e\n  FIFO Queues for ES6\n\u003c/h4\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://travis-ci.com/klaussinani/kiu\"\u003e\n    \u003cimg alt=\"Build Status\" src=\"https://travis-ci.com/klaussinani/kiu.svg?branch=master\"\u003e\n  \u003c/a\u003e\n  \u003ca href='https://coveralls.io/github/klaussinani/kiu?branch=master'\u003e\n    \u003cimg alt=\"Coverage Status\" src=\"https://coveralls.io/repos/github/klaussinani/kiu/badge.svg?branch=master\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nES6 implementation of the FIFO queue data structure with TypeScript support.\n\nVisit the [contributing guidelines](https://github.com/klaussinani/kiu/blob/master/contributing.md#translating-documentation) to learn more on how to translate this document into more languages.\n\n## Contents\n\n- [Description](#description)\n- [Install](#install)\n- [In Depth](#in-depth)\n- [Usage](#usage)\n- [Development](#development)\n- [Related](#related)\n- [Team](#team)\n- [License](#license)\n\n## Install\n\n### Yarn\n\n```bash\nyarn add kiu\n```\n\n### NPM\n\n```bash\nnpm install kiu\n```\n\n## In Depth\n\nA queue is a linear data structure, or more abstractly a sequential collection, in which the entities are kept in order and the principal operations are the addition of entities to the rear terminal position, known as `enqueue`, and removal of entities from the front terminal position, known as `dequeue`. This makes the queue a `FIFO`, First-In-First-Out, data structure. In this FIFO data structure, the first element added to the queue will be the first one to be removed. Once a new element is added, all elements that were added previously have to be removed before the new one can. Additionally, a `peekFirst` operation returns the value of the front element without dequeuing it, and a `peakLast` operation returns the value of the rear element, without mutating the queue as well. Kiu FIFO queues use a linear doubly linked list as their backbone, giving an efficient `O(1)` performance for the enqueuing and dequeuing operations.\n\n## Usage\n\nKiu exposes a chainable API, that can be utilized through a simple and minimal syntax, allowing you to combine methods effectively.\n\nUsage examples can be also found at the [`test`](https://github.com/klaussinani/kiu/tree/master/test) directory.\n\n```js\n'use strict';\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n//=\u003e Queue { head: null, last: null, length: 0 }\n\nqueue.isEmpty();\n//=\u003e true\n\nqueue.enqueue(10);\n//=\u003e Queue {\n// head: Item { value: 10, next: null, prev: null },\n// last: Item { value: 10, next: null, prev: null },\n// length: 1 }\n\nqueue.isEmpty();\n//=\u003e false\n\nqueue.peekFirst();\n//=\u003e 10\n\nqueue\n  .enqueue(20)\n  .enqueue(30)\n  .enqueue(40)\n  .enqueue(50);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 50, next: null, prev:\n//   Item { value: 40, next: [Circular], prev: [Item] } },\n// length: 5 }\n\nqueue.includes(30);\n//=\u003e true\n\nqueue.includes(60);\n//=\u003e false\n\nqueue.dequeue();\n//=\u003e 10\n\nqueue.peekFirst();\n//=\u003e 20\n\nqueue.peekLast();\n//=\u003e 50\n\nqueue.toArray();\n//=\u003e [ 20, 30, 40, 50 ]\n\nqueue.rotateRight(1);\n//=\u003e Queue {\n// head:\n//   Item { value: 30, prev: null, next:\n//   Item { value: 40, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 20, next: null, prev:\n//   Item { value: 50, next: [Circular], prev: [Item] } },\n// length: 4 }\n\nqueue.toArray();\n//=\u003e [ 30, 40, 50, 20 ]\n\nqueue.rotateLeft(3);\n//=\u003e Queue {\n// head:\n//   Item { value: 40, prev: null, next:\n//   Item { value: 50, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 4 }\n\nqueue.toArray();\n//=\u003e [ 40, 50, 20, 30 ]\n\nqueue\n  .reverse()\n  .map(x =\u003e x * 10)\n  .toArray();\n//=\u003e [ 300, 200, 500, 400 ]\n\nqueue.nth(2);\n//=\u003e 500\n```\n\n## API\n\n#### queue.`length`\n\n- Return Type: `Number`\n\nReturns the total number of values in the queue.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: null } },\n// last:\n//   Item { value: 20, next: null, prev:\n//   Item { value: 10, next: [Circular], prev: null } },\n// length: 2 }\nqueue.length;\n//=\u003e 2\n```\n\n#### queue.`clear()`\n\n- Return Type: `Queue`\n\nMutates the queue by removing all residing values and returns it empty.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.length;\n//=\u003e 3\nqueue.clear();\n//=\u003e Queue { head: null, last: null, length: 0 }\nqueue.length;\n//=\u003e 0\n```\n\n#### queue.`dequeue()`\n\n- Return Type: `Any | undefined`\n\nMutates the queue by removing the value located at the front terminal position. Returns the removed value, if the queue is not empty, or `undefined` if it is.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue.enqueue(10);\n//=\u003e Queue {\n// head: Item { value: 10, prev: null, next: null }\n// last: Item { value: 10, prev: null, next: null }\n// length: 1 }\nqueue.dequeue();\n//=\u003e 10\nqueue.dequeue();\n//=\u003e undefined\n```\n\n#### queue.`enqueue(value)`\n\n- Return Type: `Queue`\n\nMutates the queue by inserting a new value at the rear terminal position. Returns the queue itself.\n\n##### **`value`**\n\n- Type: `Any`\n\nValue to insert.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue.enqueue(10);\n//=\u003e Queue {\n// head: Item { value: 10, prev: null, next: null }\n// last: Item { value: 10, prev: null, next: null }\n// length: 1 }\nqueue.enqueue(20).enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.length;\n//=\u003e 3\n```\n\n#### queue.`forEach(fn)`\n\n- Return Type: `Queue`\n\nTraverses the queue, from the front to the rear, and executes the provided `fn` function once for each traversed value, without mutating the queue. Returns the queue itself at the end of the traversal.\n\n##### **`fn`**\n\n- Type: `Function`\n\nUnary function to execute for each traversed value.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.forEach(console.log);\n//=\u003e 10\n// 20\n// 20\n```\n\n#### queue.`includes(value)`\n\n- Return Type: `Boolean`\n\nDetermines whether the queue includes a certain value, returning `true` or `false` as appropriate.\n\n##### **`value`**\n\n- Type: `Any`\n\nValue to search for.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.includes(10);\n//=\u003e true\nqueue.includes(40);\n//=\u003e false\nqueue.includes(20);\n//=\u003e true\n```\n\n#### queue.`isEmpty()`\n\n- Return Type: `Boolean`\n\nDetermines whether the queue is empty, returning `true` or `false` as appropriate.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue.isEmpty();\n//=\u003e true\nqueue.enqueue(10);\n//=\u003e Queue {\n// head: Item { value: 10, prev: null, next: null }\n// last: Item { value: 10, prev: null, next: null }\n// length: 1 }\nqueue.isEmpty();\n//=\u003e false\n```\n\n#### queue.`map(fn)`\n\n- Return Type: `Queue`\n\nTraverses the queue, from front to rear, and mutates it by updating each stored value with the result of calling once the provided `fn` function on it. Returns the in-place mutated queue at the end of the traversal.\n\n##### **`fn`**\n\n- Type: `Function`\n\nUnary function that produces a value of the mutated queue.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.map(x =\u003e x * 10);\n//=\u003e Queue {\n// head:\n//   Item { value: 100, prev: null, next:\n//   Item { value: 200, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 300, next: null, prev:\n//   Item { value: 200, next: [Circular], prev: [Item] } },\n// length: 3 }\n```\n\n#### queue.`nth(n)`\n\n- Return Type: `Any | undefined`\n\nTraverses the queue, from front to rear, and returns the nth value. If the value does not exist, then `undefined` is returned. The queue values follow zero-based numbering, thus the first/initial value corresponds to index 0.\n\n##### **`n`**\n\n- Type: `Number`\n\nZero-based queue index number.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.nth(0);\n//=\u003e 10\nqueue.nth(2);\n//=\u003e 30\nqueue.nth(3);\n//=\u003e undefined\n```\n\n#### queue.`peekFirst()`\n\n- Return Type: `Any | undefined`\n\nReturns the first value, located at the front of the queue, without mutating the queue itself. If the queue is empty, then `undefined` is returned.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.peekFirst();\n//=\u003e 10\n```\n\n#### queue.`peekLast()`\n\n- Return Type: `Any | undefined`\n\nReturns the last value, located at the rear of the queue, without mutating the queue itself. If the queue is empty, then `undefined` is returned.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.peekLast();\n//=\u003e 30\n```\n\n#### queue.`reverse()`\n\n- Return Type: `Queue`\n\nMutates the queue by reversing in-place the residing values. The first value becomes the last one, and the last one becomes the first. Returns the reversed queue.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.toArray();\n//=\u003e [ 10, 20, 30 ]\nqueue.reverse();\n//=\u003e Queue {\n// head:\n//   Item { value: 30, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 10, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 3 }\nqueue.toArray();\n//=\u003e [ 30, 20, 10 ]\n```\n\n#### queue.`rotateLeft(n)`\n\n- Return Type: `Queue`\n\nMutates the queue by moving the `n` rear-most values to the front of the queue in a rotating fashion. Returns the queue itself.\n\n##### **`n`**\n\n- Type: `Number`\n\nNumber of rear-most values to be rotated.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30)\n  .enqueue(40)\n  .enqueue(50);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 50, next: null, prev:\n//   Item { value: 40, next: [Circular], prev: [Item] } },\n// length: 5 }\nqueue.toArray();\n//=\u003e [ 10, 20, 30, 40, 50 ]\nqueue.rotateLeft(2);\n//=\u003e Queue {\n// head:\n//   Item { value: 40, prev: null, next:\n//   Item { value: 50, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 30, next: null, prev:\n//   Item { value: 20, next: [Circular], prev: [Item] } },\n// length: 5 }\nqueue.toArray();\n//=\u003e [ 40, 50, 10, 20, 30 ]\n```\n\n#### queue.`rotateRight(n)`\n\n- Return Type: `Queue`\n\nMutates the queue by moving the `n` front-most items to the rear of the queue in a rotating fashion. Returns the queue itself.\n\n##### **`n`**\n\n- Type: `Number`\n\nNumber of front-most values to be rotated.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30)\n  .enqueue(40)\n  .enqueue(50);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 50, next: null, prev:\n//   Item { value: 40, next: [Circular], prev: [Item] } },\n// length: 5 }\nqueue.toArray();\n//=\u003e [ 10, 20, 30, 40, 50 ]\nqueue.rotateRight(2);\n//=\u003e Queue {\n// head:\n//   Item { value: 30, prev: null, next:\n//   Item { value: 40, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 20, next: null, prev:\n//   Item { value: 10, next: [Circular], prev: [Item] } },\n// length: 5 }\nqueue.toArray();\n//=\u003e [ 30, 40, 50, 10, 20 ]\n```\n\n#### queue.`toArray()`\n\n- Return Type: `Array\u003cAny\u003e`\n\nThe method traverses the queue, from front to rear, and stores each traversed value in an array. The array is returned at the end of the traversal.\n\n```js\nconst {Queue} = require('kiu');\n\nconst queue = new Queue();\n\nqueue\n  .enqueue(10)\n  .enqueue(20)\n  .enqueue(30)\n  .enqueue(40)\n  .enqueue(50);\n//=\u003e Queue {\n// head:\n//   Item { value: 10, prev: null, next:\n//   Item { value: 20, prev: [Circular], next: [Item] } },\n// last:\n//   Item { value: 50, next: null, prev:\n//   Item { value: 40, next: [Circular], prev: [Item] } },\n// length: 5 }\nqueue.toArray();\n//=\u003e [ 10, 20, 30, 40, 50 ]\n```\n\n## Development\n\nFor more info on how to contribute to the project, please read the [contributing guidelines](https://github.com/klaussinani/kiu/blob/master/contributing.md).\n\n- Fork the repository and clone it to your machine\n- Navigate to your local fork: `cd kiu`\n- Install the project dependencies: `npm install` or `yarn install`\n- Lint the code and run the tests: `npm test` or `yarn test`\n\n## Related\n\n- [avlbinstree](https://github.com/klaussinani/avlbinstree) - AVL self-balancing binary search trees for ES6\n- [binoheap](https://github.com/klaussinani/binoheap) - Binomial heaps for ES6\n- [binstree](https://github.com/klaussinani/binstree) - Binary search trees for ES6\n- [doublie](https://github.com/klaussinani/doublie) - Doubly circular \u0026 linear linked lists for ES6\n- [dsforest](https://github.com/klaussinani/dsforest) - Disjoint-set forests for ES6\n- [mheap](https://github.com/klaussinani/mheap) - Binary min \u0026 max heaps for ES6\n- [prioqueue](https://github.com/klaussinani/prioqueue) - Priority queues for ES6\n- [shtack](https://github.com/klaussinani/shtack) - LIFO Stacks for ES6\n- [singlie](https://github.com/klaussinani/singlie) - Singly circular \u0026 linear linked lists for ES6\n\n## Team\n\n- Klaus Sinani [(@klaussinani)](https://github.com/klaussinani)\n\n## License\n\n[MIT](https://github.com/klaussinani/kiu/blob/master/license.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fkiu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklaudiosinani%2Fkiu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fkiu/lists"}