From 369a35d92abaa9be0c20b874ac13d5322cfd2630 Mon Sep 17 00:00:00 2001 From: natxocc Date: Fri, 8 May 2026 12:47:51 +0200 Subject: [PATCH] add utils --- dist/sigpro.js | 23 ++++++++++++++++++++++- dist/sigpro.min.js | 2 +- dist/utils.js | 23 +++++++++++++++++++++++ docs/sigpro.js | 23 ++++++++++++++++++++++- package.json | 8 ++++++-- src/build_umd.js | 3 ++- src/utils.js | 23 +++++++++++++++++++++++ 7 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 dist/utils.js create mode 100644 src/utils.js diff --git a/dist/sigpro.js b/dist/sigpro.js index 4f7d878..d8a73f7 100644 --- a/dist/sigpro.js +++ b/dist/sigpro.js @@ -833,8 +833,29 @@ router.back = () => window.history.back(); router.path = () => window.location.hash.replace(/^#/, "") || "/"; + // src/utils.js + var db = async (url, data = {}, loading = null) => { + if (loading) + loading(true); + try { + const res = await fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data) + }); + if (!res.ok) { + const errorText = await res.text(); + throw new Error(`Error ${res.status}: ${errorText}`); + } + return await res.json(); + } finally { + if (loading) + loading(false); + } + }; + // src/build_umd.js if (typeof window !== "undefined") { - Object.assign(window, { $, watch, h, Fragment, when, each, router, mount, batch, onUnmount, isArr, isFunc, isObj }); + Object.assign(window, { $, watch, h, Fragment, when, each, router, mount, batch, onUnmount, isArr, isFunc, isObj, db }); } })(); diff --git a/dist/sigpro.min.js b/dist/sigpro.min.js index dca3eac..fc28a66 100644 --- a/dist/sigpro.min.js +++ b/dist/sigpro.min.js @@ -1 +1 @@ -(()=>{var I=Object.create;var{getPrototypeOf:Y,defineProperty:W,getOwnPropertyNames:Z}=Object;var H=Object.prototype.hasOwnProperty;function ee(e){return this[e]}var te,ne,re=(e,t,n)=>{var r=e!=null&&typeof e==="object";if(r){var s=t?te??=new WeakMap:ne??=new WeakMap,o=s.get(e);if(o)return o}n=e!=null?I(Y(e)):{};let i=t||!e||!e.__esModule?W(n,"default",{value:e,enumerable:!0}):n;for(let c of Z(e))if(!H.call(i,c))W(i,c,{get:ee.bind(e,c),enumerable:!0});if(r)s.set(e,i);return i};var se=((e)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var _=(e)=>typeof e==="function",L=(e)=>e&&typeof e==="object",w=Array.isArray,y=typeof document<"u"?document:null,q=(e)=>e?._isRuntime?e.container:e instanceof Node?e:y.createTextNode(e==null?"":String(e)),g=null,m=null,k=!1,x=0,G=new Set,j=new WeakMap,ie="http://www.w3.org/2000/svg",J="http://www.w3.org/1999/xlink",oe=new Set("svg,path,circle,rect,line,polyline,polygon,g,defs,text,textPath,tspan,use,symbol,image,marker,ellipse".split(",")),S=(e)=>{if(!e||e._disposed)return;e._disposed=!0;let t=[e];while(t.length){let n=t.pop();if(n._cleanups)n._cleanups.forEach((r)=>r()),n._cleanups.clear();if(n._children)n._children.forEach((r)=>t.push(r)),n._children.clear();if(n._deps)n._deps.forEach((r)=>r.delete(n)),n._deps.clear()}},A=(e)=>{if(m)(m._cleanups||=new Set).add(e)},ce=(e)=>{let t=g;g=null;try{return e()}finally{g=t}},T=(e,t=!1)=>{let n=()=>{if(n._disposed)return;if(n._deps)n._deps.forEach((o)=>o.delete(n));if(n._cleanups)n._cleanups.forEach((o)=>o()),n._cleanups.clear();let r=g,s=m;g=m=n;try{return n._result=e()}catch(o){console.error("[SigPro]",o)}finally{g=r,m=s}};if(n._deps=n._cleanups=n._children=null,n._disposed=!1,n._isComputed=t,n._depth=g?g._depth+1:0,n._mounts=[],n._parent=m,m)(m._children||=new Set).add(n);return n},M=()=>{if(k)return;k=!0;let e=Array.from(G).sort((t,n)=>t._depth-n._depth);G.clear();for(let t of e)if(!t._disposed)t();k=!1},P=(e)=>{x++;try{return e()}finally{if(x--,x===0&&G.size>0&&!k)M()}},p=(e,t=!1)=>{if(!t&&g&&!g._disposed)e.add(g),(g._deps||=new Set).add(e);else if(t&&e.size>0){let n=!1;for(let r of e){if(r===g||r._disposed)continue;if(r._isComputed){if(r._dirty=!0,r._subs)p(r._subs,!0)}else G.add(r),n=!0}if(n&&!k&&x===0)queueMicrotask(M)}},O=(e,t=null)=>{let n=new Set;if(_(e)){let r,s=()=>{if(s._dirty){let o=g;g=s;try{let i=e();if(!Object.is(r,i))r=i,p(n,!0)}finally{g=o}s._dirty=!1}return p(n),r};return s._isComputed=!0,s._subs=n,s._dirty=!0,s._deps=null,s._disposed=!1,s}if(t)try{e=JSON.parse(localStorage.getItem(t))??e}catch(r){}return(...r)=>{if(r.length){let s=_(r[0])?r[0](e):r[0];if(!Object.is(e,s)){if(e=s,t)localStorage.setItem(t,JSON.stringify(e));p(n,!0)}}return p(n),e}},N=(e,t)=>{if(t===void 0){let r=T(e);return r(),()=>S(r)}let n=T(()=>{let r=w(e)?e.map((s)=>s()):e();ce(()=>t(r))});return n(),()=>S(n)},z=(e)=>{if(!e)return;if(e._cleanups)e._cleanups.forEach((t)=>t()),e._cleanups.clear();if(e._ownerEffect)S(e._ownerEffect);if(e.childNodes)e.childNodes.forEach((t)=>z(t))},le=/^\s*(javascript|data|vbscript):/i,fe=new Set(["src","href","formaction","action","background","code","archive"]),ae=(e)=>fe.has(e)||e.startsWith("on"),U=(e,t)=>{if(t==null||t===!1)return null;if(ae(e)){let n=String(t);if(le.test(n))return"#"}return t},C=(e,t={},n=[])=>{if(t instanceof Node||w(t)||!L(t))n=t,t={};if(_(e)){let i=T(()=>{let f=e(t,{children:n,emit:(u,...h)=>t[`on${u[0].toUpperCase()}${u.slice(1)}`]?.(...h)});return i._result=f,f});i();let c=i._result;if(c==null)return null;let l=c instanceof Node||w(c)&&c.every((f)=>f instanceof Node)?c:y.createTextNode(String(c)),a=(f)=>{if(L(f)&&!f._isRuntime)f._mounts=i._mounts||[],f._cleanups=i._cleanups||new Set,f._ownerEffect=i};return w(l)?l.forEach(a):a(l),l}let r=oe.has(e),s=r?y.createElementNS(ie,e):y.createElement(e);s._cleanups=new Set;for(let i of Object.keys(t)){let c=t[i];if(i==="ref"){_(c)?c(s):c.current=s;continue}if(r&&i.startsWith("xlink:")){let l=U(i.slice(6),c);l==null?s.removeAttributeNS(J,i.slice(6)):s.setAttributeNS(J,i.slice(6),l);continue}if(i.startsWith("on")){let l=i.slice(2).toLowerCase();s.addEventListener(l,c);let a=()=>s.removeEventListener(l,c);s._cleanups.add(a),A(a)}else if(_(c)){let l=T(()=>{let a=U(i,c());if(i==="class")s.className=a||"";else if(a==null)s.removeAttribute(i);else if(i==="style"&&typeof a==="string")s.setAttribute("style",a);else if(i in s&&!r)s[i]=a;else s.setAttribute(i,a===!0?"":a)});if(l(),s._cleanups.add(()=>S(l)),A(()=>S(l)),/^(INPUT|TEXTAREA|SELECT)$/.test(s.tagName)&&(i==="value"||i==="checked")){let a=i==="checked"?"change":"input";s.addEventListener(a,(f)=>c(f.target[i]))}}else{let l=U(i,c);if(l!=null)if(i==="style"&&typeof l==="string")s.setAttribute("style",l);else if(i in s&&!r)s[i]=l;else s.setAttribute(i,l===!0?"":l)}}let o=(i)=>{if(w(i))return i.forEach(o);if(_(i)){let c=y.createTextNode("");s.appendChild(c);let l=[],a=T(()=>{let f=i(),u=(w(f)?f:[f]).map(q);l.forEach((d)=>{if(d._isRuntime)d.destroy();else z(d);if(d.parentNode)d.remove()});let h=c;for(let d=u.length-1;d>=0;d--){let v=u[d];if(v.parentNode!==h.parentNode)h.parentNode?.insertBefore(v,h);if(v._mounts)v._mounts.forEach((B)=>B());h=v}l=u});a(),s._cleanups.add(()=>S(a)),A(()=>S(a))}else{let c=q(i);if(s.appendChild(c),c._mounts)c._mounts.forEach((l)=>l())}};return o(n),s},R=(e)=>{let t=new Set,n=m,r=g,s=y.createElement("div");s.style.display="contents",s.setAttribute("role","presentation"),m={_cleanups:t},g=null;let o=(i)=>{if(!i)return;if(i._isRuntime)t.add(i.destroy),s.appendChild(i.container);else if(w(i))i.forEach(o);else s.appendChild(i instanceof Node?i:y.createTextNode(String(i==null?"":i)))};try{o(e({onCleanup:(i)=>t.add(i)}))}finally{m=n,g=r}return{_isRuntime:!0,container:s,destroy:()=>{t.forEach((i)=>i()),z(s),s.remove()}}},$=(e,t,n=null)=>{let r=y.createTextNode(""),s=C("div",{style:"display:contents"},[r]),o=null;return N(()=>!!(_(e)?e():e),(i)=>{if(o)o.destroy(),o=null;let c=i?t:n;if(c)o=R(()=>_(c)?c():c),s.insertBefore(o.container,r)}),A(()=>o?.destroy()),s},F=(e,t,n)=>{let r=y.createTextNode(""),s=C("div",{style:"display:contents"},[r]),o=new Map;return N(()=>(_(e)?e():e)||[],(i)=>{let c=new Map,l=[],a=i||[];for(let u=0;ut(h,u));else o.delete(d);c.set(d,v),l.push(v)}o.forEach((u)=>u.destroy());let f=r;for(let u=l.length-1;u>=0;u--){let d=l[u].container;if(d.nextSibling!==f)s.insertBefore(d,f);f=d}o=c}),s},Q=(e)=>e.children,X=(e,t)=>{let n=typeof t==="string"?y.querySelector(t):t;if(!n)return;if(j.has(n))j.get(n).destroy();let r=R(_(e)?e:()=>e);return n.replaceChildren(r.container),j.set(n,r),r};if(typeof window<"u")"a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((e)=>{window[e]=(t,n)=>C(e,t,n)});var Ce=(()=>({}));function b(e){if(typeof e!=="string")throw TypeError("Path must be a string. Received "+JSON.stringify(e))}function K(e,t){var n="",r=0,s=-1,o=0,i;for(var c=0;c<=e.length;++c){if(c2){var l=n.lastIndexOf("/");if(l!==n.length-1){if(l===-1)n="",r=0;else n=n.slice(0,l),r=n.length-1-n.lastIndexOf("/");s=c,o=0;continue}}else if(n.length===2||n.length===1){n="",r=0,s=c,o=0;continue}}if(t){if(n.length>0)n+="/..";else n="..";r=2}}else{if(n.length>0)n+="/"+e.slice(s+1,c);else n=e.slice(s+1,c);r=c-s-1}s=c,o=0}else if(i===46&&o!==-1)++o;else o=-1}return n}function ue(e,t){var n=t.dir||t.root,r=t.base||(t.name||"")+(t.ext||"");if(!n)return r;if(n===t.root)return n+r;return n+e+r}function D(){var e="",t=!1,n;for(var r=arguments.length-1;r>=-1&&!t;r--){var s;if(r>=0)s=arguments[r];else{if(n===void 0)n=process.cwd();s=n}if(b(s),s.length===0)continue;e=s+"/"+e,t=s.charCodeAt(0)===47}if(e=K(e,!t),t)if(e.length>0)return"/"+e;else return"/";else if(e.length>0)return e;else return"."}function V(e){if(b(e),e.length===0)return".";var t=e.charCodeAt(0)===47,n=e.charCodeAt(e.length-1)===47;if(e=K(e,!t),e.length===0&&!t)e=".";if(e.length>0&&n)e+="/";if(t)return"/"+e;return e}function de(e){return b(e),e.length>0&&e.charCodeAt(0)===47}function ge(){if(arguments.length===0)return".";var e;for(var t=0;t0)if(e===void 0)e=n;else e+="/"+n}if(e===void 0)return".";return V(e)}function he(e,t){if(b(e),b(t),e===t)return"";if(e=D(e),t=D(t),e===t)return"";var n=1;for(;nl){if(t.charCodeAt(o+f)===47)return t.slice(o+f+1);else if(f===0)return t.slice(o+f)}else if(s>l){if(e.charCodeAt(n+f)===47)a=f;else if(f===0)a=0}break}var u=e.charCodeAt(n+f),h=t.charCodeAt(o+f);if(u!==h)break;else if(u===47)a=f}var d="";for(f=n+a+1;f<=r;++f)if(f===r||e.charCodeAt(f)===47)if(d.length===0)d+="..";else d+="/..";if(d.length>0)return d+t.slice(o+a);else{if(o+=a,t.charCodeAt(o)===47)++o;return t.slice(o)}}function _e(e){return e}function me(e){if(b(e),e.length===0)return".";var t=e.charCodeAt(0),n=t===47,r=-1,s=!0;for(var o=e.length-1;o>=1;--o)if(t=e.charCodeAt(o),t===47){if(!s){r=o;break}}else s=!1;if(r===-1)return n?"/":".";if(n&&r===1)return"//";return e.slice(0,r)}function ve(e,t){if(t!==void 0&&typeof t!=="string")throw TypeError('"ext" argument must be a string');b(e);var n=0,r=-1,s=!0,o;if(t!==void 0&&t.length>0&&t.length<=e.length){if(t.length===e.length&&t===e)return"";var i=t.length-1,c=-1;for(o=e.length-1;o>=0;--o){var l=e.charCodeAt(o);if(l===47){if(!s){n=o+1;break}}else{if(c===-1)s=!1,c=o+1;if(i>=0)if(l===t.charCodeAt(i)){if(--i===-1)r=o}else i=-1,r=c}}if(n===r)r=c;else if(r===-1)r=e.length;return e.slice(n,r)}else{for(o=e.length-1;o>=0;--o)if(e.charCodeAt(o)===47){if(!s){n=o+1;break}}else if(r===-1)s=!1,r=o+1;if(r===-1)return"";return e.slice(n,r)}}function ye(e){b(e);var t=-1,n=0,r=-1,s=!0,o=0;for(var i=e.length-1;i>=0;--i){var c=e.charCodeAt(i);if(c===47){if(!s){n=i+1;break}continue}if(r===-1)s=!1,r=i+1;if(c===46){if(t===-1)t=i;else if(o!==1)o=1}else if(t!==-1)o=-1}if(t===-1||r===-1||o===0||o===1&&t===r-1&&t===n+1)return"";return e.slice(t,r)}function be(e){if(e===null||typeof e!=="object")throw TypeError('The "pathObject" argument must be of type Object. Received type '+typeof e);return ue("/",e)}function we(e){b(e);var t={root:"",dir:"",base:"",ext:"",name:""};if(e.length===0)return t;var n=e.charCodeAt(0),r=n===47,s;if(r)t.root="/",s=1;else s=0;var o=-1,i=0,c=-1,l=!0,a=e.length-1,f=0;for(;a>=s;--a){if(n=e.charCodeAt(a),n===47){if(!l){i=a+1;break}continue}if(c===-1)l=!1,c=a+1;if(n===46){if(o===-1)o=a;else if(f!==1)f=1}else if(o!==-1)f=-1}if(o===-1||c===-1||f===0||f===1&&o===c-1&&o===i+1){if(c!==-1)if(i===0&&r)t.base=t.name=e.slice(1,c);else t.base=t.name=e.slice(i,c)}else{if(i===0&&r)t.name=e.slice(1,o),t.base=e.slice(1,c);else t.name=e.slice(i,o),t.base=e.slice(i,c);t.ext=e.slice(o,c)}if(i>0)t.dir=e.slice(0,i-1);else if(r)t.dir="/";return t}var Ae="/",Se=":",pe=((e)=>(e.posix=e,e))({resolve:D,normalize:V,isAbsolute:de,join:ge,relative:he,_makeLong:_e,dirname:me,basename:ve,extname:ye,format:be,parse:we,sep:Ae,delimiter:Se,win32:null,posix:null});var E=(e)=>{let t=()=>window.location.hash.slice(1)||"/",n=O(t()),r=()=>n(t());window.addEventListener("hashchange",r),A(()=>window.removeEventListener("hashchange",r));let s=C("div",{class:"router-hook"}),o=null;return N([n],()=>{let i=n(),c=e.find((l)=>{let a=l.path.split("/").filter(Boolean),f=i.split("/").filter(Boolean);return a.length===f.length&&a.every((u,h)=>u[0]===":"||u===f[h])})||e.find((l)=>l.path==="*");if(c){o?.destroy();let l={};c.path.split("/").filter(Boolean).forEach((a,f)=>{if(a[0]===":")l[a.slice(1)]=i.split("/").filter(Boolean)[f]}),E.params(l),o=R(()=>_(c.component)?c.component(l):c.component),s.replaceChildren(o.container)}}),s};E.params=O({});E.to=(e)=>window.location.hash=e.replace(/^#?\/?/,"#/");E.back=()=>window.history.back();E.path=()=>window.location.hash.replace(/^#/,"")||"/";if(typeof window<"u")Object.assign(window,{$:O,watch:N,h:C,Fragment:Q,when:$,each:F,router:E,mount:X,batch:P,onUnmount:A,isArr:w,isFunc:_,isObj:L});})(); +(()=>{var Y=Object.create;var{getPrototypeOf:Z,defineProperty:D,getOwnPropertyNames:H}=Object;var ee=Object.prototype.hasOwnProperty;function te(e){return this[e]}var ne,re,se=(e,n,t)=>{var r=e!=null&&typeof e==="object";if(r){var s=n?ne??=new WeakMap:re??=new WeakMap,o=s.get(e);if(o)return o}t=e!=null?Y(Z(e)):{};let i=n||!e||!e.__esModule?D(t,"default",{value:e,enumerable:!0}):t;for(let c of H(e))if(!ee.call(i,c))D(i,c,{get:te.bind(e,c),enumerable:!0});if(r)s.set(e,i);return i};var ie=((e)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(n,t)=>(typeof require<"u"?require:n)[t]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var _=(e)=>typeof e==="function",L=(e)=>e&&typeof e==="object",w=Array.isArray,y=typeof document<"u"?document:null,J=(e)=>e?._isRuntime?e.container:e instanceof Node?e:y.createTextNode(e==null?"":String(e)),h=null,g=null,k=!1,R=0,j=new Set,G=new WeakMap,oe="http://www.w3.org/2000/svg",P="http://www.w3.org/1999/xlink",ce=new Set("svg,path,circle,rect,line,polyline,polygon,g,defs,text,textPath,tspan,use,symbol,image,marker,ellipse".split(",")),A=(e)=>{if(!e||e._disposed)return;e._disposed=!0;let n=[e];while(n.length){let t=n.pop();if(t._cleanups)t._cleanups.forEach((r)=>r()),t._cleanups.clear();if(t._children)t._children.forEach((r)=>n.push(r)),t._children.clear();if(t._deps)t._deps.forEach((r)=>r.delete(t)),t._deps.clear()}},S=(e)=>{if(g)(g._cleanups||=new Set).add(e)},le=(e)=>{let n=h;h=null;try{return e()}finally{h=n}},T=(e,n=!1)=>{let t=()=>{if(t._disposed)return;if(t._deps)t._deps.forEach((o)=>o.delete(t));if(t._cleanups)t._cleanups.forEach((o)=>o()),t._cleanups.clear();let r=h,s=g;h=g=t;try{return t._result=e()}catch(o){console.error("[SigPro]",o)}finally{h=r,g=s}};if(t._deps=t._cleanups=t._children=null,t._disposed=!1,t._isComputed=n,t._depth=h?h._depth+1:0,t._mounts=[],t._parent=g,g)(g._children||=new Set).add(t);return t},W=()=>{if(k)return;k=!0;let e=Array.from(j).sort((n,t)=>n._depth-t._depth);j.clear();for(let n of e)if(!n._disposed)n();k=!1},q=(e)=>{R++;try{return e()}finally{if(R--,R===0&&j.size>0&&!k)W()}},p=(e,n=!1)=>{if(!n&&h&&!h._disposed)e.add(h),(h._deps||=new Set).add(e);else if(n&&e.size>0){let t=!1;for(let r of e){if(r===h||r._disposed)continue;if(r._isComputed){if(r._dirty=!0,r._subs)p(r._subs,!0)}else j.add(r),t=!0}if(t&&!k&&R===0)queueMicrotask(W)}},O=(e,n=null)=>{let t=new Set;if(_(e)){let r,s=()=>{if(s._dirty){let o=h;h=s;try{let i=e();if(!Object.is(r,i))r=i,p(t,!0)}finally{h=o}s._dirty=!1}return p(t),r};return s._isComputed=!0,s._subs=t,s._dirty=!0,s._deps=null,s._disposed=!1,s}if(n)try{e=JSON.parse(localStorage.getItem(n))??e}catch(r){}return(...r)=>{if(r.length){let s=_(r[0])?r[0](e):r[0];if(!Object.is(e,s)){if(e=s,n)localStorage.setItem(n,JSON.stringify(e));p(t,!0)}}return p(t),e}},N=(e,n)=>{if(n===void 0){let r=T(e);return r(),()=>A(r)}let t=T(()=>{let r=w(e)?e.map((s)=>s()):e();le(()=>n(r))});return t(),()=>A(t)},$=(e)=>{if(!e)return;if(e._cleanups)e._cleanups.forEach((n)=>n()),e._cleanups.clear();if(e._ownerEffect)A(e._ownerEffect);if(e.childNodes)e.childNodes.forEach((n)=>$(n))},fe=/^\s*(javascript|data|vbscript):/i,ae=new Set(["src","href","formaction","action","background","code","archive"]),ue=(e)=>ae.has(e)||e.startsWith("on"),U=(e,n)=>{if(n==null||n===!1)return null;if(ue(e)){let t=String(n);if(fe.test(t))return"#"}return n},C=(e,n={},t=[])=>{if(n instanceof Node||w(n)||!L(n))t=n,n={};if(_(e)){let i=T(()=>{let f=e(n,{children:t,emit:(u,...m)=>n[`on${u[0].toUpperCase()}${u.slice(1)}`]?.(...m)});return i._result=f,f});i();let c=i._result;if(c==null)return null;let l=c instanceof Node||w(c)&&c.every((f)=>f instanceof Node)?c:y.createTextNode(String(c)),a=(f)=>{if(L(f)&&!f._isRuntime)f._mounts=i._mounts||[],f._cleanups=i._cleanups||new Set,f._ownerEffect=i};return w(l)?l.forEach(a):a(l),l}let r=ce.has(e),s=r?y.createElementNS(oe,e):y.createElement(e);s._cleanups=new Set;for(let i of Object.keys(n)){let c=n[i];if(i==="ref"){_(c)?c(s):c.current=s;continue}if(r&&i.startsWith("xlink:")){let l=U(i.slice(6),c);l==null?s.removeAttributeNS(P,i.slice(6)):s.setAttributeNS(P,i.slice(6),l);continue}if(i.startsWith("on")){let l=i.slice(2).toLowerCase();s.addEventListener(l,c);let a=()=>s.removeEventListener(l,c);s._cleanups.add(a),S(a)}else if(_(c)){let l=T(()=>{let a=U(i,c());if(i==="class")s.className=a||"";else if(a==null)s.removeAttribute(i);else if(i==="style"&&typeof a==="string")s.setAttribute("style",a);else if(i in s&&!r)s[i]=a;else s.setAttribute(i,a===!0?"":a)});if(l(),s._cleanups.add(()=>A(l)),S(()=>A(l)),/^(INPUT|TEXTAREA|SELECT)$/.test(s.tagName)&&(i==="value"||i==="checked")){let a=i==="checked"?"change":"input";s.addEventListener(a,(f)=>c(f.target[i]))}}else{let l=U(i,c);if(l!=null)if(i==="style"&&typeof l==="string")s.setAttribute("style",l);else if(i in s&&!r)s[i]=l;else s.setAttribute(i,l===!0?"":l)}}let o=(i)=>{if(w(i))return i.forEach(o);if(_(i)){let c=y.createTextNode("");s.appendChild(c);let l=[],a=T(()=>{let f=i(),u=(w(f)?f:[f]).map(J);l.forEach((d)=>{if(d._isRuntime)d.destroy();else $(d);if(d.parentNode)d.remove()});let m=c;for(let d=u.length-1;d>=0;d--){let v=u[d];if(v.parentNode!==m.parentNode)m.parentNode?.insertBefore(v,m);if(v._mounts)v._mounts.forEach((I)=>I());m=v}l=u});a(),s._cleanups.add(()=>A(a)),S(()=>A(a))}else{let c=J(i);if(s.appendChild(c),c._mounts)c._mounts.forEach((l)=>l())}};return o(t),s},x=(e)=>{let n=new Set,t=g,r=h,s=y.createElement("div");s.style.display="contents",s.setAttribute("role","presentation"),g={_cleanups:n},h=null;let o=(i)=>{if(!i)return;if(i._isRuntime)n.add(i.destroy),s.appendChild(i.container);else if(w(i))i.forEach(o);else s.appendChild(i instanceof Node?i:y.createTextNode(String(i==null?"":i)))};try{o(e({onCleanup:(i)=>n.add(i)}))}finally{g=t,h=r}return{_isRuntime:!0,container:s,destroy:()=>{n.forEach((i)=>i()),$(s),s.remove()}}},M=(e,n,t=null)=>{let r=y.createTextNode(""),s=C("div",{style:"display:contents"},[r]),o=null;return N(()=>!!(_(e)?e():e),(i)=>{if(o)o.destroy(),o=null;let c=i?n:t;if(c)o=x(()=>_(c)?c():c),s.insertBefore(o.container,r)}),S(()=>o?.destroy()),s},F=(e,n,t)=>{let r=y.createTextNode(""),s=C("div",{style:"display:contents"},[r]),o=new Map;return N(()=>(_(e)?e():e)||[],(i)=>{let c=new Map,l=[],a=i||[];for(let u=0;un(m,u));else o.delete(d);c.set(d,v),l.push(v)}o.forEach((u)=>u.destroy());let f=r;for(let u=l.length-1;u>=0;u--){let d=l[u].container;if(d.nextSibling!==f)s.insertBefore(d,f);f=d}o=c}),s},Q=(e)=>e.children,X=(e,n)=>{let t=typeof n==="string"?y.querySelector(n):n;if(!t)return;if(G.has(t))G.get(t).destroy();let r=x(_(e)?e:()=>e);return t.replaceChildren(r.container),G.set(t,r),r};if(typeof window<"u")"a abbr article aside audio b blockquote br button canvas caption cite code col colgroup datalist dd del details dfn dialog div dl dt em embed fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hr i iframe img input ins kbd label legend li main mark meter nav object ol optgroup option output p picture pre progress section select slot small source span strong sub summary sup svg table tbody td template textarea tfoot th thead time tr u ul video".split(" ").forEach((e)=>{window[e]=(n,t)=>C(e,n,t)});var Ee=(()=>({}));function b(e){if(typeof e!=="string")throw TypeError("Path must be a string. Received "+JSON.stringify(e))}function K(e,n){var t="",r=0,s=-1,o=0,i;for(var c=0;c<=e.length;++c){if(c2){var l=t.lastIndexOf("/");if(l!==t.length-1){if(l===-1)t="",r=0;else t=t.slice(0,l),r=t.length-1-t.lastIndexOf("/");s=c,o=0;continue}}else if(t.length===2||t.length===1){t="",r=0,s=c,o=0;continue}}if(n){if(t.length>0)t+="/..";else t="..";r=2}}else{if(t.length>0)t+="/"+e.slice(s+1,c);else t=e.slice(s+1,c);r=c-s-1}s=c,o=0}else if(i===46&&o!==-1)++o;else o=-1}return t}function de(e,n){var t=n.dir||n.root,r=n.base||(n.name||"")+(n.ext||"");if(!t)return r;if(t===n.root)return t+r;return t+e+r}function z(){var e="",n=!1,t;for(var r=arguments.length-1;r>=-1&&!n;r--){var s;if(r>=0)s=arguments[r];else{if(t===void 0)t=process.cwd();s=t}if(b(s),s.length===0)continue;e=s+"/"+e,n=s.charCodeAt(0)===47}if(e=K(e,!n),n)if(e.length>0)return"/"+e;else return"/";else if(e.length>0)return e;else return"."}function V(e){if(b(e),e.length===0)return".";var n=e.charCodeAt(0)===47,t=e.charCodeAt(e.length-1)===47;if(e=K(e,!n),e.length===0&&!n)e=".";if(e.length>0&&t)e+="/";if(n)return"/"+e;return e}function he(e){return b(e),e.length>0&&e.charCodeAt(0)===47}function me(){if(arguments.length===0)return".";var e;for(var n=0;n0)if(e===void 0)e=t;else e+="/"+t}if(e===void 0)return".";return V(e)}function _e(e,n){if(b(e),b(n),e===n)return"";if(e=z(e),n=z(n),e===n)return"";var t=1;for(;tl){if(n.charCodeAt(o+f)===47)return n.slice(o+f+1);else if(f===0)return n.slice(o+f)}else if(s>l){if(e.charCodeAt(t+f)===47)a=f;else if(f===0)a=0}break}var u=e.charCodeAt(t+f),m=n.charCodeAt(o+f);if(u!==m)break;else if(u===47)a=f}var d="";for(f=t+a+1;f<=r;++f)if(f===r||e.charCodeAt(f)===47)if(d.length===0)d+="..";else d+="/..";if(d.length>0)return d+n.slice(o+a);else{if(o+=a,n.charCodeAt(o)===47)++o;return n.slice(o)}}function ge(e){return e}function ve(e){if(b(e),e.length===0)return".";var n=e.charCodeAt(0),t=n===47,r=-1,s=!0;for(var o=e.length-1;o>=1;--o)if(n=e.charCodeAt(o),n===47){if(!s){r=o;break}}else s=!1;if(r===-1)return t?"/":".";if(t&&r===1)return"//";return e.slice(0,r)}function ye(e,n){if(n!==void 0&&typeof n!=="string")throw TypeError('"ext" argument must be a string');b(e);var t=0,r=-1,s=!0,o;if(n!==void 0&&n.length>0&&n.length<=e.length){if(n.length===e.length&&n===e)return"";var i=n.length-1,c=-1;for(o=e.length-1;o>=0;--o){var l=e.charCodeAt(o);if(l===47){if(!s){t=o+1;break}}else{if(c===-1)s=!1,c=o+1;if(i>=0)if(l===n.charCodeAt(i)){if(--i===-1)r=o}else i=-1,r=c}}if(t===r)r=c;else if(r===-1)r=e.length;return e.slice(t,r)}else{for(o=e.length-1;o>=0;--o)if(e.charCodeAt(o)===47){if(!s){t=o+1;break}}else if(r===-1)s=!1,r=o+1;if(r===-1)return"";return e.slice(t,r)}}function be(e){b(e);var n=-1,t=0,r=-1,s=!0,o=0;for(var i=e.length-1;i>=0;--i){var c=e.charCodeAt(i);if(c===47){if(!s){t=i+1;break}continue}if(r===-1)s=!1,r=i+1;if(c===46){if(n===-1)n=i;else if(o!==1)o=1}else if(n!==-1)o=-1}if(n===-1||r===-1||o===0||o===1&&n===r-1&&n===t+1)return"";return e.slice(n,r)}function we(e){if(e===null||typeof e!=="object")throw TypeError('The "pathObject" argument must be of type Object. Received type '+typeof e);return de("/",e)}function Se(e){b(e);var n={root:"",dir:"",base:"",ext:"",name:""};if(e.length===0)return n;var t=e.charCodeAt(0),r=t===47,s;if(r)n.root="/",s=1;else s=0;var o=-1,i=0,c=-1,l=!0,a=e.length-1,f=0;for(;a>=s;--a){if(t=e.charCodeAt(a),t===47){if(!l){i=a+1;break}continue}if(c===-1)l=!1,c=a+1;if(t===46){if(o===-1)o=a;else if(f!==1)f=1}else if(o!==-1)f=-1}if(o===-1||c===-1||f===0||f===1&&o===c-1&&o===i+1){if(c!==-1)if(i===0&&r)n.base=n.name=e.slice(1,c);else n.base=n.name=e.slice(i,c)}else{if(i===0&&r)n.name=e.slice(1,o),n.base=e.slice(1,c);else n.name=e.slice(i,o),n.base=e.slice(i,c);n.ext=e.slice(o,c)}if(i>0)n.dir=e.slice(0,i-1);else if(r)n.dir="/";return n}var Ae="/",Ce=":",ke=((e)=>(e.posix=e,e))({resolve:z,normalize:V,isAbsolute:he,join:me,relative:_e,_makeLong:ge,dirname:ve,basename:ye,extname:be,format:we,parse:Se,sep:Ae,delimiter:Ce,win32:null,posix:null});var E=(e)=>{let n=()=>window.location.hash.slice(1)||"/",t=O(n()),r=()=>t(n());window.addEventListener("hashchange",r),S(()=>window.removeEventListener("hashchange",r));let s=C("div",{class:"router-hook"}),o=null;return N([t],()=>{let i=t(),c=e.find((l)=>{let a=l.path.split("/").filter(Boolean),f=i.split("/").filter(Boolean);return a.length===f.length&&a.every((u,m)=>u[0]===":"||u===f[m])})||e.find((l)=>l.path==="*");if(c){o?.destroy();let l={};c.path.split("/").filter(Boolean).forEach((a,f)=>{if(a[0]===":")l[a.slice(1)]=i.split("/").filter(Boolean)[f]}),E.params(l),o=x(()=>_(c.component)?c.component(l):c.component),s.replaceChildren(o.container)}}),s};E.params=O({});E.to=(e)=>window.location.hash=e.replace(/^#?\/?/,"#/");E.back=()=>window.history.back();E.path=()=>window.location.hash.replace(/^#/,"")||"/";var B=async(e,n={},t=null)=>{if(t)t(!0);try{let r=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)});if(!r.ok){let s=await r.text();throw Error(`Error ${r.status}: ${s}`)}return await r.json()}finally{if(t)t(!1)}};if(typeof window<"u")Object.assign(window,{$:O,watch:N,h:C,Fragment:Q,when:M,each:F,router:E,mount:X,batch:q,onUnmount:S,isArr:w,isFunc:_,isObj:L,db:B});})(); diff --git a/dist/utils.js b/dist/utils.js new file mode 100644 index 0000000..ffc43b9 --- /dev/null +++ b/dist/utils.js @@ -0,0 +1,23 @@ +// src/utils.js +var db = async (url, data = {}, loading = null) => { + if (loading) + loading(true); + try { + const res = await fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data) + }); + if (!res.ok) { + const errorText = await res.text(); + throw new Error(`Error ${res.status}: ${errorText}`); + } + return await res.json(); + } finally { + if (loading) + loading(false); + } +}; +export { + db +}; diff --git a/docs/sigpro.js b/docs/sigpro.js index 4f7d878..d8a73f7 100644 --- a/docs/sigpro.js +++ b/docs/sigpro.js @@ -833,8 +833,29 @@ router.back = () => window.history.back(); router.path = () => window.location.hash.replace(/^#/, "") || "/"; + // src/utils.js + var db = async (url, data = {}, loading = null) => { + if (loading) + loading(true); + try { + const res = await fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data) + }); + if (!res.ok) { + const errorText = await res.text(); + throw new Error(`Error ${res.status}: ${errorText}`); + } + return await res.json(); + } finally { + if (loading) + loading(false); + } + }; + // src/build_umd.js if (typeof window !== "undefined") { - Object.assign(window, { $, watch, h, Fragment, when, each, router, mount, batch, onUnmount, isArr, isFunc, isObj }); + Object.assign(window, { $, watch, h, Fragment, when, each, router, mount, batch, onUnmount, isArr, isFunc, isObj, db }); } })(); diff --git a/package.json b/package.json index d0a07af..c13be03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sigpro", - "version": "1.2.36", + "version": "1.2.37", "type": "module", "license": "MIT", "author": { @@ -21,6 +21,9 @@ }, "./router": { "import": "./dist/router.js" + }, + "./utils": { + "import": "./dist/utils.js" } }, "files": [ @@ -47,8 +50,9 @@ "build:esm": "bun build ./src/sigpro.js --bundle --outfile=./dist/sigpro.esm.js --format=esm", "build:esm:min": "bun build ./src/sigpro.js --bundle --outfile=./dist/sigpro.esm.min.js --format=esm --minify", "build:router": "bun build ./src/router.js --bundle --outfile=./dist/router.js --format=esm --external:fs --external:path", + "build:utils": "bun build ./src/utils.js --bundle --outfile=./dist/utils.js --format=esm --external:fs --external:path", "build:copy": "cp ./dist/sigpro.js ./docs/sigpro.js", - "build": "bun run build:iife && bun run build:iife:min && bun run build:esm && bun run build:esm:min && bun run build:router && bun run build:copy", + "build": "bun run build:iife && bun run build:iife:min && bun run build:esm && bun run build:esm:min && bun run build:router && bun run build:utils && bun run build:copy", "docs": "bun x serve docs" }, "keywords": [ diff --git a/src/build_umd.js b/src/build_umd.js index db3a81c..9e5c87c 100644 --- a/src/build_umd.js +++ b/src/build_umd.js @@ -1,6 +1,7 @@ import { $, watch, batch, h, Fragment, mount, when, each, onUnmount, isArr, isFunc, isObj } from "./sigpro.js" import { router } from "./router.js" +import { db } from "./utils.js" if (typeof window !== "undefined") { - Object.assign(window, { $, watch, h, Fragment, when, each, router, mount, batch, onUnmount, isArr, isFunc, isObj }) + Object.assign(window, { $, watch, h, Fragment, when, each, router, mount, batch, onUnmount, isArr, isFunc, isObj, db }) } \ No newline at end of file diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..d22da8e --- /dev/null +++ b/src/utils.js @@ -0,0 +1,23 @@ +// utils.js + +export const db = async (url, data = {}, loading = null) => { + if (loading) loading(true); + + try { + const res = await fetch(url, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(data) + }); + + if (!res.ok) { + const errorText = await res.text(); + throw new Error(`Error ${res.status}: ${errorText}`); + } + + return await res.json(); + + } finally { + if (loading) loading(false); + } +}; \ No newline at end of file