{"id":20745312,"url":"https://github.com/klaudiosinani/shtack","last_synced_at":"2025-04-24T06:08:17.540Z","repository":{"id":57359027,"uuid":"194764339","full_name":"klaudiosinani/shtack","owner":"klaudiosinani","description":"LIFO Stacks for ES6","archived":false,"fork":false,"pushed_at":"2019-08-21T21:05:49.000Z","size":105,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T06:08:08.221Z","etag":null,"topics":["data","es6","lifo","stack","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}},"created_at":"2019-07-02T01:12:01.000Z","updated_at":"2022-10-20T13:00:02.000Z","dependencies_parsed_at":"2022-09-06T22:21:43.465Z","dependency_job_id":null,"html_url":"https://github.com/klaudiosinani/shtack","commit_stats":null,"previous_names":["klaussinani/shtack"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fshtack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fshtack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fshtack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klaudiosinani%2Fshtack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klaudiosinani","download_url":"https://codeload.github.com/klaudiosinani/shtack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250573351,"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","lifo","stack","structure","typescript"],"created_at":"2024-11-17T07:19:42.886Z","updated_at":"2025-04-24T06:08:17.523Z","avatar_url":"https://github.com/klaudiosinani.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  Shtack\n\u003c/h1\u003e\n\n\u003ch4 align=\"center\"\u003e\n  LIFO Stacks for ES6\n\u003c/h4\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://travis-ci.com/klaussinani/shtack\"\u003e\n    \u003cimg alt=\"Build Status\" src=\"https://travis-ci.com/klaussinani/shtack.svg?branch=master\"\u003e\n  \u003c/a\u003e\n  \u003ca href='https://coveralls.io/github/klaussinani/shtack?branch=master'\u003e\n    \u003cimg alt=\"Coverage Status\" src=\"https://coveralls.io/repos/github/klaussinani/shtack/badge.svg?branch=master\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nES6 implementation of the stack data structure with TypeScript support.\n\nCome over to [Twitter](https://twitter.com/klaussinani) to share your thoughts on the project.\n\nVisit the [contributing guidelines](https://github.com/klaussinani/shtack/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- [API](#api)\n- [Development](#development)\n- [Related](#related)\n- [Team](#team)\n- [License](#license)\n\n## Install\n\n### Yarn\n\n```bash\nyarn add shtack\n```\n\n### NPM\n\n```bash\nnpm install shtack\n```\n\n## In Depth\n\nA stack is an data structure that serves as a collection of elements, with two principal operations:\n\n- `push` which adds an element to the collection, and\n- `pop` which removes the most recently added element that was not yet removed.\n\nAdditionally, a `peek` operation gives access to the top element without mutating the stack. The order in which elements come off a stack gives rise to its alternative name, `LIFO` which stands for last in, first out. Shtack stacks are implemented using a linear singly linked list as their backbone, since stacks are linear data structures, or more abstractly sequential collections, where the `push` and `pop` operations occur only at one end of the structure, referred to as the `top` of the stack, which internally corresponds to the `head` node of the singly linked list.\n\n## Usage\n\nShtack 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/shtack/tree/master/test) directory.\n\n```js\n'use strict';\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n//=\u003e Stack { head: null, size: 0 }\n\nstack.isEmpty();\n//=\u003e true\n\nstack.push(10);\n//=\u003e Stack { head: Item { value: 10, next: null }, size: 1 }\n\nstack.isEmpty();\n//=\u003e false\n\nstack.peek();\n//=\u003e 10\n\nstack\n  .push(20)\n  .push(30)\n  .push(40)\n  .push(50);\n//=\u003e Stack { head:\n// Item { value: 50, next:\n// Item { value: 40, next:\n// Item { value: 30, next:\n// Item { value: 20, next: \n// Item { value: 10, next: null } } } } }, size: 5 }\n\nstack.includes(30);\n//=\u003e true\n\nstack.includes(60);\n//=\u003e false\n\nstack.pop();\n//=\u003e 50\n\nstack.peek();\n//=\u003e 40\n\nstack.toArray();\n//=\u003e [ 40, 30, 20, 10 ]\n\nstack.rotateRight(3);\n//=\u003e Stack { head:\n// Item { value: 10, next:\n// Item { value: 40, next:\n// Item { value: 30, next:\n// Item { value: 20, next: null } } } }, size: 4 }\n\nstack.toArray();\n//=\u003e [ 10, 40, 30, 20 ]\n\nstack.rotateLeft(1);\n//=\u003e Stack { head:\n// Item { value: 20, next:\n// Item { value: 10, next:\n// Item { value: 40, next:\n// Item { value: 30, next: null } } } }, size: 4 }\n\nstack.toArray();\n//=\u003e [ 20, 10, 40, 30 ]\n\nstack\n  .swap()\n  .duplicate()\n  .toArray();\n//=\u003e [ 10, 10, 20, 40, 30 ]\n\nstack.reverse().toArray();\n//=\u003e [30, 40, 20, 10, 10]\n```\n\n## API\n\n#### stack.`size`\n\n- Return Type: `Number`\n\nReturns the total number of values in the stack.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.size;\n//=\u003e 3\n```\n\n#### stack.`clear()`\n\n- Return Type: `Stack`\n\nMutates the stack by removing all residing values and returns it empty.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.size;\n//=\u003e 3\nstack.clear();\n//=\u003e Stack { head: null, size: 0 }\nstack.size;\n//=\u003e 0\n```\n\n#### stack.`duplicate()`\n\n- Return Type: `Stack`\n\nMutates the stack by removing the top-most value, and then pushing it twice, so that an additional copy of the former top-most value is now on the top, with the original below it. Returns the stack itself.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.duplicate();\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } } }, size: 4 }\n```\n\n#### stack.`forEach(fn)`\n\n- Return Type: `Stack`\n\nTraverses the stack, top to bottom, and executes the provided `fn` function once for each traversed element without mutating the stack. Returns the stack 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 {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30)\n  .push(40);\n//=\u003e Stack { head:\n// Item { value: 40, next:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } } }, size: 4 }\nstack.forEach(console.log);\n//=\u003e 40\n// 30\n// 20\n// 10\n```\n\n#### stack.`includes(value)`\n\n- Return Type: `Boolean`\n\nDetermines whether the stack 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 {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.includes(10);\n//=\u003e true\nstack.includes(40);\n//=\u003e false\nstack.includes(20);\n//=\u003e true\n```\n\n#### stack.`isEmpty()`\n\n- Return Type: `Boolean`\n\nDetermines whether the stack is empty, returning `true` or `false` as appropriate.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack.push(10);\n//=\u003e Stack { head: Item { value: 10, next: null }, size: 1 }\nstack.isEmpty();\n//=\u003e false\nstack.clear().isEmpty();\n//=\u003e true\n```\n\n#### stack.`peek()`\n\n- Return Type: `Any | undefined`\n\nReturns the top-most value of the stack, without mutating the stack itself.\nIf the stack is empty `undefined` is returned.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack.push(10);\n//=\u003e Stack { head: Item { value: 10, next: null }, size: 1 }\nstack.peek();\n//=\u003e 10\n```\n\n#### stack.`pop()`\n\n- Return Type: `Any | undefined`\n\nMutates the stack by removing and returning the top-most value.\nIf the stack is empty `undefined` is returned.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack.push(10);\n//=\u003e Stack { head: Item { value: 10, next: null }, size: 1 }\nstack.pop();\n//=\u003e 10\nstack.pop();\n//=\u003e undefined\n```\n\n#### stack.`push(value)`\n\n- Return Type: `Stack`\n\nMutates the stack by inserting a new value at the top. Returns the stack itself.\n\n##### **`value`**\n\n- Type: `Any`\n\nValue to insert.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack.push(10);\n//=\u003e Stack { head: Item { value: 10, next: null }, size: 1 }\nstack.push(20).push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.size;\n//=\u003e 3\n```\n\n#### stack.`reverse()`\n\n- Return Type: `Stack`\n\nMutates the stack by reversing in-place the contained values. The top-most value becomes the bottom-most one, and the bottom-most one becomes the top-most. Returns the stack itself.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.reverse();\n//=\u003e Stack { head:\n// Item { value: 10, next:\n// Item { value: 20, next:\n// Item { value: 30, next: null } } }, size: 3 }\n```\n\n#### stack.`rotateLeft(n)`\n\n- Return Type: `Stack`\n\nMutates the stack by moving the `n` bottom-most values to the top in a rotating fashion. Returns the stack itself.\n\n##### **`n`**\n\n- Type: `Number`\n\nNumber of bottom-most values to be rotated.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30)\n  .push(40)\n  .push(50);\n//=\u003e Stack { head:\n// Item { value: 50, next:\n// Item { value: 40, next:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } } } }, size: 5 }\nstack.toArray();\n//=\u003e [ 50, 40, 30, 20, 10 ]\nstack.rotateLeft(2);\n//=\u003e Stack { head:\n// Item { value: 20, next:\n// Item { value: 10, next:\n// Item { value: 50, next:\n// Item { value: 40, next:\n// Item { value: 30, next: null } } } } }, size: 5 }\nstack.toArray();\n//=\u003e [ 20, 10, 50, 40, 30 ]\n```\n\n#### stack.`rotateRight(n)`\n\n- Return Type: `Stack`\n\nMutates the stack by moving the `n` top-most values to the bottom in a rotating fashion. Returns the stack itself.\n\n##### **`n`**\n\n- Type: `Number`\n\nNumber of top-most values to be rotated.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30)\n  .push(40)\n  .push(50);\n//=\u003e Stack { head:\n// Item { value: 50, next:\n// Item { value: 40, next:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } } } }, size: 5 }\nstack.toArray();\n//=\u003e [ 50, 40, 30, 20, 10 ]\nstack.rotateRight(2);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next:\n// Item { value: 50, next:\n// Item { value: 40, next: null } } } } }, size: 5 }\nstack.toArray();\n//=\u003e [ 30, 20, 10, 50, 40 ]\n```\n\n#### stack.`swap()`\n\n- Return Type: `Stack`\n\nMutates the stack by exchanging the positions of the two top-most values. Returns the stack itself.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30);\n//=\u003e Stack { head:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.toArray();\n//=\u003e [ 30, 20, 10 ]\nstack.swap();\n//=\u003e Stack { head:\n// Item { value: 20, next:\n// Item { value: 30, next:\n// Item { value: 10, next: null } } }, size: 3 }\nstack.toArray();\n//=\u003e [ 20, 30, 10 ]\n```\n\n#### stack.`toArray()`\n\n- Return Type: `Array\u003cAny\u003e`\n\nTraverses the stack, from top to bottom, and stores each traversed value in an array. The array is returned at the end of the traversal.\n\n```js\nconst {Stack} = require('shtack');\n\nconst stack = new Stack();\n\nstack\n  .push(10)\n  .push(20)\n  .push(30)\n  .push(40)\n  .push(50);\n//=\u003e Stack { head:\n// Item { value: 50, next:\n// Item { value: 40, next:\n// Item { value: 30, next:\n// Item { value: 20, next:\n// Item { value: 10, next: null } } } } }, size: 5 }\nstack.toArray();\n//=\u003e [ 50, 40, 30, 20, 10 ]\n```\n\n## Development\n\nFor more info on how to contribute to the project, please read the [contributing guidelines](https://github.com/klaussinani/shtack/blob/master/contributing.md).\n\n- Fork the repository and clone it to your machine\n- Navigate to your local fork: `cd shtack`\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- [kiu](https://github.com/klaussinani/kiu) - FIFO Queues 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- [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/shtack/blob/master/license.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fshtack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklaudiosinani%2Fshtack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklaudiosinani%2Fshtack/lists"}