All files / src dictionary.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 13/13
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129                1x 85x                         2x 2x 2x         5x 3x         2x                       3x 1x                   1x                 6x                 636x               81x                     405x 405x                 6x 6x 5x   6x               4x               2x      
/**
 * Dictionary - an iterable data store
 *
 * Instantiate with a template type for the key:
 * ```
 * const store: Dictionary<string> = new Dictionary<string>();
 * ```
 */
export class Dictionary<K, T> implements Iterable<any> {
    private items: { [index: string]: any } = {};
 
    /**
     * Support for / each pattern:
     *
     * ```
     * for (const key of store) {
     *   const value = store.get(key);
     *   ...
     * }
     * ```
     */
    public [Symbol.iterator]() {
        let pointer: number = 0;
        const items: any = this.items;
        return {
            /**
             * @hidden
             */
            next(): IteratorResult<any> {
                if (pointer < Object.keys(items).length) {
                    return {
                        "done": false,
                        "value": items[Object.keys(items)[pointer++]]
                    };
                }
                return {
                    "done": true,
                    "value": null
                };
            }
        };
    }
 
    /**
     * Remove all elements - reset to empty
     */
    public clear(): void {
        Object.keys(this.items).forEach((key) => {
            delete this.items[key];
        });
    }
 
    /**
     * Check if an item exists - by value
     * @param value The item required
     * @returns
     */
    public contains(value: any): boolean {
        return this.values().indexOf(value) !== -1;
    }
 
    /**
     * Check if a key exists
     * @param key The key required
     * @returns
     */
    public exists(key: string): boolean {
        return !!this.items[key];
    }
 
    /**
     * Get an item by key
     * @param key The element key
     * @returns
     */
    public get(key: string): any {
        return this.items[key];
    }
 
    /**
     * The array of keys in the store
     * @returns
     */
    public keys(): string[] {
        return Object.keys(this.items);
    }
 
    /**
     * Put a new key/value pair
     * Overwrites an existing key
     * @param key The key
     * @param value The value - supports null
     * @returns
     */
    public put(key: string, value: any): any {
        this.items[key] = value;
        return value;
    }
 
    /**
     * Remove an item by key
     * @param key The key to remove
     * @returns
     */
    public remove(key: string): any {
        const item = this.items[key];
        if (item) {
            delete this.items[key];
        }
        return item;
    }
 
    /**
     * Report the number of keys in the store
     * @returns
     */
    public size(): number {
        return Object.keys(this.items).length;
    }
 
    /**
     * The array of values
     * @returns
     */
    public values(): any[] {
        return Object.values(this.items);
    }
}