All files storage-utilities.ts

91.67% Statements 33/36
93.75% Branches 15/16
88.24% Functions 15/17
91.67% Lines 33/36

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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 2211x       1x   1x                 1x                       22x       22x 19x   22x 5x                 2x                 4x                 2x 1x   1x 1x             1x 1x                           4x                           6x                                                                       1x                   1x                 6x                       1x                       1x                       1x                 1x       7x 7x 2x 1x 1x 1x         5x        
import * as _ from 'lodash';
import { IStorageSettings } from './interfaces/storage-settings';
import { StorageReturnTypes } from './enums/storage-return-types';
import { StorageItem } from './models/storage-item';
import { of, Observable } from 'rxjs';
 
import StorageUtilitiesBase, { defaultSettings } from './storage-utilities-base';
import { StorageTypes } from './enums/storage-types';
import { IStorageNotifier } from './interfaces/storage-typings';
 
/**
 * Provides Core functionality for StorageUtilities,
 * including getItem(), addItem(), removeItem(), removeItems(), 
 * as well as additional functionality.
 */
export class StorageUtilities<T> {
	// Used to hold the current settings applied to StorageUtilities.
	// These settings can be the default, custom settings, or both.
	private static _settings: IStorageSettings;
 
	/**
	 * An Observable type property that allows subscribers to be notified of any storage state changes.
	 * @returns: { storage: StorageTypes, oldValue: T, newValue: T }
	 */
	storageStateChanged: Observable<IStorageNotifier<T>>;
 
	constructor(settings?: IStorageSettings) {
		StorageUtilities._settings = {
			...StorageUtilitiesBase.defaultSettings,
			...settings
		};
		if (settings) {
			StorageUtilitiesBase.setCustomSettings(StorageUtilities._settings);
		}
		if (StorageUtilities._settings.notifiedOfStateChanges) {
			this.storageStateChanged = this.getStorageState();
		}
	}
 
	/**
     * Get default settings applied to StorageUtilities.
     * @returns default settings
     */
	static get defaultSettings(): IStorageSettings {
		return StorageUtilitiesBase.defaultSettings;
	}
 
	/**
     * Get current settings applied to StorageUtilities.
     * These can be either the default, custom settings, or both.
     * @returns currently applied settings
     */
	static get currentSettings(): IStorageSettings {
		return this._settings;
	}
 
	/**
     * Set custom settings to be applied to StorageUtilities,
     * they always overwrite default settings.
     * @param settings the custom settings to be applied to StorageUtilities
     */
	static set customSettings(settings: IStorageSettings) {
		if (!settings || _.isEmpty(settings)) {
			throw new Error(`The settings ${settings} are invalid!`);
		}
		StorageUtilities._settings = settings;
		StorageUtilitiesBase.setCustomSettings(settings);
	}
 
	/**
     * Reset to default settings applied to StorageUtilities.
     */
	static resetSettings(): void {
		StorageUtilities._settings = defaultSettings;
		StorageUtilitiesBase.resetSettings();
	}
 
	/**
     * Returns the item stored in Storage by its key. 
     * If item is not found, or it has an expiry time which has elapsed, null will be returned.
     * The item can be returned as a Promise or Observable if @param returnType is specified.
     * The item is returned from localStorage by default, or sessionStorage as specified by @param storageType.
     * @param key the item's key
     * @param storageType (optional) localStorage (default) / sessionStorage
	 * @param returnType (optional) Promise / Observable
     * @returns the stored item, or null if item is not found or has expired
     */
	protected getItem(key: string, storageType?: StorageTypes, returnType?: StorageReturnTypes): T {
		return this._getDataBasedOnReturnType(StorageUtilitiesBase.getItem(key, storageType), returnType);
	}
 
	/**
     * Adds the item to Storage with its key.
     * The item is added to localStorage by default, or sessionStorage if specified by @param storageType.
     * The item can be added with an expiry time (in milliseconds).
     * This is to add a TTL (Time to live) to invalidate item after the expiry time elapses.
     * @param key the item's key
     * @param item the item to be added to Storage
     * @param expiry (optional) the expiry time (in milliseconds)
     * @param storageType (optional) localStorage (default) / sessionStorage
     */
	protected addItem(key: string, item: T, expiry?: number, storageType?: StorageTypes): void {
		StorageUtilitiesBase.addItem(key, item, expiry, storageType);
	}
 
