123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import { IStorage } from '../interfaces/IStorage';
- import { Utils } from '../Utils';
- 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);
- if (!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 key
- * @param value
- */
- public set (key: string | number, value: T): void {
- this.storage.set(key, value);
- }
- }
|