import { Settings } from "../Settings" ; export class LocalStorageSettings implements Settings { defaults : { [ s ] : string | number | boolean ; } ; constructor ( defaults ) { this . defaults = defaults || { } ; } private get ( setting ) { const data = localStorage . getItem ( `setting_${ setting }` ) ; if ( data !== null ) { return JSON . parse ( data ) ; } if ( this . defaults . hasOwnProperty ( setting ) ) { return this . defaults [ setting ] ; } throw new Error ( `Unable to resolve setting "${ setting }"` ) ; } set ( setting , value ) { localStorage . setItem ( `setting_${ setting }` , JSON . stringify ( value ) ) ; } getNumber ( setting ) { const value = this . get ( setting ) ; if ( typeof value == 'string' ) { return parseInt ( value , 10 ) ; } if ( typeof value !== 'number' ) { throw new Error ( `Setting "${ setting } is not a number!` ) ; } return value ; } getString ( setting ) { const value = this . get ( setting ) ; if ( typeof value === 'number' ) { return String ( value ) ; } if ( typeof value !== 'string' ) { throw new Error ( `Setting "${ setting } is not a string!` ) ; } return value ; } isEnabled ( setting ) { const value = this . get ( setting ) ; if ( typeof value !== 'boolean' ) { throw new Error ( `Setting "${ setting } is not a boolean!` ) ; } return value ; } }