UNPKG

coldstitch

Version:

A code generation library, that helps you craft code snippets in code for multiple languages

167 lines (166 loc) 3.76 kB
const T = { indentSize: 2, indentChar: " ", stringQuote: '"' }, d = { indentSize: 4, indentChar: " ", stringQuote: '"' }, m = T, E = { js: T, python: d, swift: d, java: d, kotlin: d }; function I(t) { return { ...m, ...E[t ?? ""] ?? {} }; } class b { resolve(n) { throw new Error("Not implemented"); } } function $(t) { return t && typeof t == "object" && "namespace" in t && "name" in t; } function A(t, n, s = m) { const c = Array.isArray(t), [e, o] = c ? n.arrayTokens : n.objectTokens, { indentSize: a, indentChar: i, indentLevel: u = 0, inlineArrayElements: h = !0 } = { ...m, ...s }, y = i.repeat(a * u), p = i.repeat(a * (u + 1)), S = (r, f) => { let l; return Array.isArray(r) ? l = A(r, n, { ...s, indentLevel: h ? u : u + 1 }) : C(r) ? l = A(r, n, { ...s, indentLevel: u + 1 }) : typeof r == "function" ? l = `"${r.name} function reference"` : l = n.formatValue(r), c ? l : `${n.formatKey(f)}${n.assignToken}${l}`; }; if (c) { const r = t.map((f) => S(f)); return h ? `${e}${r.join(", ")}${o}` : r.length === 0 ? `${e}${o}` : `${e} ${p}${r.join(`, ` + p)} ${y}${o}`; } else { const r = []; for (const f in t) r.push(S(t[f], f)); return r.length === 0 ? `${e}${o}` : `${e} ${p}${r.join(`, ` + p)} ${y}${o}`; } } function N(t) { const n = t; return Object.defineProperty(n, "raw", { value: t }), n; } function L(t) { var n; return ((n = /^(\s*)/.exec(t)) == null ? void 0 : n[1]) ?? ""; } function _(t) { return t[t.length - 1]; } function w(t) { const n = {}; return t.forEach((s) => { n[s.namespace] || (n[s.namespace] = []), n[s.namespace].push(s); }), n; } function P(t, n = '"') { return t.replace(new RegExp(n, "g"), `\\${n}`); } function C(t) { return t !== null && typeof t == "object" && t.constructor === Object; } function V(t) { return t === null || typeof t != "object" && typeof t != "function" && !Array.isArray(t); } const j = Symbol("omitLine"), O = "__COLDSNIP_OMIT_LINE__"; function R() { return j; } class g { constructor(n, s = []) { this.literals = n, this.values = s; } static fromString(n) { return new g(N([n])); } get imports() { const n = []; return this.values.forEach((s) => { $(s) && n.push(s); }), n; } resolveNestedCode(n, s, c) { const o = n.toCodeString(c).split(` `), a = o.shift() ?? "", i = o.map((u) => s + u).join(` `); return a + ` ` + i; } resolveValues(n) { const s = this.imports, c = (e, o) => { var a; if (e instanceof b) return e.resolve(s); if (e === j) return O; if (e instanceof g) { const i = _(((a = this.literals[o]) == null ? void 0 : a.split(` `)) ?? []) ?? "", u = L(i); return this.resolveNestedCode(e, u, n).trim(); } return Array.isArray(e) ? e.map((i) => c(i, o)).join(` `) : $(e) ? e.toString() : e === !1 || e === void 0 ? "" : e; }; return this.values.map(c); } get unindent() { const n = this.literals[0], s = n.startsWith(` `) ? n.slice(1) : n; return L(s); } toString() { return this.toCodeString(); } toCodeString(n) { var a; const s = (a = this.imports[0]) == null ? void 0 : a.language, c = n ?? I(s), e = this.unindent, o = this.resolveValues(c); return String.raw(this.literals, ...o).split(` `).filter((i) => i.includes(O) === !1).map((i) => i.startsWith(e) ? i.substring(e.length).trimEnd() : i).join(` `).trim(); } } function F(t, ...n) { return new g(t, n); } export { g as C, b as I, C as a, V as b, F as c, P as e, I as f, w as g, $ as i, R as o, A as s }; //# sourceMappingURL=index-ZSW9UYQX.js.map