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 🙏

© 2024 – Pkg Stats / Ryan Hefner

t-structs

v1.0.1

Published

A library consisting of standard data structures implemented using TypeScript.

Downloads

3

Readme

T-Structs

t-structs is a TypeScript library consisting of standard data structures provided through a simple API.

Getting Started

Installation

npm install t-structs

Usage

Example 1 - Using a stack of numbers.

import { Stack } from "t-structs";

/** Initialize a new empty stack. */
const stack = new Stack<number>();

/** Stack is empty */
stack.size; // returns 0.
stack.isEmpty; // returns true.

/** Add elements using push method. */
stack.push(25);

/** push method can accept one or more arguments. */
stack.push(10, 11);

/** pushAll method can be used to push multiple elements at once using a list of values. */
stack.pushAll([5, 6, 7]);

/** pop method removes a value from the top of the stack and returns it. */
stack.pop(); // returns 7

/** peek method returns the value at the top of the stack without removing it from the stack. */
stack.peek(); // returns 6

/** toArray method returns a list of all the values in the stack. */
stack.toArray(); // returns [6, 5, 11, 10, 25]

Example 2 - Using a Binary Search tree with a complex custom data type.

import { BinarySearchTree } from "t-structs";
import type { CompareFunc, EqualsFunc } from "t-structs";

type Vehicle = {
  brand: string;
  cost: number;
};

const vehicles: Vehicle[] = [
  {
    brand: "Tata",
    cost: 300,
  },
  {
    brand: "Tesla",
    cost: 800,
  },
  {
    brand: "Mercedes",
    cost: 550,
  },
  {
    brand: "BMW",
    cost: 620,
  },
];

/**
 * Comparison logic for our custom Vehicle type.
 * @param {Vehicle} a
 * @param {Vehicle} b
 * @returns {1 | -1 | 0}
 */
const compare: CompareFunc<Vehicle> = function (
  a: Vehicle,
  b: Vehicle
): 1 | -1 | 0 {
  if (+a.cost > +b.cost) return 1;
  else if (+a.cost < +b.cost) return -1;
  else return 0;
};

/**
 * Equality logic for our custom Vehicle type.
 * @param {Vehicle} a
 * @param {Vehicle} b
 * @returns {boolean}
 */
const equals: EqualsFunc<Vehicle> = function (a: Vehicle, b: Vehicle): boolean {
  return a.brand === b.brand && +a.cost === +b.cost;
};

/** Initialize an empty binarySearchtree using the custom equality and compare functions. */
const binarySearchTree = new BinarySearchTree<Vehicle>([], compare, equals);

/** Initially the tree is empty. */
binarySearchTree.isEmpty; // returns true.
binarySearchTree.height; // returns 0.

/** Insert an element into the tree using insert method. */
binarySearchTree.insert({
  brand: "Porsche",
  cost: 750,
});

/** Insert method is variadic and can accept one or more arguments. */
binarySearchTree.insert(
  {
    brand: "Jeep",
    cost: 600,
  },
  {
    brand: "Toyota",
    cost: 400,
  }
);

/** Insert all method can be used to add multiple elements by passing a list as an argument. */
binarySearchTree.insertAll(vehicles);

/** has method can be used to check if a value exists in the tree. */
binarySearchTree.has({
  brand: "Mercedes",
  cost: 550,
}); // returns true.

binarySearchTree.has({
  brand: "Lamborghini",
  cost: "999",
}); // returns false.

/** remove method can be used to remove a node from the tree. */
binarySearchTree.remove({
  brand: "Mercedes",
  cost: 550,
}); // returns { brand: "Mercedes", cost: 550 } (the removed node).

/** traverse method can be used to traverse the tree. The default traversal is in_order traversal */
console.log(binarySearchTree.traverse()); /**
    Same as binarySearchTree.traverse("IN_ORDER").
    returns [
              { brand: 'Tata', cost: 300 },
              { brand: 'Toyota', cost: 400 },
              { brand: 'Jeep', cost: 600 },
              { brand: 'BMW', cost: 620 },
              { brand: 'Porsche', cost: 750 },
              { brand: 'Tesla', cost: 800 }
            ] (the in-order traversal)
*/

API Reference

Visit the t-structs reference documentation page for a more comprehensive coverage of the API.

License

t-structs is offered under the MIT License.

Note

Please note that although the library has been effectively tested, the author does not provide any correctness or performance guarantees and assurances. Feel free to incorporate and use this library in any way by your own volition.