Types on src/share

This commit is contained in:
Shin'ya Ueoka 2019-04-30 21:50:46 +09:00
parent 2b8c37e57f
commit 0cffb09e24
11 changed files with 88 additions and 54 deletions

View file

@ -2,20 +2,20 @@
// mystr: 'string',
// mynum: 'number',
// mybool: 'boolean',
const types = {
const types: { [key: string]: string } = {
hintchars: 'string',
smoothscroll: 'boolean',
complete: 'string',
};
// describe default values of a property
const defaults = {
const defaults: { [key: string]: string | number | boolean } = {
hintchars: 'abcdefghijklmnopqrstuvwxyz',
smoothscroll: false,
complete: 'sbh',
};
const docs = {
const docs: { [key: string]: string } = {
hintchars: 'hint characters on follow mode',
smoothscroll: 'smooth scroll',
complete: 'which are completed at the open page',

View file

@ -1,12 +1,12 @@
import DefaultSettings from './default';
import * as settingsValues from './values';
const loadRaw = async() => {
const loadRaw = async(): Promise<any> => {
let { settings } = await browser.storage.local.get('settings');
if (!settings) {
return DefaultSettings;
}
return { ...DefaultSettings, ...settings };
return { ...DefaultSettings, ...settings as object };
};
const loadValue = async() => {
@ -23,7 +23,7 @@ const loadValue = async() => {
return { ...settingsValues.valueFromJson(DefaultSettings.json), ...value };
};
const save = (settings) => {
const save = (settings: any): Promise<any> => {
return browser.storage.local.set({
settings,
});

View file

@ -1,4 +1,4 @@
import operations from 'shared/operations';
import operations from '../operations';
import * as properties from './properties';
const VALID_TOP_KEYS = ['keymaps', 'search', 'blacklist', 'properties'];
@ -6,7 +6,7 @@ const VALID_OPERATION_VALUES = Object.keys(operations).map((key) => {
return operations[key];
});
const validateInvalidTopKeys = (settings) => {
const validateInvalidTopKeys = (settings: any): void => {
let invalidKey = Object.keys(settings).find((key) => {
return !VALID_TOP_KEYS.includes(key);
});
@ -15,7 +15,7 @@ const validateInvalidTopKeys = (settings) => {
}
};
const validateKeymaps = (keymaps) => {
const validateKeymaps = (keymaps: any): void => {
for (let key of Object.keys(keymaps)) {
let value = keymaps[key];
if (!VALID_OPERATION_VALUES.includes(value.type)) {
@ -24,7 +24,7 @@ const validateKeymaps = (keymaps) => {
}
};
const validateSearch = (search) => {
const validateSearch = (search: any): void => {
let engines = search.engines;
for (let key of Object.keys(engines)) {
if ((/\s/).test(key)) {
@ -49,7 +49,7 @@ const validateSearch = (search) => {
}
};
const validateProperties = (props) => {
const validateProperties = (props: any): void => {
for (let name of Object.keys(props)) {
if (!properties.types[name]) {
throw new Error(`Unknown property name: "${name}"`);
@ -60,7 +60,7 @@ const validateProperties = (props) => {
}
};
const validate = (settings) => {
const validate = (settings: any): void => {
validateInvalidTopKeys(settings);
if (settings.keymaps) {
validateKeymaps(settings.keymaps);

View file

@ -1,6 +1,6 @@
import * as properties from './properties';
const operationFromFormName = (name) => {
const operationFromFormName = (name: string): any => {
let [type, argStr] = name.split('?');
let args = {};
if (argStr) {
@ -9,7 +9,7 @@ const operationFromFormName = (name) => {
return { type, ...args };
};
const operationToFormName = (op) => {
const operationToFormName = (op: any): string => {
let type = op.type;
let args = { ...op };
delete args.type;
@ -20,12 +20,12 @@ const operationToFormName = (op) => {
return op.type + '?' + JSON.stringify(args);
};
const valueFromJson = (json) => {
const valueFromJson = (json: string): object => {
return JSON.parse(json);
};
const valueFromForm = (form) => {
let keymaps = undefined;
const valueFromForm = (form: any): object => {
let keymaps: any = undefined;
if (form.keymaps) {
keymaps = {};
for (let name of Object.keys(form.keymaps)) {
@ -34,7 +34,7 @@ const valueFromForm = (form) => {
}
}
let search = undefined;
let search: any = undefined;
if (form.search) {
search = { default: form.search.default };
@ -54,12 +54,12 @@ const valueFromForm = (form) => {
};
};
const jsonFromValue = (value) => {
const jsonFromValue = (value: any): string => {
return JSON.stringify(value, undefined, 2);
};
const formFromValue = (value, allowedOps) => {
let keymaps = undefined;
const formFromValue = (value: any, allowedOps: any[]): any => {
let keymaps: any = undefined;
if (value.keymaps) {
let allowedSet = new Set(allowedOps);
@ -73,7 +73,7 @@ const formFromValue = (value, allowedOps) => {
}
}
let search = undefined;
let search: any = undefined;
if (value.search) {
search = { default: value.search.default };
if (value.search.engines) {
@ -93,11 +93,11 @@ const formFromValue = (value, allowedOps) => {
};
};
const jsonFromForm = (form) => {
const jsonFromForm = (form: any): string => {
return jsonFromValue(valueFromForm(form));
};
const formFromJson = (json, allowedOps) => {
const formFromJson = (json: string, allowedOps: any[]): any => {
let value = valueFromJson(json);
return formFromValue(value, allowedOps);
};