Declare setting types

This commit is contained in:
Shin'ya Ueoka 2019-05-05 08:03:29 +09:00
parent d01db82c0d
commit a0882bbceb
48 changed files with 1618 additions and 903 deletions

View file

@ -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 };