diff options
Diffstat (limited to 'src/background/usecases/parsers.ts')
-rw-r--r-- | src/background/usecases/parsers.ts | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/background/usecases/parsers.ts b/src/background/usecases/parsers.ts index 3616ac3..6135fd8 100644 --- a/src/background/usecases/parsers.ts +++ b/src/background/usecases/parsers.ts @@ -1,3 +1,5 @@ +import * as PropertyDefs from '../../shared//property-defs'; + const mustNumber = (v: any): number => { let num = Number(v); if (isNaN(num)) { @@ -7,29 +9,28 @@ const mustNumber = (v: any): number => { }; const parseSetOption = ( - word: string, - types: { [key: string]: string }, + args: string, ): any[] => { - let [key, value]: any[] = word.split('='); + let [key, value]: any[] = args.split('='); if (value === undefined) { value = !key.startsWith('no'); key = value ? key : key.slice(2); } - let type = types[key]; - if (!type) { + let def = PropertyDefs.defs.find(d => d.name === key); + if (!def) { throw new Error('Unknown property: ' + key); } - if (type === 'boolean' && typeof value !== 'boolean' || - type !== 'boolean' && typeof value === 'boolean') { - throw new Error('Invalid argument: ' + word); + if (def.type === 'boolean' && typeof value !== 'boolean' || + def.type !== 'boolean' && typeof value === 'boolean') { + throw new Error('Invalid argument: ' + args); } - switch (type) { + 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: ' + type); + throw new Error('Unknown property type: ' + def.type); }; export { parseSetOption }; |