fix(config): improve getBoolean() and getNumber()

This commit is contained in:
Adam Bradley
2016-03-12 23:44:10 -06:00
parent 085088ee77
commit d44f8f6fcf
2 changed files with 114 additions and 11 deletions

View File

@ -124,7 +124,9 @@ export class Config {
* Returns a single config value, given a key. * Returns a single config value, given a key.
* *
* @param {string} [key] - the key for the config value * @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 { get(key: string, fallbackValue: any = null): any {
@ -232,13 +234,42 @@ export class Config {
/** /**
* @name getBoolean * @name getBoolean
* @description * @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 {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); 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() * @name settings()
* @description * @description
*/ */
settings() { settings(arg0?: any, arg1?: any) {
const args = arguments; switch (arguments.length) {
switch (args.length) {
case 0: case 0:
return this._s; return this._s;
case 1: case 1:
// settings({...}) // settings({...})
this._s = args[0]; this._s = arg0;
this._c = {}; // clear cache this._c = {}; // clear cache
break; break;
case 2: case 2:
// settings('ios', {...}) // settings('ios', {...})
this._s.platforms = this._s.platforms || {}; this._s.platforms = this._s.platforms || {};
this._s.platforms[args[0]] = args[1]; this._s.platforms[arg0] = arg1;
this._c = {}; // clear cache this._c = {}; // clear cache
break; break;
} }

View File

@ -444,6 +444,80 @@ export function run() {
expect(config.get('occupation', 'Weather Man')).toEqual('Weather Man'); 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', () => { it('should get settings object', () => {
let config = new Config({ let config = new Config({
name: 'Doc Brown', name: 'Doc Brown',