JSFiddle - React, Tailwind, and code Playground

by nockawa

TypeScript

export class StringDictionary<T> {
        private array : any = {};

        has(key : string) : boolean {
            return this.array[key] != undefined;
        }

        get(key: string): T {
            return this.array[key];
        }

        add(key: string, value: T): boolean {
            if (this.has(key)) {
                return false;
            }
            this.set(key, value);
        }

        set(key: string, value: T) {
            this.array[key] = value;
        }

        remove(key: string) {
            delete this.array[key];
        }

        tryRemove(key: string): boolean {
            if (this.has(key) === false) {
                return false;
            }
            delete this.array[key];
            return true;
        }

        get length(): number {
            return Object.keys(this.array).length;
        }

        forEach(callback: (string, T) => void){
            for (var key in this.array) {
                callback(key, this.get(key));
            }
        }

        clear() {
            this.array = {}
        }
    }