diff --git a/src/util/ticonfig.js b/src/util/ticonfig.js index bdc66520..c47c397a 100644 --- a/src/util/ticonfig.js +++ b/src/util/ticonfig.js @@ -77,7 +77,7 @@ export class TiConfig { * Gets a value for a given key. Keys may use dot notation to get values from * nested objects. For example, "cli.colors" maps to { cli: { colors: true } }. * @param {String} key - The config object name - * @param {string} defaultValue - A default value if the value does not exist + * @param {string} defaultValue - A default value if the value does not exist or is empty * @returns {*} The value */ get(key, defaultValue) { @@ -101,7 +101,10 @@ export class TiConfig { } while (obj && (p = parts[i++])); } - return obj && q && Object.hasOwn(obj, q) ? obj[q] : defaultValue; + if (obj && q && Object.hasOwn(obj, q)) { + return obj[q] === '' && defaultValue !== undefined ? defaultValue : obj[q]; + } + return defaultValue; } /** diff --git a/test/util/fixtures/ticonfig/good.json b/test/util/fixtures/ticonfig/good.json index f6c791d9..8572c6bd 100644 --- a/test/util/fixtures/ticonfig/good.json +++ b/test/util/fixtures/ticonfig/good.json @@ -1,4 +1,7 @@ { + "cli": { + "width": "" + }, "user": { "name": "Titanium" } diff --git a/test/util/ticonfig.test.js b/test/util/ticonfig.test.js index 86cbb70d..eba3429c 100644 --- a/test/util/ticonfig.test.js +++ b/test/util/ticonfig.test.js @@ -45,6 +45,7 @@ describe('TiConfig', () => { assert.strictEqual(cfg.user.name, 'Titanium'); assert.strictEqual(cfg.get().user.name, 'Titanium'); assert.strictEqual(cfg.get('foo', 'bar'), 'bar'); + assert.strictEqual(cfg.get('cli.width', 80), 80); }); it('should get the config path', () => { @@ -58,12 +59,14 @@ describe('TiConfig', () => { truthy: 'true', falsey: 'false', undef: undefined, - nil: 'null' + nil: 'null', + empty: '' }); assert.strictEqual(cfg.get('truthy'), true); assert.strictEqual(cfg.get('falsey'), false); assert.strictEqual(cfg.get('undef'), ''); assert.strictEqual(cfg.get('nil'), null); + assert.strictEqual(cfg.get('empty'), ''); }); it('should set values', () => { @@ -106,6 +109,10 @@ describe('TiConfig', () => { cfg.set('bar', 1.23); assert.strictEqual(cfg.get('bar'), '1.23'); + + cfg.set('bar', ''); + assert.strictEqual(cfg.get('bar'), ''); + assert.strictEqual(cfg.get('bar', 123), 123); }); it('should save the config', async () => {