1.1.6 • Published 2 years ago

@cwola/limited-size-stack v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

      NameTypeDescription
      capacitynumberThe maximum capacity of the stack.If you specify a negative number, the capacity is unlimited.
    • Return

      TypeDescription
      StackStack instance.
  • push


    • Description

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

    • Arguments

      NameTypeDescription
      ...elementsany[]input elements.
    • Return

      TypeDescription
      numberThe 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
      indexnumberThe index (position) of the stack element to be returned.
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
      directionROTATEROTATE.RIGHT or ROTATE.LEFT
      nnumbernumber of elements to rotateoptional default : 0
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      anyReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      numberNumber 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

      NameTypeDescription
    • Return

      TypeDescription
      BooleanReturns 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

      NameTypeDescription
    • Return

      TypeDescription
      thisthis.
    • 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

      NameTypeDescription
    • Return

      TypeDescription
      Stacknew 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

      NameTypeDescription
    • Return

      TypeDescription
      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

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago