You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

40 lines
1.0 KiB

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: SearchJSON): Search {
for (let [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);
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(json.engines).includes(json.default)) {
throw new TypeError(`Default engine "${json.default}" not found`);
}
return new Search(json.default, json.engines);
}
toJSON(): SearchJSON {
return {
default: this.defaultEngine,
engines: this.engines,
};
}
}