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

typed-structures

v1.0.5

Published

Common typed structures implementations in TypeScript

Downloads

289

Readme

Typed Structures is an MIT-licensed on-going open source project meant to bring a fullset of data structures to the awesome TypeScript 🎉

Motivation

With the rise in popularity of Typescript, we think that having the same kind of data structures as the standard libraries of widely used strongly typed languages would be helpful to anyone planning to implement complex features. We believe in the power of open-source and we want this project to be driven by the ones who need it.

How to install

npm install typed-structures

How to import

import { Map, Buffer, Set /* ... */ } from 'typed-structures';

Documentation 📝

Complete API available here.

Structures (Examples) 📕📗📘

Doubly linked list

Generic doubly linked list.

import { DoublyLinkedList, Node, Queue } from 'typed-structures';
import { Track } from './track';
import { MixService } from './mix-service';

class Player {
    private playlist: DoublyLinkedList<Track> = new DoublyLinkedList<Track>();
    private mix: Queue<Track> = MixService.getMix();
    private currentTrack: Node<Track>;
    
    constructor() {
        this.playlist.push(this.mix.dequeue());
        this.currentTrack = new Node<Track>(this.playlist.peek());
    }
    
    onNextClick() {
        this.next();
    }
    
    onPreviousClick() {
        this.previous();
    }
    
    onTrackEnd() {
        this.next();
    }
    
    private next() {
        this.playlist.push(this.mix.dequeue());
        this.currentTrack = this.currentTrack.next();
    }
    
    private previous() {
        this.currentTrack = this.currentTrack.previous();
    }
}

Map

Generic plain data map.

import { Map, Stack } from 'typed-structures';
import { Task } from "./task";

let map: Map<string, Stack<Task>> = new Map<string, Stack<Task>>();

map.put('todo', new Stack<Task>());
map.put('in-progress', new Stack<Task>());
map.put('done', new Stack<Task>());

let firstTask: Task = new Task('Read the docs');
let todo: Stack<Task> = map.get('todo');
todo.stack(firstTask);
map.replace('todo', todo);

let inProgress: Stack<Task> = map.get('in-progress');
inProgress.stack(map.get('todo').unstack());
map.replace('in-progress', inProgress);

let done: Stack<Task> = map.get('done');
done.stack(map.get('in-progress').unstack());
map.replace('done', done);

Set

Generic set.

import { Set } from 'typed-structures';

let set: Set<string> = new Set<string>();

set.add('first');
set.add('second');

console.log(set.contains('first')); // true

for (let key of set.keys())
    console.log(key); // first, second

Stack

Simple and generic stack.

import { Stack } from "typed-structures";
import { Task } from "./task";

let taskStack: Stack<Task> = new Stack<Task>();

taskStack.stack(new Task('taskName'));

console.log(taskStack.peek().name); // taskName

console.log(taskStack.empty()); // false

console.log(taskStack.length()); // 1

console.log(taskStack.unstack().name); //taskName

console.log(taskStack.empty()); // true

Live example on CodeSandbox

Queue

import { Queue } from 'typed-structures';
import { Emiter } from './emiter';
import { Logger } from './logger';

let emiter1: Emiter = new Emiter();
let emiter2: Emiter = new Emiter();
let logger: Logger = new Logger();
let channel: Queue<string> = new Queue<string>();

channel.enqueue(emiter1.emit('Hi !'));
channel.enqueue((emiter2.emit('Hey, How are you doing ?')));
/* ... */

while (!channel.empty()) {
    logger.log(channel.dequeue());
}

Binary tree

import { BinaryTreeSearch } from 'typed-structures';

let tree: BinaryTreeSearch<string> = new BinaryTreeSearch<string>();

tree.add('Find me ^^');

console.log(tree.find(tree.root(), 'Find me ^^')); // Find me ^^
console.log(tree.find(tree.root(), '404')); // undefined

Buffer

Generic buffer.

import { GenericBuffer } from 'typed-structures';

let b: GenericBuffer<number> = new GenericBuffer<number>(4, 3, 0, -1);

    b.put(1);
    b.put(2);

    b.rewind();

    console.log(b.get()); // 1
    console.log(b.get()); // 2

RingBuffer

Generic ring buffer.

import { GenericRingBuffer } from 'typed-structures';

let b: GenericRingBuffer<number> = new GenericRingBuffer<number>(4, 3, 0, 0, 0);
b.put(1);
b.put(2);

console.log(b.get()); // 1

TsQ (LinQ inspired)

Minimalist implementation of .NET's LinQ for TypedStructures.

import { TsQ, Set } from 'typed-structures';
import { Person } from './person';

let firstUnknown: Person = new Person(1, 'John', 'Doe', new Date('1970-01-01'));
let secondUnknown: Person = new Person(2, 'Jane', 'Doe', new Date('1970-01-02'));
let thirdUnknown: Person = new Person(3, 'Complete', 'Stranger', new Date('1970-01-03'));
let fourthUnknown: Person = new Person(4, 'Unidentified', 'Person', new Date('1970-01-04'));
let fifthUnknown: Person = new Person(5, 'Unnamed', 'Person', new Date('1970-01-04'));

let unknowns: Set<Person> = new Set<Person>();
unknowns.add(firstUnknown);
unknowns.add(secondUnknown);
unknowns.add(thirdUnknown);
unknowns.add(fourthUnknown);
unknowns.add(fifthUnknown);

TsQ.from(unknowns)
    .select("name")
    .where(e => e.name.length > 4)
    .order_by("id", "desc")
    .fetch();
    
TsQ.from(unknowns)
    .group_by("group")
    .fetch();

Questions 💬

For questions and support feel free to open an issue

Issues 🔎

See CONTRIBUTING.md

Contribution 🛠

Feel free to contribute and enhance typed structures 🎉💛

License

MIT

2018-present Anthony & Thibaud