Terrace/packages/js/core/dist/document.js
2023-01-30 20:59:10 -05:00

74 lines
2.2 KiB
JavaScript

import { parseLine as v, createLineData as O } from "./parser.js";
function D(h, m = " ") {
let c = 0;
const r = O("", m);
async function l() {
switch (c) {
case 0:
c = 1;
break;
case 2:
return c = 1, !1;
}
const e = await h.next();
return e === null ? (c = 3, !0) : (r.line = e, v(r), !1);
}
const y = () => c = 2, a = () => r.level, f = () => r.line.slice(r.offsetHead), s = () => r.line.slice(r.offsetHead, r.offsetTail), u = () => r.line.slice(r.offsetTail), b = (e) => e === s();
async function w(e) {
const i = c === 0 ? -1 : a();
for (; ; ) {
if (await l())
return;
if (a() <= i)
return y();
if (await e())
return;
}
}
async function p(e = -1, i = [f()]) {
var t;
return e === -1 && (e = a() + 1), await l() ? i : a() < e ? (y(), i) : (i.push(((t = r.line) == null ? void 0 : t.slice(e)) || ""), p(e, i));
}
async function j(e = {}) {
const i = {};
let t = {};
return Object.keys(e).length ? Object.keys(e).forEach((n) => {
e[n] === !0 && (t[n] = { type: "normal", handle: () => u().trim() }), typeof e[n] == "function" && (t[n] = { type: "normal", handle: e[n] }), typeof e[n] == "object" && (t[n] = e[n]);
}) : t = { "#any": { type: "normal", handle: () => u().trim() } }, await w(async () => {
const n = s();
if (!n)
return;
const o = t[n] || t["#any"];
if (!!o && (o.type === "normal" ? i[n] = await o.handle() : o.type === "collection" ? (i[n] || (i[n] = []), i[n].push(await o.handle())) : t != null && t["#text"] && (i["#text"] = await p(a())), t && Object.keys(t).every((d) => ["collection"].includes(t[d].type) ? !1 : i[d] !== void 0)))
return !0;
}), i;
}
async function x() {
const e = [["root", []]];
for (; !await l(); ) {
const i = a(), t = i + 1, n = e[i];
if (!n)
continue;
e.length = t;
const o = e[t] = [f(), []];
n[1].push(o);
}
return e[0];
}
return {
next: l,
line: f,
head: s,
tail: u,
level: a,
match: b,
each: w,
blockAsText: p,
toObject: j,
toLineArray: x
};
}
export {
D as useDocument
};