import { MemoryStorage } from './memory_storage' ; let storage = window . localStorage ; let keyPrefix = '' ; if ( storage == null ) { try { if ( 'localStorage' in window && window . localStorage != null ) { storage = window . localStorage ; } } catch ( e ) { } if ( storage == null ) storage = new MemoryStorage ( ) ; } function getKey ( key ) { return `${ keyPrefix }${ key }` ; } export class StorageUtil { public static setKeyPrefix ( prefix ) { keyPrefix = prefix ; } public static hasKey ( key ) { const k = getKey ( key ) ; return storage . getItem ( k ) != null ; } public static get ( key , defaultValue ) { const k = getKey ( key ) ; return storage . getItem ( k ) || defaultValue ; } public static getInt ( key , defaultValue ) { const k = getKey ( key ) ; const item = storage . getItem ( k ) ; if ( item == null ) return defaultValue ; const value = parseInt ( item , 10 ) ; if ( isNaN ( value ) ) return defaultValue ; return value ; } public static getFloat ( key , defaultValue ) { const k = getKey ( key ) ; const item = storage . getItem ( k ) ; if ( item == null ) return defaultValue ; const value = parseFloat ( item ) ; if ( isNaN ( value ) ) return defaultValue ; return value ; } public static getBool ( key , defaultValue ) { const k = getKey ( key ) ; const value = storage . getItem ( k ) ; if ( value === 'true' ) return true ; if ( value === 'false' ) return false ; return defaultValue ; } public static getObject ( key , defaultValue ) { const k = getKey ( key ) ; const value = storage . getItem ( k ) ; try { if ( value != null ) return JSON . parse ( value ) ; } catch ( e ) { console . error ( e ) ; } return defaultValue ; } public static put ( key , value ) { const k = getKey ( key ) ; storage . setItem ( k , value ) ; return true ; } public static putObject ( key , obj ) { const k = getKey ( key ) ; storage . setItem ( k , JSON . stringify ( obj ) ) ; return true ; } }