{"id":13432026,"url":"https://github.com/wooorm/linked-list","last_synced_at":"2025-04-07T07:13:34.348Z","repository":{"id":13634801,"uuid":"16328157","full_name":"wooorm/linked-list","owner":"wooorm","description":"Minimalistic linked lists","archived":false,"fork":false,"pushed_at":"2023-04-28T12:07:33.000Z","size":135,"stargazers_count":83,"open_issues_count":0,"forks_count":10,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-04-14T06:51:56.873Z","etag":null,"topics":["double","doubly","linked","list"],"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/wooorm.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":"funding.yml","license":"license","code_of_conduct":null,"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":"wooorm"}},"created_at":"2014-01-28T22:26:18.000Z","updated_at":"2024-06-18T13:47:21.740Z","dependencies_parsed_at":"2024-06-22T20:33:17.930Z","dependency_job_id":null,"html_url":"https://github.com/wooorm/linked-list","commit_stats":{"total_commits":129,"total_committers":6,"mean_commits":21.5,"dds":0.0852713178294574,"last_synced_commit":"761d320679a432ca77be7f8fbcd40e5bc0a3e9e7"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Flinked-list","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Flinked-list/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Flinked-list/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wooorm%2Flinked-list/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wooorm","download_url":"https://codeload.github.com/wooorm/linked-list/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247608153,"owners_count":20965952,"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":["double","doubly","linked","list"],"created_at":"2024-07-31T02:01:07.977Z","updated_at":"2025-04-07T07:13:34.318Z","avatar_url":"https://github.com/wooorm.png","language":"JavaScript","funding_links":["https://github.com/sponsors/wooorm"],"categories":["JavaScript"],"sub_categories":[],"readme":"# linked-list\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n\nSmall double [linked list][wiki].\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`List([items…])`](#listitems)\n    *   [`Item()`](#item)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Security](#security)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a small double linked list.\nItems in linked lists know about their next sibling (the item after them).\nIn double linked lists, items also know about their previous sibling (the item\nbefore them).\n\n## When should I use this?\n\nYou can use this project as a reference for how to implement a linked list but\nit’s also definitely possible to use it, directly or by subclassing its lists\nand items.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 14.14+, 16.0+), install with [npm][]:\n\n```sh\nnpm install linked-list\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {List, Item} from 'https://esm.sh/linked-list@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {List, Item} from 'https://esm.sh/linked-list@3?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {List, Item} from 'linked-list'\n\nconst item1 = new Item()\nconst item2 = new Item()\nconst item3 = new Item()\nconst list = new List(item1, item2, item3)\n\nlist.head // =\u003e item1\nlist.head.next // =\u003e item2\nlist.head.next.next // =\u003e item3\nlist.head.next.prev // =\u003e item1\nlist.tail // =\u003e item3\nlist.tail.next // =\u003e `null`\n```\n\nSubclassing:\n\n```js\nimport {List, Item} from 'linked-list'\n\nclass Tokens extends List {\n  /** @param {string} delimiter */\n  join(delimiter) {\n    return this.toArray().join(delimiter)\n  }\n}\n\nclass Token extends Item {\n  /** @param {string} value */\n  constructor(value) {\n    super()\n    this.value = value\n  }\n\n  toString() {\n    return this.value\n  }\n}\n\nconst dogs = new Token('dogs')\nconst and = new Token('\u0026')\nconst cats = new Token('cats')\nconst tokens = new Tokens(dogs, and, cats)\n\nconsole.log(tokens.join(' ')) // =\u003e 'dogs \u0026 cats'\n\nand.prepend(cats)\nand.append(dogs)\n\nconsole.log(tokens.join(' ') + '!') // =\u003e 'cats \u0026 dogs!'\n```\n\n## API\n\nThis package exports the identifiers `List` and `Item`.\nThere is no default export.\n\n### `List([items…])`\n\n```js\nnew List()\nnew List(new Item(), new Item())\n```\n\nCreate a new list from the given items.\n\nIgnores `null` or `undefined` values.\nThrows an error when a given item has no `detach`, `append`, or `prepend`\nmethods.\n\n#### `List.from([items])`\n\n```js\nList.from()\nList.from([])\nList.from([new Item(), new Item()])\n```\n\nCreate a new `this` from the given array of items.\n\nIgnores `null` or `undefined` values.\nThrows an error when a given item has no `detach`, `append`, or `prepend`\nmethods.\n\n#### `List.of([items…])`\n\n```js\nList.of()\nList.of(new Item(), new Item())\n```\n\nCreate a new `this` from the given arguments.\n\nIgnores `null` or `undefined` values.\nThrows an error when a given item has no `detach`, `append`, or `prepend`\nmethods.\n\n#### `List#append(item)`\n\n```js\nconst list = new List()\nconst item = new Item()\n\nconsole.log(list.head === null) // =\u003e true\nconsole.log(item.list === null) // =\u003e true\n\nlist.append(item)\n\nconsole.log(list.head === item) // =\u003e true\nconsole.log(item.list === list) // =\u003e true\n```\n\nAppend an item to a list.\n\nThrows an error when the given item has no `detach`, `append`, or `prepend`\nmethods.\nReturns the given item.\n\n#### `List#prepend(item)`\n\n```js\nconst list = new List()\nconst item = new Item()\n\nlist.prepend(item)\n```\n\nPrepend an item to a list.\n\nThrows an error when the given item has no `detach`, `append`, or `prepend`\nmethods.\nReturns the given item.\n\n#### `List#toArray()`\n\n```js\nconst item1 = new Item()\nconst item2 = new Item()\nconst list = new List(item1, item2)\nconst array = list.toArray()\n\nconsole.log(array[0] === item1) // =\u003e true\nconsole.log(array[1] === item2) // =\u003e true\nconsole.log(array[0].next === item2) // =\u003e true\nconsole.log(array[1].prev === item1) // =\u003e true\n```\n\nReturns the items of the list as an array.\n\nThis does *not* detach the items.\n\n\u003e **Note**: `List` also implements an iterator.\n\u003e That means you can also do `[...list]` to get an array.\n\n#### `List#head`\n\n```js\nconst item = new Item()\nconst list = new List(item)\n\nconsole.log(list.head === item) // =\u003e true\n```\n\nThe first item in a list or `null` otherwise.\n\n#### `List#tail`\n\n```js\nconst list = new List()\nconst item1 = new Item()\nconst item2 = new Item()\n\nconsole.log(list.tail === null) // =\u003e true\n\nlist.append(item1)\nconsole.log(list.tail === null) // =\u003e true, see note.\n\nlist.append(item2)\nconsole.log(list.tail === item2) // =\u003e true\n```\n\nThe last item in a list and `null` otherwise.\n\n\u003e 👉 **Note**: a list with only one item has **no tail**, only a head.\n\n#### `List#size`\n\n```js\nconst list = new List()\nconst item1 = new Item()\nconst item2 = new Item()\n\nconsole.log(list.size === 0) // =\u003e true\n\nlist.append(item1)\nconsole.log(list.size === 1) // =\u003e true\n\nlist.append(item2)\nconsole.log(list.size === 2) // =\u003e true\n```\n\nThe number of items in the list.\n\n### `Item()`\n\n```js\nconst item = new Item()\n```\n\nCreate a new linked list item.\n\n#### `Item#append(item)`\n\n```js\nconst item1 = new Item()\nconst item2 = new Item()\n\nnew List().append(item1)\n\nconsole.log(item1.next === null) // =\u003e true\n\nitem1.append(item2)\nconsole.log(item1.next === item2) // =\u003e true\n```\n\nAdd the given item **after** the operated on item in a list.\n\nThrows an error when the given item has no `detach`, `append`, or `prepend`\nmethods.\nReturns `false` when the operated on item is not attached to a list, otherwise\nthe given item.\n\n#### `Item#prepend(item)`\n\n```js\nconst item1 = new Item()\nconst item2 = new Item()\n\nnew List().append(item1)\n\nconsole.log(item1.prev === null) // =\u003e true\n\nitem1.prepend(item2)\nconsole.log(item1.prev === item2) // =\u003e true\n```\n\nAdd the given item **before** the operated on item in a list.\n\nThrows an error when the given item has no `detach`, `append`, or `prepend`\nmethods.\nReturns `false` when the operated on item is not attached to a list, otherwise\nthe given item.\n\n#### `Item#detach()`\n\n```js\nconst item = new Item()\nconst list = new List(item)\n\nconsole.log(item.list === list) // =\u003e true\n\nitem.detach()\nconsole.log(item.list === null) // =\u003e true\n```\n\nRemove the operated on item from its parent list.\n\nRemoves references to it on its parent `list`, and `prev` and `next` items.\nRelinks all references.\nReturns the operated on item.\nEven when it was already detached.\n\n#### `Item#next`\n\n```js\nconst item1 = new Item()\nconst item2 = new Item()\n\nconst list = new List(item1)\n\nconsole.log(item1.next === null) // =\u003e true\nconsole.log(item2.next === null) // =\u003e true\n\nitem1.append(item2)\n\nconsole.log(item1.next === item2) // =\u003e true\n\nitem1.detach()\n\nconsole.log(item1.next === null) // =\u003e true\n```\n\nThe following item or `null` otherwise.\n\n#### `Item#prev`\n\n```js\nconst item1 = new Item()\nconst item2 = new Item()\n\nconst list = new List(item1)\n\nconsole.log(item1.prev === null) // =\u003e true\nconsole.log(item2.prev === null) // =\u003e true\n\nitem1.append(item2)\n\nconsole.log(item2.prev === item1) // =\u003e true\n\nitem2.detach()\n\nconsole.log(item2.prev === null) // =\u003e true\n```\n\nThe preceding item or `null` otherwise.\n\n#### `Item#list`\n\n```js\nconst item = new Item()\nconst list = new List()\n\nconsole.log(item.list === null) // =\u003e true\n\nlist.append(item)\n\nconsole.log(item.list === list) // =\u003e true\n\nitem.detach()\n\nconsole.log(item.list === null) // =\u003e true\n```\n\nThe list this item belongs to or `null` otherwise.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports no additional types.\n\n## Compatibility\n\nThis package is at least compatible with all maintained versions of Node.js.\nAs of now, that is Node.js 14.14+ and 16.0+.\nIt also works in Deno and modern browsers.\n\n## Security\n\nThis package is safe.\n\n## Contribute\n\nYes please!\nSee [How to Contribute to Open Source][contribute].\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/wooorm/linked-list/workflows/main/badge.svg\n\n[build]: https://github.com/wooorm/linked-list/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/wooorm/linked-list.svg\n\n[coverage]: https://codecov.io/github/wooorm/linked-list\n\n[downloads-badge]: https://img.shields.io/npm/dm/linked-list.svg\n\n[downloads]: https://www.npmjs.com/package/linked-list\n\n[size-badge]: https://img.shields.io/bundlephobia/minzip/linked-list.svg\n\n[size]: https://bundlephobia.com/result?p=linked-list\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esmsh]: https://esm.sh\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[typescript]: https://www.typescriptlang.org\n\n[contribute]: https://opensource.guide/how-to-contribute/\n\n[wiki]: https://wikipedia.org/wiki/Linked_list\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwooorm%2Flinked-list","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwooorm%2Flinked-list","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwooorm%2Flinked-list/lists"}