UNPKG

@rr0/case

Version:
199 lines (198 loc) 4.67 kB
var h = Object.defineProperty; var p = (l, i, s) => i in l ? h(l, i, { enumerable: !0, configurable: !0, writable: !0, value: s }) : l[i] = s; var t = (l, i, s) => p(l, typeof i != "symbol" ? i + "" : i, s); class u { constructor() { /** * @member {URL} */ t(this, "url"); /** * "YYYY-MM-DD" * @member {string} */ t(this, "time"); /** * @member {string} */ t(this, "place"); /** * @member {string} */ t(this, "title"); /** * @member {"hoax", "misinterpretation"} */ t(this, "conclusion"); /** * @member {ClassificationMessages} */ t(this, "classification"); /** * @member {string} */ t(this, "image"); } } class c { /** * @param {string} baseUrl */ constructor(i) { /** * @protected * @type {string} */ t(this, "baseUrl"); this.baseUrl = i; } /** * @protected * @param {URL} url * @template T * @return {Promise<T>} */ async fetchJson(i) { return await (await fetch(i, { headers: { accept: "application/json" } })).json(); } } const o = class o extends c { /** * @param {RR0CaseCatalogOptions} options * @param {string[]} files */ constructor(s = {}, e) { super(s.baseUrl); /** * @readonly * @type {string[]} */ t(this, "files"); /** * @readonly * @type {RR0CaseCatalogOptions} */ t(this, "options"); this.options = Object.assign({ ...o.OPTIONS_DEFAULT }, s), this.files = e; } /** * Fetch a case JSON URL. * * @param {string} caseUrl The URL of the JSON file to fetch. * @param {string} [lang] * @return {Promise<RR0Case>} */ async fetch(s, e = navigator.language) { const a = await this.fetchJson(new URL(s, this.baseUrl)); return a.url = new URL(s.replace(this.options.filePath, "/index.html"), this.baseUrl), (!e.startsWith("fr") || !a.title) && (a.title = this.titleFromUrl(s)), a; } /** * @protected * @param {string} caseUrl * @return {string} */ titleFromUrl(s) { let e = s.substring(0, s.length - this.options.filePath.length); return e = e.substring(e.lastIndexOf("/") + 1), e.replaceAll(/([a-z0-9])([A-Z0-9])/g, "$1 $2").trim(); } }; /** * @readonly * @type {RR0CaseCatalogOptions} */ t(o, "OPTIONS_DEFAULT", { baseUrl: new URL("https://rr0.org"), filePath: "/case.json", dirsFile: "casesDirs.json" }); let r = o; class U extends c { /** * * @param {RR0CatalogOptions} [options] The URL to fetch the case and people directories from. */ constructor(s = { cases: r.OPTIONS_DEFAULT, peopleDirsFile: "peopleDirs.json" }) { super(s.cases.baseUrl); /** * @protected * @type {RR0CaseCatalog|undefined} */ t(this, "cases"); /** * @protected * @member {string[] | undefined} */ t(this, "peopleFiles"); /** * @protected * @type {RR0CatalogOptions} */ t(this, "options"); this.options = s; } /** * * @return {Promise<RR0CaseCatalog>} */ async getCases() { if (!this.cases) { const s = this.options.cases, e = s.baseUrl, a = ( /** @type {string[]} */ await this.fetchArray(new URL(s.dirsFile, e), s.filePath) ); this.cases = new r(s, a); } return this.cases; } /** * * @param {string} [peopleDirsFile] */ async getPeople(s = this.options.peopleDirsFile) { return this.peopleFiles || (this.peopleFiles = /** @type {string[]} */ await this.fetchArray(new URL(s, this.options.baseUrl), "/people.json")), this.peopleFiles; } /** * @protected * @param {URL} url * @param {string} suffix * @template T * @return {Promise<T[]>} */ async fetchArray(s, e) { return (await this.fetchJson(s)).map((n) => n + e); } /** * Fetch a people JSON URL. * * @param {string} peopleUrl The URL of the JSON file to fetch. * @return {Promise<T>} */ async fetchPeople(s) { const e = await this.fetchJson(new URL(s, this.baseUrl)), a = "/people.json"; if (e.url = new URL(s.replace(a, "/index.html"), this.baseUrl), !e.title) { let n = s.substring(0, s.length - a.length); n = n.substring(n.lastIndexOf("/") + 1), e.title = n.replaceAll(/([a-z0-9])([A-Z0-9])/g, "$1 $2").trim(); } return e; } } class F { constructor() { /** * @type {RR0CaseCatalogOptions} */ t(this, "cases"); /** * @readonly * @member {string} */ t(this, "peopleDirsFile"); } } export { c as Fetcher, u as RR0Case, r as RR0CaseCatalog, U as RR0Catalog, F as RR0CatalogOptions };