aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings
diff options
context:
space:
mode:
authorShin'ya UEOKA <ueokande@i-beam.org>2019-10-05 01:08:07 +0000
committerShin'ya UEOKA <ueokande@i-beam.org>2019-10-06 12:58:59 +0000
commit2116ac90a6dfdb0910d7ad2896f70a052aa635cc (patch)
treee3ed16ac3fb126e06e97b18a742e074fbeed079a /src/shared/settings
parent410ffbb0376b9399928ef8d4dd13079bfb120e14 (diff)
Make Search class
Diffstat (limited to 'src/shared/settings')
-rw-r--r--src/shared/settings/Search.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/shared/settings/Search.ts b/src/shared/settings/Search.ts
new file mode 100644
index 0000000..4580236
--- /dev/null
+++ b/src/shared/settings/Search.ts
@@ -0,0 +1,76 @@
+type Entries = { [name: string]: string };
+
+export type SearchJSON = {
+ default: string;
+ engines: { [key: string]: string };
+};
+
+export default class Search {
+ constructor(
+ public defaultEngine: string,
+ public engines: Entries,
+ ) {
+ }
+
+ 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)}`);
+ }
+ let 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`);
+ }
+
+ return new Search(
+ json.default as string,
+ json.engines,
+ );
+ }
+
+ toJSON(): SearchJSON {
+ return {
+ default: this.defaultEngine,
+ 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];
+ }
+}