UNPKG

taxonium-component

Version:

React component for exploring large phylogenetic trees in the browser

129 lines (128 loc) 4.16 kB
var R = Object.defineProperty; var A = (n, t, s) => t in n ? R(n, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : n[t] = s; var y = (n, t, s) => A(n, typeof t != "symbol" ? t + "" : t, s); import { s as b, a1 as F, be as E } from "./JBrowsePanel-BNE3gNW1.js"; import { B as O } from "./index-CpJXUZUB.js"; function B(n, t = JSON.stringify) { const s = [], e = /* @__PURE__ */ new Set(); for (const i of n) { const o = t(i); e.has(o) || (s.push(i), e.add(o)); } return s; } function T(n) { let t = 0; for (const s of n) t += s.length; return t; } function U(n) { const t = new Uint8Array(T(n)); let s = 0; for (const e of n) t.set(e, s), s += e.length; return t; } const I = 65536, k = 10; class N { constructor(t, s, e = 20) { y(this, "ixxFile"); y(this, "ixFile"); y(this, "maxResults"); this.ixxFile = t, this.ixFile = s, this.maxResults = e; } async search(t, s) { let e = []; const o = t.split(" ")[0].toLowerCase(), r = await this._getBuffer(o, s); if (!r) return []; let { end: a, buffer: u } = r, l = !1; const w = new TextDecoder("utf8"); for (; !l; ) { const d = w.decode(u), g = d.slice(0, d.lastIndexOf(` `)).split(` `).filter((c) => !!c), m = []; for (const c of g) { const h = c.split(" ")[0], p = h.startsWith(o); h.slice(0, o.length) > o && (l = !0), p && m.push(c); } const x = m.flatMap((c) => { const [h, ...p] = c.split(" "); return p.map((C) => [h, C.split(",")[0]]); }); if (e.length + x.length < this.maxResults && !l) { const c = await this.ixFile.read(I, a, s); if (c.length === 0) { e = e.concat(x); break; } u = U([u, c]), a += I; } else if (e.length + x.length >= this.maxResults || l) { e = e.concat(x); break; } } return B(e, (d) => d[1]).slice(0, this.maxResults); } async getIndex(t) { return (await this.ixxFile.readFile({ encoding: "utf8", ...t })).split(` `).filter((e) => !!e).map((e) => { const i = e.length - k, o = e.slice(0, i), r = e.slice(i), a = Number.parseInt(r, 16); return [o, a]; }); } async _getBuffer(t, s) { let e = 0, i = 65536; const o = await this.getIndex(s); for (const [u, l] of o) u.slice(0, t.length) < t && (e = l, i = l + 65536); const r = i - e; return r < 0 ? void 0 : { buffer: await this.ixFile.read(r, e, s), end: i }; } } function L(n) { try { return decodeURIComponent(n); } catch { return n; } } function S(n, t, s = 15) { const e = n.toLowerCase().indexOf(t); return n.length < 40 ? n : (Math.max(0, e - s) > 0 ? "..." : "") + n.slice(Math.max(0, e - s), e + t.length + s).trim() + (e + t.length < n.length ? "..." : ""); } class _ extends O.BaseAdapter { constructor(t, s, e) { super(t, s, e); const i = b.readConfObject(t, "ixFilePath"), o = b.readConfObject(t, "ixxFilePath"); if (!i) throw new Error("must provide out.ix"); if (!o) throw new Error("must provide out.ixx"); this.trixJs = new N(F.openLocation(o, e), F.openLocation(i, e), 1500); } async searchIndex(t) { const s = t.queryString.toLowerCase(), e = s.split(" "), o = (await this.trixJs.search(s)).filter(([, r]) => e.every((a) => L(r).toLowerCase().includes(a))).map(([r, a]) => { const u = JSON.parse(a.replaceAll("|", ",")), [l, w, ...d] = u.map((f) => L(f)), g = d.findIndex((f) => !!f), m = d.map((f) => f.toLowerCase()).findIndex((f) => f.includes(r.toLowerCase())), x = d[g], c = d[m], h = m !== -1 ? S(c, r) : void 0, p = S(x, r), C = !h || p.toLowerCase() === h.toLowerCase() ? p : `${p} (${h})`; return new E({ locString: l, label: x, displayString: C, matchedObject: u.map((f) => decodeURIComponent(f)), trackId: w }); }); return t.searchType === "exact" ? o.filter((r) => r.getLabel().toLowerCase() === t.queryString.toLowerCase()) : o; } } export { _ as default }; //# sourceMappingURL=TrixTextSearchAdapter-DHWV5uY9.js.map