0.9.3 ā€¢ Published 1 year ago

typed-stack v0.9.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

typed-stack

Version npm

Javascript/Typescript implementation of the stack data-structure.

Homepage

Author

šŸ‘¤ chrisitopherus


Table of Contents

Installing

npm install typed-stack

Code Documentation

See /docs/index.html to view a documentation generated by TYPEDOC. You can also checkout the documentation generated in Markdown at /docs/md


Getting Started

Note: The Guide will be written in Typescript, but no worries besides generics its exactly the same for JS.

The package comes with...

2 classes:

  • Stack - The Stack implementation
  • InjectableStack - Same as Stack but with optional dependency injection for the cloning process

1 utility function:

  • deepSimpleClone - Recursively clones object literals and arrays

For TS only...

2 interfaces:

  • IStack - Represents a Stack.
  • ISearchable<T> - Represents a structure than can be searched.

And 2 type aliases:

  • SearchFn<T> - Represents a generic function to be used for searching.
  • CloneFn<T> - Defines the structure of a clone function.

Creating a new Stack

First of all, we have to import the class

import { Stack } from "typed-stack";

Now we can either create an empty Stack

const stack = new Stack<number>();

or initialize it with a few values.

const stack = new Stack([1, 2, 3, 4]);

It is adviced to manually set the type of the Stack when not initializing it with values.

For JS a jsdoc comment could be helpful:

/**
 * @type {Stack<number>}
 */
const stack = new Stack();

Note: A list of all methods and properties can be found in the docs.

Usage examples


Calculation with RPN

import { Stack } from "typed-stack";

const rpn = [ 1.3, 4, '+', 3, 2.5454545545, '^', '*', 4323, '-' ];
const stack = new Stack<number>();

for (const element of rpn) {
    if (typeof element === "number") {
        stack.push(element);
        continue;
    }

    const number2 = stack.pop() as number;
    const number1 = stack.pop() as number;
    const operation = MATH_OPERATIONS.find(
        (operation) => operation.type === element);
    stack.push(operation.execute(number1, number2));
}

const result = stack.pop(); // -4236.150696387356

Image Carousel

import { Stack, IStack } from "typed-stack";

class Carousel<T> {
    private _storage: IStack<T>;
    constructor(storage: IStack<T>) {
        this._storage = storage;
    }

    get current() {
        return this._storage.peek();
    }

    add(entry: T) {
        this._storage.push(entry);
        return this;
    }

    next() {
        this._storage.moveUp();
        return this._storage.peek();
    }

    prev() {
        this._storage.moveDown();
        return this._storage.peek();
    }
}

const imageCarousel = new Carousel(new Stack<string>());
imageCarousel
    .add("./assets/mountain.png")
    .add("./assets/sea.png")
    .add("./assets/house.png");
imageCarousel.current; // ./assets/house.png
imageCarousel.next(); // ./assets/sea.png
imageCarousel.next(); // ./assets/mountain.png
imageCarousel.next(); // ./assets/house.png

Feature requests are welcome


License

MIT

0.9.3

1 year ago

0.9.2

1 year ago

0.9.1

1 year ago

0.9.0

1 year ago

2.0.0

1 year ago

1.0.0

1 year ago