Comparing version 0.0.1 to 0.0.2
import { useState, useEffect } from 'react'; | ||
const g="USEPARAMS",l="popstate",w="?s",p="?n",d="?b",h="true";const pmask={str:w,num:p,bool:d};const U=()=>window&&window.history||{},P=()=>new URLSearchParams(location.search),k=t=>Object.keys(t),y=()=>{const t=U().state;return t&&t[g]?t[g]:[...P()].reduce((s,[r,m])=>({...s,[r]:m}),{})},V=t=>new URL(`?${t.toString()}`,location).toString(),S=(t,s,r)=>{t&&dispatchEvent(new CustomEvent(l,{detail:{param:t}})),U().pushState({[g]:r},"",V(s));};const removeParam=t=>{const s=y(),r=P();r.delete(t),delete s[t],S(t,r,s);},setParam=(t,s)=>{const r=P();r.set(t,s+""),S(t,r,{...y(),[t]:s});},useParams=t=>{const[s,r]=useState(0),[m,L]=useState({});return useEffect(()=>{const c=y(),n=P(),o={};let u="";const R=e=>{const a=t[e],f=c[e],i=n.get(e),A=e in t;if(i!=="undefined")return [w,p,d].includes(a)?(o[e]=f,a==p?i*1:a==d?i==h:i):(A&&i===null&&(u=h,n.set(e,a)),f===void 0?a:f)},T=[...k(c),...k(t)].reduce((e,a)=>({...e,[a]:R(a)}),{});L(T);const x={...T,...o};S(u,n,x);},[s]),useEffect(()=>{function c({detail:n}){const o=n&&n.param;(!o||o in t||o===h)&&r(u=>u+1);}return addEventListener(l,c),()=>removeEventListener(l,c)},[]),m}; | ||
const c="USEPARAMS",w="popstate",k="?s",y="?n",h="?b",v="true";const pmask={str:k,num:y,bool:h};const U=()=>null,C=()=>typeof window<"u"?window.history:{},d=()=>new URLSearchParams(location.search),L=t=>Object.keys(t),S=()=>{const t=C().state;return t&&t[c]?t[c]:window[c]?window[c]:[...d()].reduce((e,[s,l])=>({...e,[s]:l}),{})},O=t=>new URL(`?${t.toString()}`,location).toString(),E=(t,e,s)=>{t&&(dispatchEvent(new CustomEvent(w,{detail:{param:t}})),window[c]=s,C().pushState({[c]:s},"",O(e)));};const removeParam=t=>{const e=S(),s=d();s.delete(t),delete e[t],E(t,s,e);},setParam=(t,e)=>{const s=d();s.set(t,e+""),E(t,s,{...S(),[t]:e});},useParams=t=>{const[e,s]=useState(0),[l,A]=useState({});return useEffect(()=>{const m=S(),i=d(),u={};let r="";const P=n=>{const o=t[n],p=m[n],f=i.get(n),V=n in t,T=p===void 0?o:p;if(f!=="undefined")return [k,y,h].includes(o)?(u[n]=p,o==y?f*1:o==h?f==v:f):(V&&f===null&&(r=v,i.set(n,T)),T)},a=[...L(m),...L(t)].reduce((n,o)=>({...n,[o]:P(o)}),{});A(a);const g={...a,...u};E(r,i,g);},[e]),useEffect(()=>{const m="next"in window&&window.next,i={events:{on:U,off:U}},u=m.router||i;function r({detail:P}){const a=P&&P.param;(!a||a in t||a===v)&&s(g=>g+1);}return addEventListener(w,r),u.events.on("routeChangeComplete",r),()=>{removeEventListener(w,r),u.events.off("routeChangeComplete",r);}},[]),l}; | ||
export { pmask, removeParam, setParam, useParams }; |
@@ -1,1 +0,1 @@ | ||
(function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.useparams={},s.React))})(this,function(s,o){"use strict";const l="USEPARAMS",p="popstate",b="?s",S="?n",v="?b",P="true",k={str:b,num:S,bool:v},g=()=>window&&window.history||{},m=()=>new URLSearchParams(location.search),L=e=>Object.keys(e),h=()=>{const e=g().state;return e&&e[l]?e[l]:[...m()].reduce((t,[n,E])=>({...t,[n]:E}),{})},T=e=>new URL(`?${e.toString()}`,location).toString(),w=(e,t,n)=>{e&&dispatchEvent(new CustomEvent(p,{detail:{param:e}})),g().pushState({[l]:n},"",T(t))},U=e=>{const t=h(),n=m();n.delete(e),delete t[e],w(e,n,t)},j=(e,t)=>{const n=m();n.set(e,t+""),w(e,n,{...h(),[e]:t})},A=e=>{const[t,n]=o.useState(0),[E,M]=o.useState({});return o.useEffect(()=>{const c=h(),i=m(),u={};let f="";const O=r=>{const a=e[r],y=c[r],d=i.get(r),q=r in e;if(d!=="undefined")return[b,S,v].includes(a)?(u[r]=y,a==S?d*1:a==v?d==P:d):(q&&d===null&&(f=P,i.set(r,a)),y===void 0?a:y)},R=[...L(c),...L(e)].reduce((r,a)=>({...r,[a]:O(a)}),{});M(R);const _={...R,...u};w(f,i,_)},[t]),o.useEffect(()=>{function c({detail:i}){const u=i&&i.param;(!u||u in e||u===P)&&n(f=>f+1)}return addEventListener(p,c),()=>removeEventListener(p,c)},[]),E};s.pmask=k,s.removeParam=U,s.setParam=j,s.useParams=A,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
(function(o,s){typeof exports=="object"&&typeof module<"u"?s(exports,require("react")):typeof define=="function"&&define.amd?define(["exports","react"],s):(o=typeof globalThis<"u"?globalThis:o||self,s(o.useparams={},o.React))})(this,function(o,s){"use strict";const a="USEPARAMS",v="popstate",L="?s",h="?n",S="?b",P="true",j={str:L,num:h,bool:S},k=()=>null,R=()=>typeof window<"u"?window.history:{},l=()=>new URLSearchParams(location.search),T=e=>Object.keys(e),E=()=>{const e=R().state;return e&&e[a]?e[a]:window[a]?window[a]:[...l()].reduce((n,[t,y])=>({...n,[t]:y}),{})},A=e=>new URL(`?${e.toString()}`,location).toString(),g=(e,n,t)=>{e&&(dispatchEvent(new CustomEvent(v,{detail:{param:e}})),window[a]=t,R().pushState({[a]:t},"",A(n)))},M=e=>{const n=E(),t=l();t.delete(e),delete n[e],g(e,t,n)},O=(e,n)=>{const t=l();t.set(e,n+""),g(e,t,{...E(),[e]:n})},_=e=>{const[n,t]=s.useState(0),[y,q]=s.useState({});return s.useEffect(()=>{const w=E(),c=l(),f={};let r="";const p=i=>{const u=e[i],b=w[i],m=c.get(i),x=i in e,U=b===void 0?u:b;if(m!=="undefined")return[L,h,S].includes(u)?(f[i]=b,u==h?m*1:u==S?m==P:m):(x&&m===null&&(r=P,c.set(i,U)),U)},d=[...T(w),...T(e)].reduce((i,u)=>({...i,[u]:p(u)}),{});q(d);const C={...d,...f};g(r,c,C)},[n]),s.useEffect(()=>{const w="next"in window&&window.next,c={events:{on:k,off:k}},f=w.router||c;function r({detail:p}){const d=p&&p.param;(!d||d in e||d===P)&&t(C=>C+1)}return addEventListener(v,r),f.events.on("routeChangeComplete",r),()=>{removeEventListener(v,r),f.events.off("routeChangeComplete",r)}},[]),y};o.pmask=j,o.removeParam=M,o.setParam=O,o.useParams=_,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); |
@@ -24,8 +24,9 @@ { | ||
"types": "./dist/index.d.ts", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"scripts": { | ||
"build": "vite build && cp lib/*.d.ts README.md -t dist/", | ||
"build": "vite build && cp lib/*.d.ts -t dist/", | ||
"pack:untar": "tar -xf useparams*.tgz", | ||
"build:pack": "npm run build && npm pack && npm run pack:untar && npm ln package", | ||
"start:pack": "npm run build:pack && vite --mode PACK playground", | ||
"roll": "npm run build && npm publish", | ||
"start": "vite playground" | ||
@@ -32,0 +33,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5293
33
5