aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/Search.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/settings/Search.ts')
-rw-r--r--src/shared/settings/Search.ts52
1 files changed, 8 insertions, 44 deletions
diff --git a/src/shared/settings/Search.ts b/src/shared/settings/Search.ts
index 4580236..7de03de 100644
--- a/src/shared/settings/Search.ts
+++ b/src/shared/settings/Search.ts
@@ -12,35 +12,23 @@ export default class Search {
) {
}
- static fromJSON(json: any): Search {
- let defaultEngine = Search.getStringField(json, 'default');
- let engines = Search.getObjectField(json, 'engines');
-
- for (let [name, url] of Object.entries(engines)) {
- if ((/\s/).test(name)) {
- throw new TypeError(
- `While space in the search engine not allowed: "${name}"`);
- }
- if (typeof url !== 'string') {
- throw new TypeError(
- `Invalid type of value in filed "engines": ${JSON.stringify(json)}`);
+ static fromJSON(json: SearchJSON): Search {
+ for (const [name, url] of Object.entries(json.engines)) {
+ if (!(/^[a-zA-Z0-9]+$/).test(name)) {
+ throw new TypeError('Search engine\'s name must be [a-zA-Z0-9]+');
}
- let matches = url.match(/{}/g);
+ const matches = url.match(/{}/g);
if (matches === null) {
throw new TypeError(`No {}-placeholders in URL of "${name}"`);
} else if (matches.length > 1) {
throw new TypeError(`Multiple {}-placeholders in URL of "${name}"`);
}
}
-
- if (!Object.keys(engines).includes(defaultEngine)) {
- throw new TypeError(`Default engine "${defaultEngine}" not found`);
+ if (!Object.keys(json.engines).includes(json.default)) {
+ throw new TypeError(`Default engine "${json.default}" not found`);
}
- return new Search(
- json.default as string,
- json.engines,
- );
+ return new Search(json.default, json.engines);
}
toJSON(): SearchJSON {
@@ -49,28 +37,4 @@ export default class Search {
engines: this.engines,
};
}
-
- private static getStringField(json: any, name: string): string {
- if (!Object.prototype.hasOwnProperty.call(json, name)) {
- throw new TypeError(
- `missing field "${name}" on search: ${JSON.stringify(json)}`);
- }
- if (typeof json[name] !== 'string') {
- throw new TypeError(
- `invalid type of filed "${name}" on search: ${JSON.stringify(json)}`);
- }
- return json[name];
- }
-
- private static getObjectField(json: any, name: string): Object {
- if (!Object.prototype.hasOwnProperty.call(json, name)) {
- throw new TypeError(
- `missing field "${name}" on search: ${JSON.stringify(json)}`);
- }
- if (typeof json[name] !== 'object' || json[name] === null) {
- throw new TypeError(
- `invalid type of filed "${name}" on search: ${JSON.stringify(json)}`);
- }
- return json[name];
- }
}