aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/parsers.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-07 21:16:47 +0900
committerGitHub <noreply@github.com>2019-05-07 21:16:47 +0900
commit05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch)
tree2c7708ca91ac2b462cc86aa28612e3d3943496f3 /src/background/usecases/parsers.ts
parent457d954e08923b4accd28a919c72d0b61db1bb98 (diff)
parent27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff)
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'src/background/usecases/parsers.ts')
-rw-r--r--src/background/usecases/parsers.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/background/usecases/parsers.ts b/src/background/usecases/parsers.ts
new file mode 100644
index 0000000..6135fd8
--- /dev/null
+++ b/src/background/usecases/parsers.ts
@@ -0,0 +1,36 @@
+import * as PropertyDefs from '../../shared//property-defs';
+
+const mustNumber = (v: any): number => {
+ let num = Number(v);
+ if (isNaN(num)) {
+ throw new Error('Not number: ' + v);
+ }
+ return num;
+};
+
+const parseSetOption = (
+ args: string,
+): any[] => {
+ let [key, value]: any[] = args.split('=');
+ if (value === undefined) {
+ value = !key.startsWith('no');
+ key = value ? key : key.slice(2);
+ }
+ let def = PropertyDefs.defs.find(d => d.name === key);
+ if (!def) {
+ throw new Error('Unknown property: ' + key);
+ }
+ if (def.type === 'boolean' && typeof value !== 'boolean' ||
+ def.type !== 'boolean' && typeof value === 'boolean') {
+ throw new Error('Invalid argument: ' + args);
+ }
+
+ switch (def.type) {
+ case 'string': return [key, value];
+ case 'number': return [key, mustNumber(value)];
+ case 'boolean': return [key, value];
+ }
+ throw new Error('Unknown property type: ' + def.type);
+};
+
+export { parseSetOption };