1.1.2 • Published 4 years ago

js-dast v1.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
4 years ago

js-dast

Implementation of useful data-structures in vanilla JavaScript.

Linked list, stack, queue, sorted array and type safe extensions for these collections, plus array and set.

See the package source on GitHub for more details.

Refer to js-dast documentation for more information.

Installation

npm install js-dast

Importing

import {
    LinkedList,
    TypeSafeLinkedList,
    Queue,
    TypeSafeQueue,
    Stack,
    TypeSafeStack,
    TypeSafeSet,
    TypeSafeArray,
    TypeSafeSortedArray,
    TYPE,
    COMPARE,
    getType,
    getArrayType,
    isType
} from "js-dast"; // ES6 import

import * as dast from "js-dast" // ES6 import all as alias

Usage

import {
    LinkedList,
    TypeSafeLinkedList,
    Queue,
    TypeSafeQueue,
    Stack,
    TypeSafeStack,
    TypeSafeSet,
    TypeSafeArray,
    TypeSafeSortedArray,
    TYPE,
    COMPARE,
    getType,
    getArrayType,
    isType
} from "js-dast";

/**
 * Create a collection
 */
// Create a new collection with constructor.
new Queue("foo", "bar", "baz");
// Or use static factory methods {from, of}.
Stack.from(["foo", "bar", "baz"]);

/**
 * Create a type safe collection
 */
// Create a new type safe collection with constructor, and {TYPE} constant to set the collection type.
new TypeSafeLinkedList(TYPE.STRING, "foo", "bar");
// Or use {getType} function to get the type of an element.
new TypeSafeSet(getType("foo"), "foo", "bar");
// Or use type safe static factory methods {from, of}, that will figure the elements type automatically.
TypeSafeArray.of("foo", "bar");
// This will throw an Error, as not all elements are of the same type.
TypeSafeQueue.from(["foo", "bar", 0, 1]);

/**
 * Type helper functions
 */
// {TYPE} constant stores common types references.
getType("foo"); // Gets an element type.
getArrayType(["foo", "bar", "baz"]); // Gets an array type.
getArrayType(["foo", "bar", 1, 2, 3]); // Will result in {undefined}.
isType("foo", TYPE.STRING); // Checks if element is of specific type.

/**
 * Using compare functions and sorted array
 */
// {COMPARE} constant stores common compare functions references.
// Set a compare function to a sorted array, upon initialization, using {COMPARE} constant.
new TypeSafeSortedArray(TYPE.NUMBER, COMPARE.NUMBER_DESCENDING, 1, 3, 2, 8, 5)
// Or sort any array or list using this constant.
const list = new LinkedList(1, 3, 2, 8, 5);
list.sort(COMPARE.NUMBER);
// Or set a compare function of your own.
const compare = (a, b) => a.name.localeCompare(b.name);
const sortedArray = new TypeSafeSortedArray(TYPE.OBJECT, compare, {name: "Foo", age: 33}, {name: "Bar", age: 32});
// Sorted array compare function could be reset, resulting in resorting the array.
const newCompare = (a, b) => a.age - b.age;
sortedArray.setCompareFunction(newCompare);

/**
 * Iterate over collections
 */
// All implemented collections are iterable. Either with {for ... of} loops.
const stack = new Stack(5, 4, 3, 2, 1);
for (const entry of stack) {
    console.log(entry, "is an entry in this Stack");
}
// Or with {forEach} function.
stack.forEach((entry, index) =>
    console.log(entry, "is in index", index)
);

/**
 * Array.prototype methods
 */
// Type safe array: All Array.prototype methods are implemented, some with type safe validations.
const typedArray = TypeSafeArray.from([1, 2, 3, 4, 5]);
// This will work without any issues, as nothing is mutating the array elements.
typedArray.every(
    entry => isType(entry, TYPE.NUMBER) // true
);
// This will check if the pushed items are of type Number, and then push it to the array.
typedArray.push(6, 7, 8);
// This will throw an Error, as "foo" is of type String and not Number.
typedArray.push("foo", "bar");
// Sorted array: All non-mutating Array.prototype methods are implemented.
// This will work without any issues, as nothing is mutating the array elements.
sortedArray.filter(
    entry => entry > 3
);
// This will throw an Error, as {push} is not a method in sorted array.
sortedArray.push(0, 10);
// Instead use {add} method.
sortedArray.add(0, 10);