import { Injectable } from '@angular/core' ; @ Injectable ( { providedIn : 'root' } ) export class StorageService { constructor ( ) { } public getValueNumber ( key , defaultValue ) { return this . getValueT < number > ( key , ( item ) => Number ( item ) , defaultValue ) ; } public getValueString ( key , defaultValue ) { return this . getValueT < string > ( key , ( item ) => item , defaultValue ) ; } public getValueBoolean ( key , defaultValue ) { return this . getValueT < boolean > ( key , ( item ) => item === 'true' , defaultValue ) ; } public getValueT < T > ( key , convert , defaultValue ) { const value = localStorage . getItem ( key ) ; if ( value !== null ) { return convert ( value ) ; } return defaultValue ; } public clear ( key ) { if ( this . isDefined ( key ) ) { localStorage . removeItem ( key ) ; } } public isDefined ( key ) { return ! ! localStorage . getItem ( key ) ; } public setValue ( key , newValue ) { const oldValue = localStorage . getItem ( key ) || '' ; if ( oldValue !== newValue . toString ( ) ) { localStorage . setItem ( key , newValue . toString ( ) ) ; } } }