From ff85797ffcce6dffe9a36ec14e1d1efbba1c1e5b Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 26 May 2019 21:39:38 +0900 Subject: [PATCH] Fix settings parsing --- src/shared/Settings.ts | 28 +++++++++++++++++----------- test/shared/Settings.test.ts | 4 ++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/shared/Settings.ts b/src/shared/Settings.ts index e1e2046..0bef342 100644 --- a/src/shared/Settings.ts +++ b/src/shared/Settings.ts @@ -101,17 +101,23 @@ export const blacklistValueOf = (o: any): string[] => { export const valueOf = (o: any): Settings => { let settings = { ...DefaultSetting }; - if (Object.prototype.hasOwnProperty.call(o, 'keymaps')) { - settings.keymaps = keymapsValueOf(o.keymaps); - } - if (Object.prototype.hasOwnProperty.call(o, 'search')) { - settings.search = searchValueOf(o.search); - } - if (Object.prototype.hasOwnProperty.call(o, 'properties')) { - settings.properties = propertiesValueOf(o.properties); - } - if (Object.prototype.hasOwnProperty.call(o, 'blacklist')) { - settings.blacklist = blacklistValueOf(o.blacklist); + for (let key of Object.keys(o)) { + switch (key) { + case 'keymaps': + settings.keymaps = keymapsValueOf(o.keymaps); + break; + case 'search': + settings.search = searchValueOf(o.search); + break; + case 'properties': + settings.properties = propertiesValueOf(o.properties); + break; + case 'blacklist': + settings.blacklist = blacklistValueOf(o.blacklist); + break; + default: + throw new TypeError('unknown setting: ' + key); + } } return settings; }; diff --git a/test/shared/Settings.test.ts b/test/shared/Settings.test.ts index 02cd022..04b28c4 100644 --- a/test/shared/Settings.test.ts +++ b/test/shared/Settings.test.ts @@ -186,5 +186,9 @@ describe('Settings', () => { expect(value.search.engines).to.be.an('object'); expect(value.blacklist).to.be.empty; }); + + it('throws a TypeError with an unknown field', () => { + expect(() => settings.valueOf({ name: 'alice' })).to.throw(TypeError) + }); }); });