feat(config): strongly typed config

fixes #15097
This commit is contained in:
Manu Mtz.-Almeida
2018-08-08 23:53:04 +02:00
parent 6f2827ad25
commit 01690452e9
5 changed files with 61 additions and 17 deletions

View File

@ -1,19 +1,62 @@
export interface IonicConfig {
isDevice?: boolean;
statusbarPadding?: boolean;
inputShims?: boolean;
backButtonIcon?: string;
backButtonText?: string;
spinner?: string;
loadingSpinner?: string;
menuIcon?: string;
animate?: boolean;
pickerSpinner?: string;
refreshingIcon?: string;
refreshingSpinner?: string;
mode?: string;
menuType?: string;
scrollPadding?: string;
inputBlurring?: string;
scrollAssist?: string;
hideCaretOnScroll?: string;
infiniteLoadingSpinner?: string;
keyboardHeight?: number;
swipeBackEnabled?: boolean;
tabbarPlacement?: string;
tabbarLayout?: string;
tabbarHighlight?: boolean;
actionSheetEnter?: string;
alertEnter?: string;
loadingEnter?: string;
modalEnter?: string;
popoverEnter?: string;
toastEnter?: string;
pickerEnter?: string;
actionSheetLeave?: string;
alertLeave?: string;
loadingLeave?: string;
modalLeave?: string;
popoverLeave?: string;
toastLeave?: string;
pickerLeave?: string;
}
export class Config {
private m: Map<string, any>;
constructor(configObj: {[key: string]: any}) {
this.m = new Map<string, any>(Object.entries(configObj));
private m: Map<keyof IonicConfig, any>;
constructor(configObj: IonicConfig) {
this.m = new Map<keyof IonicConfig, any>(Object.entries(configObj) as any);
}
get(key: string, fallback?: any): any {
get(key: keyof IonicConfig, fallback?: any): any {
const value = this.m.get(key);
return (value !== undefined) ? value : fallback;
}
getBoolean(key: string, fallback = false): boolean {
getBoolean(key: keyof IonicConfig, fallback = false): boolean {
const val = this.m.get(key);
if (val === undefined) {
return fallback;
@ -24,12 +67,12 @@ export class Config {
return !!val;
}
getNumber(key: string, fallback?: number): number {
getNumber(key: keyof IonicConfig, fallback?: number): number {
const val = parseFloat(this.m.get(key));
return isNaN(val) ? (fallback !== undefined ? fallback : NaN) : val;
}
set(key: string, value: any) {
set(key: keyof IonicConfig, value: any) {
this.m.set(key, value);
}
}