├── .gitignore ├── index.d.ts ├── index.js ├── README.md ├── .npmignore ├── .travis.yml ├── src ├── stack.d.ts └── stack.js ├── .eslintrc ├── Gruntfile.js ├── package.json ├── LICENSE ├── CHANGELOG.md └── test └── stack.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | .DS_Store -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import { Stack } from './src/stack'; 2 | 3 | export { Stack } 4 | 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { Stack } = require('./src/stack'); 2 | 3 | exports.Stack = Stack; 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @datastructures-js/stack 2 | 3 | ## Docs 4 | 5 | https://datastructures-js.info/docs/stack 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .git* 2 | .travis.yml 3 | .gitignore 4 | .eslintrc 5 | Gruntfile.js 6 | coverage/ 7 | node_modules/ 8 | test/ 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8" 4 | - "9" 5 | - "10" 6 | - "11" 7 | - "12" 8 | install: 9 | - npm install -g grunt-cli 10 | - npm install 11 | script: 12 | - grunt build 13 | -------------------------------------------------------------------------------- /src/stack.d.ts: -------------------------------------------------------------------------------- 1 | export class Stack { 2 | constructor(elements?: T[]); 3 | isEmpty(): boolean; 4 | size(): number; 5 | peek(): T | null; 6 | push(element: T): Stack; 7 | pop(): T | null; 8 | toArray(): T[]; 9 | clear(): void; 10 | clone(): Stack; 11 | static fromArray(elements: T[]): Stack; 12 | } 13 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "max-len": ["error", { "code": 80, "ignoreComments": true }], 4 | "comma-dangle": ["error", { 5 | "functions": "ignore" 6 | }], 7 | "no-underscore-dangle": [ 8 | "error", 9 | { "allowAfterThis": true } 10 | ] 11 | }, 12 | "env": { 13 | "mocha": true, 14 | "node": true 15 | }, 16 | "extends": ["airbnb-base"] 17 | } 18 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = (grunt) => { 2 | grunt.initConfig({ 3 | eslint: { 4 | src: ['src/*.js', 'test/*.test.js'] 5 | }, 6 | mochaTest: { 7 | files: ['test/*.test.js'] 8 | }, 9 | mocha_istanbul: { 10 | coverage: { 11 | src: 'test', 12 | options: { 13 | mask: '*.test.js' 14 | } 15 | } 16 | } 17 | }); 18 | 19 | grunt.loadNpmTasks('grunt-eslint'); 20 | grunt.loadNpmTasks('grunt-mocha-test'); 21 | grunt.loadNpmTasks('grunt-mocha-istanbul'); 22 | 23 | grunt.registerTask('lint', ['eslint']); 24 | grunt.registerTask('test', ['mochaTest']); 25 | grunt.registerTask('coverage', ['mocha_istanbul']); 26 | grunt.registerTask('build', ['lint', 'coverage']); 27 | }; 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@datastructures-js/stack", 3 | "version": "3.1.6", 4 | "description": "stack implementation in javascript", 5 | "main": "index.js", 6 | "types": "index.d.ts", 7 | "scripts": { 8 | "test": "grunt test" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/datastructures-js/stack.git" 13 | }, 14 | "keywords": [ 15 | "stack", 16 | "stack es6", 17 | "stack js" 18 | ], 19 | "author": "Eyas Ranjous ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/datastructures-js/stack/issues" 23 | }, 24 | "homepage": "https://github.com/datastructures-js/stack#readme", 25 | "devDependencies": { 26 | "chai": "^4.2.0", 27 | "eslint": "^6.8.0", 28 | "eslint-config-airbnb-base": "^14.0.0", 29 | "eslint-plugin-import": "^2.19.1", 30 | "grunt": "^1.0.4", 31 | "grunt-eslint": "^22.0.0", 32 | "grunt-mocha-istanbul": "^5.0.2", 33 | "grunt-mocha-test": "^0.13.3", 34 | "istanbul": "^0.4.5", 35 | "mocha": "^6.2.2" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Eyas Ranjous 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [Unreleased] 8 | ## [3.1.6] - 2025-09-08 9 | ### Fixed 10 | - return null in type definition for peek & pop functions. 11 | 12 | ## [3.1.5] - 2025-08-26 13 | ### Fixed 14 | - README 15 | 16 | ## [3.1.4] - 2022-08-15 17 | ### Fixed 18 | - add types to package.json 19 | 20 | ## [3.1.3] - 2022-05-15 21 | ### Fixed 22 | - README 23 | 24 | ## [3.1.2] - 2021-06-20 25 | 26 | ### Fixed 27 | - index.d.ts 28 | 29 | ## [3.1.1] - 2021-06-14 30 | 31 | ### Fixed 32 | - README 33 | 34 | ## [3.1.0] - 2021-06-13 35 | 36 | ### Added 37 | - typescript. 38 | 39 | ## [3.0.0] - 2021-01-02 40 | 41 | ### Changed 42 | - `.push` can now chain other stack methods. 43 | - named export for stack class. 44 | 45 | ### Fixed 46 | - readme 47 | 48 | ## [2.0.4] - 2020-12-26 49 | ### Fixed 50 | - jsdoc 51 | - readme 52 | 53 | ## [2.0.3] - 2020-04-03 54 | ### Fixed 55 | - .npmignore 56 | 57 | ## [2.0.2] - 2020-04-02 58 | ### Fixed 59 | - README & jsdoc. 60 | 61 | ## [2.0.1] - 2020-03-12 62 | ### Fixed 63 | - Readme. 64 | 65 | ## [2.0.0] - 2020-03-12 66 | ### Changed 67 | - initial new release. 68 | -------------------------------------------------------------------------------- /test/stack.test.js: -------------------------------------------------------------------------------- 1 | const { expect } = require('chai'); 2 | const { Stack } = require('../src/stack'); 3 | 4 | describe('stack tests', () => { 5 | const stack = new Stack(); 6 | 7 | describe('.push(element)', () => { 8 | it('should push elements to the top of the stack', () => { 9 | stack.push(1); 10 | stack.push(2); 11 | stack.push('3th'); 12 | }); 13 | }); 14 | 15 | describe('.size()', () => { 16 | it('should have length of 3', () => { 17 | expect(stack.size()).to.equal(3); 18 | }); 19 | }); 20 | 21 | describe('.isEmpty()', () => { 22 | it('should not be empty', () => { 23 | expect(stack.isEmpty()).to.equal(false); 24 | }); 25 | }); 26 | 27 | describe('.peek()', () => { 28 | it('should peek the top element', () => { 29 | expect(stack.peek()).to.equal('3th'); 30 | }); 31 | }); 32 | 33 | describe('.toArray()', () => { 34 | it('returns an array copy', () => { 35 | expect(stack.toArray()).to.deep.equal([1, 2, '3th']); 36 | }); 37 | }); 38 | 39 | describe('.clone()', () => { 40 | it('clones the stack', () => { 41 | const clone = stack.clone(); 42 | clone.pop(); 43 | 44 | expect(stack.peek()).to.equal('3th'); 45 | expect(clone.peek()).to.equal(2); 46 | }); 47 | }); 48 | 49 | describe('.pop()', () => { 50 | it('should pop the elements', () => { 51 | expect(stack.pop()).to.equal('3th'); 52 | expect(stack.pop()).to.equal(2); 53 | }); 54 | }); 55 | 56 | describe('.clear()', () => { 57 | it('should clear the stack', () => { 58 | stack.clear(); 59 | expect(stack.pop()).to.equal(null); 60 | expect(stack.peek()).to.equal(null); 61 | expect(stack.size()).to.equal(0); 62 | expect(stack.isEmpty()).to.equal(true); 63 | }); 64 | }); 65 | 66 | describe('Stack.fromArray(list)', () => { 67 | it('creates a stack from an existing array', () => { 68 | const s = Stack.fromArray([1, 2, 3]); 69 | expect(s.peek()).to.equal(3); 70 | expect(s.size()).to.equal(3); 71 | }); 72 | }); 73 | }); 74 | -------------------------------------------------------------------------------- /src/stack.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @license MIT 3 | * @copyright 2020 Eyas Ranjous 4 | * 5 | * @class 6 | */ 7 | class Stack { 8 | /** 9 | * Creates a stack. 10 | * @param {array} [elements] 11 | */ 12 | constructor(elements) { 13 | this._elements = Array.isArray(elements) ? elements : []; 14 | } 15 | 16 | /** 17 | * Checks if the stack is empty. 18 | * @public 19 | * @returns {boolean} 20 | */ 21 | isEmpty() { 22 | return this._elements.length === 0; 23 | } 24 | 25 | /** 26 | * Returns the number of elements in the stack. 27 | * @public 28 | * @returns {number} 29 | */ 30 | size() { 31 | return this._elements.length; 32 | } 33 | 34 | /** 35 | * Returns the top element in the stack. 36 | * @public 37 | * @returns {number|string|object} 38 | */ 39 | peek() { 40 | if (this.isEmpty()) { 41 | return null; 42 | } 43 | 44 | return this._elements[this._elements.length - 1]; 45 | } 46 | 47 | /** 48 | * Adds an element to the top of the stack. 49 | * @public 50 | * @param {number|string|object} element 51 | */ 52 | push(element) { 53 | this._elements.push(element); 54 | return this; 55 | } 56 | 57 | /** 58 | * Removes and returns the top element in the stack. 59 | * @public 60 | * @returns {number|string|object} 61 | */ 62 | pop() { 63 | if (this.isEmpty()) { 64 | return null; 65 | } 66 | 67 | return this._elements.pop(); 68 | } 69 | 70 | /** 71 | * Returns the remaining elements as an array. 72 | * @public 73 | * @returns {array} 74 | */ 75 | toArray() { 76 | return this._elements.slice(); 77 | } 78 | 79 | /** 80 | * Clears all elements from the stack. 81 | * @public 82 | */ 83 | clear() { 84 | this._elements = []; 85 | } 86 | 87 | /** 88 | * Creates a shallow copy from the stack. 89 | * @public 90 | * @return {Stack} 91 | */ 92 | clone() { 93 | return new Stack(this._elements.slice()); 94 | } 95 | 96 | /** 97 | * Creates a stack from an existing array 98 | * @public 99 | * @static 100 | * @param {array} [elements] 101 | * @return {Stack} 102 | */ 103 | static fromArray(elements) { 104 | return new Stack(elements); 105 | } 106 | } 107 | 108 | exports.Stack = Stack; 109 | --------------------------------------------------------------------------------