npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cwola/limited-size-stack

v1.1.6

Published

Providing limited size stack for JS (& TypeScript).

Downloads

23

Readme

limited-size-stack

Providing limited size stack for JS (& TypeScript).

Overview

Providing limited size stack for JS (& TypeScript).

Installation

$ npm i --save @cwola/limited-size-stack

Usage

import {
    Stack,
    ROTATE
} from '@cwola/limited-size-stack';

const STACK = new Stack(/* capacity = */ 5);

STACK.push('one', 'two', 'three', 'four', 'five');
// STACK : ['one', 'two', 'three', 'four', 'five']

STACK.push('six');
// STACK : ['two', 'three', 'four', 'five', 'six']

console.log(STACK.top());
// output : 'six'

STACK.rotate(ROTATE.RIGHT);
// STACK : ['six', 'two', 'three', 'four', 'five']

STACK.dup();
// STACK : ['two', 'three', 'four', 'five', 'five']

Method

  • constructor


    • Description

      Constructor

    • Arguments

      | Name | Type | Description | |---|:---:|---| | capacity | number | The maximum capacity of the stack.If you specify a negative number, the capacity is unlimited. |

    • Return

      | Type | Description | |:---:|---| | Stack | Stack instance. |

  • push


    • Description

      Push one or more elements onto the end of stack, removing the oldest element if the stack is full.

    • Arguments

      | Name | Type | Description | |---|:---:|---| | ...elements | any[] | input elements. |

    • Return

      | Type | Description | |:---:|---| | number | The new number of elements in the stack. |

    • example

      const stack = new Stack(3);
      
      stack.push('one', 'two');  // returns 2
      // stack : ['one', 'two']
      
      stack.push('three');  // returns 3
      // stack : ['one', 'two', 'three']
      
      stack.push('four');  // returns 3
      // stack : ['two', 'three', 'four']
  • pop


    • Description

      Pop the element off the end of stack.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the last element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      tack.pop();  // returns 'three'
      // stack : ['one', 'two']
  • shift


    • Description

      Shift an element off the beginning of stack.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the shifted element.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.shift();  // returns 'one'
      // stack : ['two', 'three']
  • top


    • Description

      Returns the last element of stack. The topmost element is returned, but the stack size does not change (meaning the element remains on the stack).

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the last element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.top();  // returns 'three'
      // stack : ['one', 'two', 'three']
  • bottom


    • Description

      Returns the first element of stack. The bottom element is returned, but the stack size does not change (meaning the element remains on the stack).

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the first element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.bottom();  // returns 'one'
      // stack : ['one', 'two', 'three']
  • at


    • Description

      Takes an integer value and returns the element at that index. Supports relative indexing from the end of the stack when passed a negative index. i.e. if a negative number is used, the element returned will be found by counting back from the end of the stack.

    • Arguments

      | Name | Type | Description | |---|:---:|---| | index | number | The index (position) of the stack element to be returned. |

    • Return

      | Type | Description | |:---:|---| | any | Returns the element in the stack matching the given index.If stack is empty or index is out of bounds, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.at(1);  // returns 'two'
      // stack : ['one', 'two', 'three']
      
      stack.at(-1);  // returns 'three'
      // stack : ['one', 'two', 'three']
  • dup


    • Description

      The top element is popped, and then pushed again (twice). so that an additional copy of the former top element is now on top, with the original below it.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the last element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.dup();  // returns 'three'
      // stack : ['two', 'three', 'three']
  • swap


    • Description

      Swap the two positions of the topmost elements on the stack.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the last element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.swap();  // returns 'two'
      // stack : ['one', 'three', 'two']
  • rotate


    • Description

      Rotate the topmost n items to move on the stack. The n topmost items are moved on the stack in a rotating fashion. 'n' is the number of elements to rotate (but if you specify '0' or negative number, the target will be all elements). so, if you specify '2', it becomes 'swap'. Two variants of this operation are possible, left rotate and right rotate.

    • Arguments

      | Name | Type | Description | |---|:---:|---| | direction | ROTATE | ROTATE.RIGHT or ROTATE.LEFT | | n | number | number of elements to rotate[optional] default : 0 |

    • Return

      | Type | Description | |:---:|---| | any | Returns the last element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.rotate(ROTATE.RIGHT);  // returns 'two'
      // stack : ['three', 'one', 'two']
      
      stack.rotate(ROTATE.LEFT);  // returns 'three'
      // stack : ['one', 'two', 'three']
  • reverse


    • Description

      Reverse the order of the elements on stack.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any | Returns the last element of stack.If stack is empty, 'undefined' will be returned. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.reverse();  // returns 'one'
      // stack : ['three', 'two', 'one']
  • size


    • Description

      Get number of elements.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | number | Number of elements. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.size();  // returns 3
      // stack : ['one', 'two', 'three']
  • isEmpty


    • Description

      Returns True if the stack is empty, false otherwise.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | Boolean | Returns True if the stack is empty, false otherwise. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.isEmpty();  // returns False
      // stack : ['one', 'two', 'three']
      
      stack.clear();
      stack.isEmpty;  // returns True
      // stack : []
  • clear


    • Description

      Clear all elements.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | this | this. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      stack.clear();  // returns stack
      // stack : []
  • clone


    • Description

      Clone this stack. The stack property is shallow-copied.

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | Stack | new Stack instance. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      const copied = stack.clone();
      stack.push('four');
      // stack : ['two', 'three', 'four']
      // copied : ['one', 'two', 'three']
  • toArray


    • Description

      To array (shallow-copied).

    • Arguments

      | Name | Type | Description | |---|:---:|---|

    • Return

      | Type | Description | |:---:|---| | any[] | array. |

    • example

      const stack = new Stack(3);
      stack.push('one', 'two', 'three');
      // stack : ['one', 'two', 'three']
      
      const arr = stack.toArray();
      stack.push('four');
      // stack : ['two', 'three', 'four']
      // arr : ['one', 'two', 'three']

License

MIT