Skip to content

Commit d44f8f6

Browse files
committed
fix(config): improve getBoolean() and getNumber()
1 parent 085088e commit d44f8f6

File tree

2 files changed

+114
-11
lines changed

2 files changed

+114
-11
lines changed

ionic/config/config.ts

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ export class Config {
124124
* Returns a single config value, given a key.
125125
*
126126
* @param {string} [key] - the key for the config value
127-
* @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`.
127+
* @param {any} [fallbackValue] - a fallback value to use when the config
128+
* value was not found, or is config value is `null`. Fallback value
129+
* defaults to `null`.
128130
*/
129131
get(key: string, fallbackValue: any = null): any {
130132

@@ -232,13 +234,42 @@ export class Config {
232234
/**
233235
* @name getBoolean
234236
* @description
235-
* Same as `get()`, however always returns a boolean value.
236-
*
237+
* Same as `get()`, however always returns a boolean value. If the
238+
* value from `get()` is `null`, then it'll return the `fallbackValue`
239+
* which defaults to `false`. Otherwise, `getBoolean()` will return
240+
* if the config value is truthy or not. It also returns `true` if
241+
* the config value was the string value `"true"`.
237242
* @param {string} [key] - the key for the config value
243+
* @param {boolean} [fallbackValue] - a fallback value to use when the config
244+
* value was `null`. Fallback value defaults to `false`.
238245
*/
239-
getBoolean(key: string): boolean {
246+
getBoolean(key: string, fallbackValue: boolean = false): boolean {
240247
let val = this.get(key);
241-
return (val || val === 'true') ? true : false;
248+
if (val === null) {
249+
return fallbackValue;
250+
}
251+
if (typeof val === 'string') {
252+
return val === 'true';
253+
}
254+
return !!val;
255+
}
256+
257+
258+
/**
259+
* @name getNumber
260+
* @description
261+
* Same as `get()`, however always returns a number value. Uses `parseFloat()`
262+
* on the value received from `get()`. If the result from the parse is `NaN`,
263+
* then it will return the value passed to `fallbackValue`. If no fallback
264+
* value was provided then it'll default to returning `NaN` when the result
265+
* is not a valid number.
266+
* @param {string} [key] - the key for the config value
267+
* @param {number} [fallbackValue] - a fallback value to use when the config
268+
* value turned out to be `NaN`. Fallback value defaults to `NaN`.
269+
*/
270+
getNumber(key: string, fallbackValue: number = NaN): number {
271+
let val = parseFloat( this.get(key) );
272+
return isNaN(val) ? fallbackValue : val;
242273
}
243274

244275

@@ -284,24 +315,22 @@ export class Config {
284315
* @name settings()
285316
* @description
286317
*/
287-
settings() {
288-
const args = arguments;
289-
290-
switch (args.length) {
318+
settings(arg0?: any, arg1?: any) {
319+
switch (arguments.length) {
291320

292321
case 0:
293322
return this._s;
294323

295324
case 1:
296325
// settings({...})
297-
this._s = args[0];
326+
this._s = arg0;
298327
this._c = {}; // clear cache
299328
break;
300329

301330
case 2:
302331
// settings('ios', {...})
303332
this._s.platforms = this._s.platforms || {};
304-
this._s.platforms[args[0]] = args[1];
333+
this._s.platforms[arg0] = arg1;
305334
this._c = {}; // clear cache
306335
break;
307336
}

ionic/config/test/config.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,80 @@ export function run() {
444444
expect(config.get('occupation', 'Weather Man')).toEqual('Weather Man');
445445
});
446446

447+
it('should get a boolean value with a boolean config value', () => {
448+
let config = new Config({
449+
key1: true,
450+
key2: false
451+
});
452+
expect(config.getBoolean('key1')).toEqual(true);
453+
expect(config.getBoolean('key2')).toEqual(false);
454+
});
455+
456+
it('should get a boolean value with a string config value', () => {
457+
let config = new Config({
458+
key1: 'true',
459+
key2: 'false',
460+
key3: 'whatever'
461+
});
462+
expect(config.getBoolean('key1')).toEqual(true);
463+
expect(config.getBoolean('key2')).toEqual(false);
464+
expect(config.getBoolean('key3')).toEqual(false);
465+
expect(config.getBoolean('key4')).toEqual(false);
466+
expect(config.getBoolean('key5', true)).toEqual(true);
467+
});
468+
469+
it('should get a boolean value with a number config value', () => {
470+
let config = new Config({
471+
key1: 0,
472+
key2: 1,
473+
key3: 'whatever'
474+
});
475+
expect(config.getBoolean('key1')).toEqual(false);
476+
expect(config.getBoolean('key2')).toEqual(true);
477+
});
478+
479+
it('should get a number value with a number config value', () => {
480+
let config = new Config({
481+
key: 6
482+
});
483+
expect(config.getNumber('key')).toEqual(6);
484+
});
485+
486+
it('should get a number value with a string config value', () => {
487+
let config = new Config({
488+
key: '6',
489+
numThenString: '6baymax',
490+
stringThenNum: 'baymax6'
491+
});
492+
expect(config.getNumber('key', 5)).toEqual(6);
493+
expect(config.getNumber('numThenString', 4)).toEqual(6);
494+
expect( isNaN(config.getNumber('stringThenNum')) ).toEqual(true);
495+
});
496+
497+
it('should get a number NaN value with a NaN config value', () => {
498+
let config = new Config({
499+
allString: 'allstring',
500+
imNull: null,
501+
imUndefined: undefined
502+
});
503+
expect( isNaN(config.getNumber('notfound'))).toEqual(true);
504+
expect( isNaN(config.getNumber('allString'))).toEqual(true);
505+
expect( isNaN(config.getNumber('imNull'))).toEqual(true);
506+
expect( isNaN(config.getNumber('imUndefined'))).toEqual(true);
507+
});
508+
509+
it('should get a number fallback value with a NaN config value', () => {
510+
let config = new Config({
511+
allString: 'allstring',
512+
imNull: null,
513+
imUndefined: undefined
514+
});
515+
expect( config.getNumber('notfound', 6)).toEqual(6);
516+
expect( config.getNumber('allString', 6)).toEqual(6);
517+
expect( config.getNumber('imNull', 6)).toEqual(6);
518+
expect( config.getNumber('imUndefined', 6)).toEqual(6);
519+
});
520+
447521
it('should get settings object', () => {
448522
let config = new Config({
449523
name: 'Doc Brown',

0 commit comments

Comments
 (0)