UNPKG

@ea-lab/reactive-json

Version:

A REACT-based lib that transforms JSON (or YAML) into interactive HTML markup.

120 lines (119 loc) 4.01 kB
import { useContext as c } from "react"; import { GlobalDataContext as y } from "./GlobalDataContext.js"; import { TemplateContext as P } from "./TemplateContext.js"; import { normalizeAttributesForReactJsx as d } from "./utility/reactJsxHelpers.js"; const A = ({ dataLocation: r, currentPath: t, globalDataContext: e, templateContext: s }) => { if (typeof r != "string" || !(r.startsWith("~.") || r.startsWith("~~.") || r.startsWith("~>") || r.startsWith("~~>"))) return r === "~" ? s.templatePath : r === "~~" ? e.templatePath : r; let f; if (r.startsWith("~~.")) f = e.templatePath; else if (r.startsWith("~.")) f = s.templatePath; else if (r.startsWith("~>") || r.startsWith("~~>")) { const h = r.startsWith("~>") ? "~>" : "~~>", m = r.indexOf("."), n = m === -1 ? r.substring(h.length) : r.substring(h.length, m); if (!s.templatePath.includes(n)) throw new Error( n + " not found in the current template path. The current template path is: " + s.templatePath ); let p; h === "~>" ? p = s.templatePath.lastIndexOf(n) : p = s.templatePath.indexOf(n), f = s.templatePath.substring(0, p + n.length); } else f = t; const i = r.split("."); return i.shift(), i.length ? f + "." + i.join(".") : f; }, I = ({ attrs: r, globalDataContext: t, templateContext: e, options: s = {} }) => { const f = {}; if (!r) return f; const i = s.normalizeBeforeEvaluation ? d(r) : r; for (const h of Object.keys(i)) { const m = u({ globalDataContext: t, templateContext: e, valueToEvaluate: i[h] }); m && (f[h] = m); } return f; }, W = ({ valueToEvaluate: r, globalDataContext: t, templateContext: e }) => { if (!j(r)) return r; if (r === "~") return e.templateData; if (r === "~~") return t.templateData; let s; if (r.startsWith("~~.") ? s = t == null ? void 0 : t.templateData : r.startsWith("~>") || r.startsWith("~~>") ? (r = A({ dataLocation: r, currentPath: e.templatePath, globalDataContext: t, templateContext: e }), s = t == null ? void 0 : t.templateData) : s = e == null ? void 0 : e.templateData, !s) return; const f = r.split("."); for (f.shift(); f.length; ) if (typeof s != "object" || s === null || (s = s[f.shift()], s === void 0)) return; return s; }, u = ({ valueToEvaluate: r, globalDataContext: t, templateContext: e, evaluationDepth: s = 1 }) => { if (s === 0 || s <= -20) return r; let i; if (typeof r == "object" && r !== null) { i = Array.isArray(r) ? [] : {}; for (const [h, m] of Object.entries(r)) { const n = W({ globalDataContext: t, templateContext: e, valueToEvaluate: m }); (s > 1 || s < 0) && typeof n == "object" && n !== null ? i[h] = u({ valueToEvaluate: n, globalDataContext: t, templateContext: e, evaluationDepth: s - 1 }) : i[h] = n; } } else i = W({ globalDataContext: t, templateContext: e, valueToEvaluate: r }), (s > 1 || s < 0) && typeof i == "object" && i !== null && (i = u({ valueToEvaluate: i, globalDataContext: t, templateContext: e, evaluationDepth: s - 1 })); return i; }, j = (r) => typeof r != "string" || !(r.startsWith("~.") || r.startsWith("~~.") || r.startsWith("~>") || r.startsWith("~~>") || r === "~" || r === "~~") ? !1 : r, B = ({ valueToEvaluate: r }) => { const t = c(y), e = c(P); return W({ globalDataContext: t, templateContext: e, valueToEvaluate: r }); }, w = (r, t = {}) => { const e = c(y), s = c(P); return I({ attrs: r, globalDataContext: e, templateContext: s, options: t.normalizeBeforeEvaluation === void 0 ? { ...t, normalizeBeforeEvaluation: !0 } : t }); }; export { A as dataLocationToPath, B as default, I as evaluateAttributes, W as evaluateTemplateValue, u as evaluateTemplateValueCollection, j as isTemplateValue, w as useEvaluatedAttributes };