diff --git a/ionic/config/config.ts b/ionic/config/config.ts index 0fa0a6ab44..3ae34c824b 100644 --- a/ionic/config/config.ts +++ b/ionic/config/config.ts @@ -124,7 +124,9 @@ export class Config { * Returns a single config value, given a key. * * @param {string} [key] - the key for the config value - * @param {any} [fallbackValue] - a fallback value to use when the config value was not found, or is config value is `null`. Fallback value defaults to `null`. + * @param {any} [fallbackValue] - a fallback value to use when the config + * value was not found, or is config value is `null`. Fallback value + * defaults to `null`. */ get(key: string, fallbackValue: any = null): any { @@ -232,13 +234,42 @@ export class Config { /** * @name getBoolean * @description - * Same as `get()`, however always returns a boolean value. - * + * Same as `get()`, however always returns a boolean value. If the + * value from `get()` is `null`, then it'll return the `fallbackValue` + * which defaults to `false`. Otherwise, `getBoolean()` will return + * if the config value is truthy or not. It also returns `true` if + * the config value was the string value `"true"`. * @param {string} [key] - the key for the config value + * @param {boolean} [fallbackValue] - a fallback value to use when the config + * value was `null`. Fallback value defaults to `false`. */ - getBoolean(key: string): boolean { + getBoolean(key: string, fallbackValue: boolean = false): boolean { let val = this.get(key); - return (val || val === 'true') ? true : false; + if (val === null) { + return fallbackValue; + } + if (typeof val === 'string') { + return val === 'true'; + } + return !!val; + } + + + /** + * @name getNumber + * @description + * Same as `get()`, however always returns a number value. Uses `parseFloat()` + * on the value received from `get()`. If the result from the parse is `NaN`, + * then it will return the value passed to `fallbackValue`. If no fallback + * value was provided then it'll default to returning `NaN` when the result + * is not a valid number. + * @param {string} [key] - the key for the config value + * @param {number} [fallbackValue] - a fallback value to use when the config + * value turned out to be `NaN`. Fallback value defaults to `NaN`. + */ + getNumber(key: string, fallbackValue: number = NaN): number { + let val = parseFloat( this.get(key) ); + return isNaN(val) ? fallbackValue : val; } @@ -284,24 +315,22 @@ export class Config { * @name settings() * @description */ - settings() { - const args = arguments; - - switch (args.length) { + settings(arg0?: any, arg1?: any) { + switch (arguments.length) { case 0: return this._s; case 1: // settings({...}) - this._s = args[0]; + this._s = arg0; this._c = {}; // clear cache break; case 2: // settings('ios', {...}) this._s.platforms = this._s.platforms || {}; - this._s.platforms[args[0]] = args[1]; + this._s.platforms[arg0] = arg1; this._c = {}; // clear cache break; } diff --git a/ionic/config/test/config.spec.ts b/ionic/config/test/config.spec.ts index 1c76ca4cbb..83a87bd430 100644 --- a/ionic/config/test/config.spec.ts +++ b/ionic/config/test/config.spec.ts @@ -444,6 +444,80 @@ export function run() { expect(config.get('occupation', 'Weather Man')).toEqual('Weather Man'); }); + it('should get a boolean value with a boolean config value', () => { + let config = new Config({ + key1: true, + key2: false + }); + expect(config.getBoolean('key1')).toEqual(true); + expect(config.getBoolean('key2')).toEqual(false); + }); + + it('should get a boolean value with a string config value', () => { + let config = new Config({ + key1: 'true', + key2: 'false', + key3: 'whatever' + }); + expect(config.getBoolean('key1')).toEqual(true); + expect(config.getBoolean('key2')).toEqual(false); + expect(config.getBoolean('key3')).toEqual(false); + expect(config.getBoolean('key4')).toEqual(false); + expect(config.getBoolean('key5', true)).toEqual(true); + }); + + it('should get a boolean value with a number config value', () => { + let config = new Config({ + key1: 0, + key2: 1, + key3: 'whatever' + }); + expect(config.getBoolean('key1')).toEqual(false); + expect(config.getBoolean('key2')).toEqual(true); + }); + + it('should get a number value with a number config value', () => { + let config = new Config({ + key: 6 + }); + expect(config.getNumber('key')).toEqual(6); + }); + + it('should get a number value with a string config value', () => { + let config = new Config({ + key: '6', + numThenString: '6baymax', + stringThenNum: 'baymax6' + }); + expect(config.getNumber('key', 5)).toEqual(6); + expect(config.getNumber('numThenString', 4)).toEqual(6); + expect( isNaN(config.getNumber('stringThenNum')) ).toEqual(true); + }); + + it('should get a number NaN value with a NaN config value', () => { + let config = new Config({ + allString: 'allstring', + imNull: null, + imUndefined: undefined + }); + expect( isNaN(config.getNumber('notfound'))).toEqual(true); + expect( isNaN(config.getNumber('allString'))).toEqual(true); + expect( isNaN(config.getNumber('imNull'))).toEqual(true); + expect( isNaN(config.getNumber('imUndefined'))).toEqual(true); + }); + + it('should get a number fallback value with a NaN config value', () => { + let config = new Config({ + allString: 'allstring', + imNull: null, + imUndefined: undefined + }); + expect( config.getNumber('notfound', 6)).toEqual(6); + expect( config.getNumber('allString', 6)).toEqual(6); + expect( config.getNumber('imNull', 6)).toEqual(6); + expect( config.getNumber('imUndefined', 6)).toEqual(6); + }); + it('should get settings object', () => { let config = new Config({ name: 'Doc Brown',