import { Injectable } from '@angular/core' ; import { UtilsService } from '../utils/utils.service' ; /**
 * Enumerative of the possible keys present in the storage
 */ export enum STORAGE_KEYS { SETTINGS = 'settings' } @ Injectable ( { providedIn : 'root' } ) export class StorageService { private readonly storage : Storage ; /**
     * Initialize the storage service setting the default storage.
     */ constructor ( ) { this . storage = window . localStorage ; } /**
     * Return the value or the values based on the keys passed as parameters.
     */ public async get ( keys ) { if ( typeof keys === 'string' ) { return this . parseItem ( this . storage . getItem ( keys ) ) ; } const items = [ ] ; for ( const key of Object . keys ( this . storage ) ) { if ( keys . includes ( key ) ) { items . push ( await this . get ( key ) ) ; } } return items && items . length > 0 ? items : null ; } /**
     * Return all the saved values in the storage.
     */ public async getAll ( ) { return Object . values ( this . storage ) . map ( ( item ) => { return this . parseItem ( item ) ; } ) || null ; } /**
     * Return all the saved keys in the storage.
     */ public async getAllEntries ( ) { return Object . entries ( this . storage ) . map ( ( entry ) => { entry [ 1 ] = this . parseItem ( entry [ 1 ] ) ; return entry ; } ) || null ; } /**
     * Save an item in the storage.
     */ public async set ( key , item ) { this . storage . setItem ( key , JSON . stringify ( item ) ) ; } /**
     * Remove an item from the storage.
     */ public async remove ( key ) { this . storage . removeItem ( key ) ; } /**
     * Check if an item exist in the storage. Return true if it exist, false otherwise.
     */ public async exist ( key ) { return ! ! this . storage . getItem ( key ) ; } /**
     * Remove all the items from the storage.
     */ public async clear ( ) { this . storage . clear ( ) ; } /**
     * Check if there are items in the storage. Return true if there are items, false otherwise.
     */ public async isEmpty ( ) { const items = await this . getAll ( ) ; return items && items . length > 0 ; } /**
     * Parse the storage item and return the correct type.
     */ private parseItem ( item ) { if ( UtilsService . isJSONString ( item ) ) { return JSON . parse ( item ) ; } else if ( ! Number . isNaN ( + item ) ) { return + item ; } else if ( item === 'true' || item === 'false' ) { return item === 'true' ; } else { return item ; } } }