From dda4e7475cdd092d00441c7cd0ceb194ee5dee3d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 11 Jan 2018 20:07:25 +0900 Subject: move commands to background action --- src/shared/commands/parsers.js | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/shared/commands/parsers.js (limited to 'src/shared/commands/parsers.js') diff --git a/src/shared/commands/parsers.js b/src/shared/commands/parsers.js new file mode 100644 index 0000000..af51338 --- /dev/null +++ b/src/shared/commands/parsers.js @@ -0,0 +1,59 @@ +const normalizeUrl = (args, searchConfig) => { + let concat = args.join(' '); + try { + return new URL(concat).href; + } catch (e) { + if (concat.includes('.') && !concat.includes(' ')) { + return 'http://' + concat; + } + let query = concat; + let template = searchConfig.engines[ + searchConfig.default + ]; + for (let key in searchConfig.engines) { + if (args[0] === key) { + query = args.slice(1).join(' '); + template = searchConfig.engines[key]; + } + } + return template.replace('{}', encodeURIComponent(query)); + } +}; + +const mustNumber = (v) => { + let num = Number(v); + if (isNaN(num)) { + throw new Error('Not number: ' + v); + } + return num; +}; + +const parseSetOption = (word, types) => { + let [key, value] = word.split('='); + if (!value) { + value = !key.startsWith('no'); + key = value ? key : key.slice(2); + } + let type = types[key]; + if (!type) { + throw new Error('Unknown property: ' + key); + } + if (type === 'boolean' && typeof value !== 'boolean' || + type !== 'boolean' && typeof value === 'boolean') { + throw new Error('Invalid argument: ' + word); + } + + switch (type) { + case 'string': return [key, value]; + case 'number': return [key, mustNumber(value)]; + case 'boolean': return [key, value]; + } +}; + +const parseCommandLine = (line) => { + let words = line.trim().split(/ +/); + let name = words.shift(); + return [name, words]; +}; + +export { normalizeUrl, parseCommandLine, parseSetOption }; -- cgit v1.2.3 From befcff973aa0fd9b0f3a73932a39d36e9eb85bf2 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 11 Jan 2018 20:14:56 +0900 Subject: fix property parser --- src/shared/commands/parsers.js | 2 +- test/shared/commands/parsers.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'src/shared/commands/parsers.js') diff --git a/src/shared/commands/parsers.js b/src/shared/commands/parsers.js index af51338..fb37d2a 100644 --- a/src/shared/commands/parsers.js +++ b/src/shared/commands/parsers.js @@ -30,7 +30,7 @@ const mustNumber = (v) => { const parseSetOption = (word, types) => { let [key, value] = word.split('='); - if (!value) { + if (value === undefined) { value = !key.startsWith('no'); key = value ? key : key.slice(2); } diff --git a/test/shared/commands/parsers.test.js b/test/shared/commands/parsers.test.js index 200323c..0a1960c 100644 --- a/test/shared/commands/parsers.test.js +++ b/test/shared/commands/parsers.test.js @@ -9,6 +9,12 @@ describe("shared/commands/parsers", () => { expect(value).to.equal('utf-8'); }); + it('parse set empty string', () => { + let [key, value] = parsers.parseSetOption('encoding=', { encoding: 'string' }); + expect(key).to.equal('encoding'); + expect(value).to.equal(''); + }); + it('parse set string', () => { let [key, value] = parsers.parseSetOption('history=50', { history: 'number' }); expect(key).to.equal('history'); @@ -34,6 +40,7 @@ describe("shared/commands/parsers", () => { it('throws error on invalid property', () => { expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'number' })).to.throw(Error, 'Not number'); expect(() => parsers.parseSetOption('charset=utf-8', { charset: 'boolean' })).to.throw(Error, 'Invalid'); + expect(() => parsers.parseSetOption('charset=', { charset: 'boolean' })).to.throw(Error, 'Invalid'); expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'string' })).to.throw(Error, 'Invalid'); expect(() => parsers.parseSetOption('smoothscroll', { smoothscroll: 'number' })).to.throw(Error, 'Invalid'); }) -- cgit v1.2.3