aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/parsers.ts
blob: 544dce49c4adc697d12d73adb6c39dba96b645eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Properties from '../../shared/settings/Properties';

const mustNumber = (v: any): number => {
  const 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);
  }
  const def = Properties.def(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 };