Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/util/ticonfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions test/util/fixtures/ticonfig/good.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"cli": {
"width": ""
},
"user": {
"name": "Titanium"
}
Expand Down
9 changes: 8 additions & 1 deletion test/util/ticonfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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 () => {
Expand Down
Loading