All files / src/storages MapStorage.ts

69.23% Statements 9/13
50% Branches 1/2
100% Functions 1/1
69.23% Lines 9/13
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 631x       1x   1x 1x       2622x             65252x   65252x       65252x                                           2619x                                
import { injectable } from 'inversify';
 
import { IStorage } from '../interfaces/storages/IStorage';
 
import { Utils } from '../Utils';
 
@injectable()
export abstract class MapStorage <T> implements IStorage <T> {
    /**
     * @type {Map <string | number, T>}
     */
    protected storage: Map <string | number, T> = new Map <string | number, T> ();
 
    /**
     * @param key
     * @returns {T}
     */
    public get (key: string | number): T {
        const value: T | undefined = this.storage.get(key);
 
        Iif (!value) {
            throw new Error(`No value found in map storage with key \`${key}\``);
        }
 
        return value;
    }
 
    /**
     * @param value
     * @returns {string | number | null}
     */
    public getKeyOf (value: T): string | number | null {
        return Utils.mapGetFirstKeyOf(this.storage, value);
    }
 
    /**
     * @returns {number}
     */
    public getLength (): number {
        return Array.from(this.storage).length;
    }
 
    /**
     * @returns {Map <string | number, T>}
     */
    public getStorage (): Map <string | number, T> {
        return this.storage;
    }
 
    /**
     * @param args
     */
    public initialize (...args: any[]): void {}
 
    /**
     * @param key
     * @param value
     */
    public set (key: string | number, value: T): void {
        this.storage.set(key, value);
    }
}