UNPKG

@qtpy/state-management-observable

Version:

1. [Основная идея и архитектура](#основная-идея-и-архитектура) 1.1. [Прозрачная реактивность](#прозрачная-реактивность) 1.2. [Поддержка типизированных строковых ключей](#поддержка-типизированных-строковых-ключей) 1.3. [Подписки с «гранул

203 lines (202 loc) 5.73 kB
function A(e) { const t = e.match(/\((.*?)\)/); return t ? t[1].split(/\s*,\s*/).filter(Boolean) : []; } function O(e, t) { const n = t.toString().trim(), o = A(n), s = o.map((l) => l.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")), r = n.match( new RegExp( `^\\s*(?:\\(\\s*${s[0]}\\s*(?:,\\s*${s[1]}\\s*)?\\s*\\)|${s[0]})\\s*=>\\s*([\\s\\S]+)$` ) ); if (!r) throw new Error("Invalid function format"); let c = r[1].trim(); c.startsWith("(") && c.endsWith(")") && (c = c.slice(1, -1).trim()); let i = c.replace(/\s+/g, ""); const u = /\[([^\[\]]+),([^\[\]]+)\]/; for (; u.test(i); ) i = i.replace(/\[([^\[\]]+),([^\[\]]+)\]/g, "[$1][$2]"); if (o.length < 2) { let l = i.replace(/\[['"]([^[\]]+)['"]\]/g, ".$1").replace(/\[([^[\]]+)\]/g, ".$1"); return l.startsWith(".") && (l = l.slice(1)), l; } const a = o[1], m = a.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), j = new RegExp(`${m}\\(\\s*([^\\)]+)\\s*\\)`), $ = new RegExp(`${m}\\(\\s*([^\\)]+)\\s*\\)`, "g"); if (!j.test(i)) { let l = i.replace(/\[['"]([^[\]]+)['"]\]/g, ".$1").replace(/\[([^[\]]+)\]/g, ".$1"); return l.startsWith(".") && (l = l.slice(1)), l; } const d = []; let S; for (; (S = $.exec(i)) !== null; ) d.push(S[1]); const w = []; let v = 0; function k(l) { const f = d[v]; return w.push([`${a}(${f})`, l]), v++, l; } try { t(e, k); } catch { } const x = {}; for (const [l, f] of w) { const p = l.slice(a.length + 1, -1); x[p] = f; } let g = i.replace($, (l, f) => { const p = x[f]; return p === void 0 ? "undefined" : String(p); }); g = g.replace(/\[['"]([\w$]+)['"]\]/g, ".$1").replace(/\[([\w$]+)\]/g, ".$1"); const y = g.replace(/\.\./g, "."); return y.startsWith(".") ? y.slice(1) : y; } function W(e) { if (!(typeof e == "string") && !(typeof e == "function")) { const o = e === null ? "null" : typeof e, s = typeof e == "object" ? JSON.stringify(Object.keys(e)) : String(e); throw new Error(`[store] Ожидался путь (строка или функция-доступ), получено: ${o} (${s}).`); } } function M(e, t) { let n; typeof t == "string" ? n = t : n = O(e, t); let o = "$"; if (typeof t != "string") { const c = t.toString().trim(); o = A(c)[0] || "$"; } const s = `${o}.`, r = n.indexOf(s); return r >= 0 ? n.slice(r + s.length) : n; } function D(e, t) { return Array.isArray(e) ? e.map((n) => D(n, t)).filter((n) => n !== "").join(".") : typeof e == "function" ? M(t, e) : e == null ? "" : String(e); } function _(e, t) { if (e === t) return !0; if (e == null || t == null) return e === t; if (typeof e != typeof t) return !1; if (e instanceof Date && t instanceof Date) return e.getTime() === t.getTime(); if (typeof e != "object") return !1; const n = Object.keys(e), o = Object.keys(t); if (n.length !== o.length) return !1; for (let s = 0; s < n.length; s++) { const r = n[s]; if (!Object.prototype.hasOwnProperty.call(t, r) || e[r] !== t[r]) return !1; } return !0; } function C(e) { return e.split(".").map((t) => /^\d+$/.test(t) ? Number(t) : t); } function R(e, t = []) { switch (e) { case "push": case "pop": case "splice": case "shift": case "unshift": case "sort": case "reverse": return !t.length || t.includes(e); } return !1; } function h(e, t) { return e === null || !(Array.isArray(e) || typeof e == "object" || typeof e == "function") ? !1 : t == null ? void 0 : t(); } function I(e, t, n) { h(t, () => e.set(t, n)); } function N(e, t) { return h(t, () => e.get(t)); } function U(e, t) { h(t, () => e.delete(t)); } function T(e, t) { return h(t, () => { const n = N(e, t) ?? {}; I(e, t, { revision: crypto.randomUUID(), _prevRevision: n.revision ?? null }); }), t; } function z(e) { try { const t = P(e); let n = 5381; for (let o = 0; o < t.length; o++) { const s = t.charCodeAt(o); n = ((n << 5) + n ^ s) >>> 0; } return (n >>> 0).toString(16); } catch { return !1; } } function P(e) { const t = [{ value: e }], n = [], o = /* @__PURE__ */ new WeakSet(); for (; t.length > 0; ) { const s = t.pop(); if (typeof s == "string") { n.push(s); continue; } let { value: r, parentIsArray: c } = s; if (r == null || typeof r != "object") { r === void 0 ? n.push(c ? "null" : "") : typeof r == "number" ? n.push(isFinite(r) ? r.toString() : "null") : n.push(JSON.stringify(r)); continue; } if (o.has(r)) { n.push('"__cycle__"'); continue; } if (o.add(r), Array.isArray(r)) { if (r.length === 0) { n.push("[]"); continue; } t.push("]"); for (let i = r.length - 1; i >= 0; i--) t.push({ value: r[i], parentIsArray: !0 }), i > 0 && t.push(","); t.push("["); } else { const i = Object.keys(r); if (i.length === 0) { n.push("{}"); continue; } t.push("}"); for (let u = i.length - 1; u >= 0; u--) { const a = i[u]; t.push({ value: r[a] }), t.push(`"${a}":`), u > 0 && t.push(","); } t.push("{"); } } return n.join(""); } function B() { return `${Math.floor(performance.now()).toString(36)}-${Math.random().toString(36).slice(2, 6)}`; } export { z as calculateSnapshotHash, U as deleteMetaData, B as detRandomId, N as getMetaData, O as getStringOfObject, M as getStringPath, R as isArrayMethod, D as normalizeCacheKey, I as setMetaData, _ as shallowEqual, C as splitPath, P as stringify, W as validatePath, h as withMetaSupport, T as wrapWithMetaUsingUUID };