	/**
	 * Updates the value of a specific property for the stored item.
	 * The property can be of any type. If the property doesn't exist, a new property will be created.
	 * If the item is not found by key, or has expired, null will be returned, or updated item otherwise.
	 * @param key the item's key
	 * @param propName the item's property to be updated
	 * @param newValue the item's property's new value
	 * @param storageType (optional) localStorage (default) / sessionStorage
	 * @returns the updated item, or null if item is not found or has expired
	 */
	protected updateItemProperty(key: string, propName: string, newValue: any, storageType?: StorageTypes): T {
		return StorageUtilitiesBase.updateItemProp(key, propName, newValue, storageType) as T;
	}
 
	/**
	 * Removes the item's specified property.
	 * If the item is not found by key, or has expired, null will be returned, or updated item otherwise.
	 * @param key the item's key
	 * @param propName the item's property to be removed
	 * @param storageType (optional) localStorage (default) / sessionStorage
	 * @returns the updated item, or null if item is not found or has expired
	 */
	protected removeItemProperty(key: string, propName: string, storageType?: StorageTypes): T {
		return StorageUtilitiesBase.removeItemProp(key, propName, storageType) as T;
	}
 
	/**
     * Removes item from Storage by its key.
     * The item is removed from localStorage by default, or sessionStorage if specified by @param storageType.
     * @param key the item's key
     * @param storageType (optional) localStorage (default) / sessionStorage 
     */
	protected removeItem(key: string, storageType?: StorageTypes): void {
		StorageUtilitiesBase.removeItem(key, storageType);
	}
 
	/**
     * Removes items from Storage by their keys.
     * The items are removed from localStorage by default, or sessionStorage if specified by @param storageType.
     * @param keys the items' keys
     * @param storageType (optional) localStorage (default) / sessionStorage 
     */
	protected removeItems(keys: string[], storageType?: StorageTypes): void {
		StorageUtilitiesBase.removeItems(keys, storageType);
	}
 
	/**
     * Returns storage state as an Observable of type IStorageNotifier.
     * All subscribers will be notified when state changes.
     * @returns storage state of Observable type
     */
	protected getStorageState(): Observable<IStorageNotifier<T>> {
		return StorageUtilitiesBase.getStateObservable() as Observable<IStorageNotifier<T>>;
	}
 
	/**
     * Returns an Array of all storage items.
     * The items are returned from localStorage by default, or sessionStorage if specified by @param storageType.
     * The items can be returned as a Promise or Observable if @param returnType is specified.
     * @param storageType (optional) localStorage (default) / sessionStorage
	 * @param returnType (optional) Promise or Observable
     * @returns an Array of all storage items
     */
	protected getStorageItems(storageType?: StorageTypes, returnType?: StorageReturnTypes): StorageItem<T>[] {
		return this._getDataBasedOnReturnType(StorageUtilitiesBase.getItems(storageType), returnType);
	}
 
	/**
     * Returns an Array of all storage values.
     * The values are returned from localStorage by default, or sessionStorage if specified by @param storageType.
     * The values can be returned as a Promise or Observable if @param returnType is specified.
     * @param storageType (optional) localStorage (default) / sessionStorage
	 * @param returnType (optional) Promise or Observable
     * @returns an Array of all storage values
     */
	protected getStorageValues(storageType?: StorageTypes, returnType?: StorageReturnTypes): T[] {
		return this._getDataBasedOnReturnType(StorageUtilitiesBase.getValues(storageType), returnType);
	}
 
	/**
     * Returns an Array of all storage keys.
     * The keys are returned from localStorage by default, or sessionStorage if specified by @param storageType.
     * The keys can be returned as a Promise or Observable if @param returnType is specified.
     * @param storageType (optional) localStorage (default) / sessionStorage
	 * @param returnType (optional) Promise or Observable
     * @returns an Array of all storage keys
     */
	protected getStorageKeys(storageType?: StorageTypes, returnType?: StorageReturnTypes): string[] {
		return this._getDataBasedOnReturnType(StorageUtilitiesBase.getKeys(storageType), returnType);
	}
 
	/**
     * Removes all items from storage.
     * The items are removed from localStorage by default, or sessionStorage if specified by @param storageType.
     * @param storageType (optional) localStorage (default) / sessionStorage
     */
	protected clearStorage(storageType?: StorageTypes): void {
		StorageUtilitiesBase.clearAll(storageType);
	}
 
	private _getDataBasedOnReturnType(data: any, returnType: StorageReturnTypes): any {
		const type = returnType || StorageUtilities._settings.setReturnType;
		if (type) {
			if (type === 'promise') {
				return Promise.resolve(data);
			} else Eif (type === 'observable') {
				return of(data);
			} else {
				throw new Error(`Return type ${type} is not supported!`);
			}
		} else {
			return data;
		}
	}
}