diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d41b10d..b64c38a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [4.1.3](https://github.com/asmyshlyaev177/state-in-url/compare/v4.1.2...v4.1.3) (2024-11-21) + + +### Bug Fixes + +* use esbuild instead of terser ([59226b0](https://github.com/asmyshlyaev177/state-in-url/commit/59226b02d654067e858eae6333bf95dc79595ae2)) + ## [4.1.2](https://github.com/asmyshlyaev177/state-in-url/compare/v4.1.1...v4.1.2) (2024-11-20) diff --git a/dist/constants/constants.mjs b/dist/constants/constants.mjs index 48843fe1..512d0d19 100644 --- a/dist/constants/constants.mjs +++ b/dist/constants/constants.mjs @@ -1 +1 @@ -import{isSafari as e}from"../utils.mjs";const d={date:"⏲",undefined:"∙undefined"},n=e?330:70;export{d as SYMBOLS,n as TIMEOUT}; +import{isSafari as e}from"../utils.mjs";const d={date:"\u23F2",undefined:"\u2219undefined"},i=e?330:70;export{d as SYMBOLS,i as TIMEOUT}; diff --git a/dist/constants/index.mjs b/dist/constants/index.mjs index 82b2d5e8..7e70901f 100644 --- a/dist/constants/index.mjs +++ b/dist/constants/index.mjs @@ -1 +1 @@ -export{SYMBOLS,TIMEOUT}from"./constants.mjs"; +import{SYMBOLS as M,TIMEOUT as O}from"./constants.mjs";export{M as SYMBOLS,O as TIMEOUT}; diff --git a/dist/encodeState/encodeState.mjs b/dist/encodeState/encodeState.mjs index 7955a81d..bf94793c 100644 --- a/dist/encodeState/encodeState.mjs +++ b/dist/encodeState/encodeState.mjs @@ -1 +1 @@ -import{encode as t,decode as r}from"../encoder/encoder.mjs";import{getParams as n}from"../utils.mjs";function e(r,e,o){const i=n(o);return Object.entries(r||{}).forEach((([r,n])=>{const o=e?.[r];JSON.stringify(n)!==JSON.stringify(o)&&i.set(r,t(n))})),i.toString()}function o(t,e){return{...e||{},...Object.fromEntries([...n(t).entries()].map((([t,n])=>{const o=e?.[t];return[t,r(n,o)??o]})))}}export{o as decodeState,e as encodeState}; +import{encode as f,decode as a}from"../encoder/encoder.mjs";import{getParams as i}from"../utils.mjs";function m(o,e,n){const r=i(n);return Object.entries(o||{}).forEach(([t,c])=>{const s=e?.[t];JSON.stringify(c)!==JSON.stringify(s)&&r.set(t,f(c))}),r.toString()}function d(o,e){return{...e||{},...Object.fromEntries([...i(o).entries()].map(([n,r])=>{const t=e?.[n];return[n,a(r,t)??t]}))}}export{d as decodeState,m as encodeState}; diff --git a/dist/encodeState/encodeState.test.d.ts b/dist/encodeState/encodeState.test.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/encodeState/encodeState.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/encodeState/index.mjs b/dist/encodeState/index.mjs index bb965cc7..44914f04 100644 --- a/dist/encodeState/index.mjs +++ b/dist/encodeState/index.mjs @@ -1 +1 @@ -export{decodeState,encodeState}from"./encodeState.mjs"; +import{decodeState as o,encodeState as d}from"./encodeState.mjs";export{o as decodeState,d as encodeState}; diff --git a/dist/encoder/encoder.mjs b/dist/encoder/encoder.mjs index df217135..1a465ea9 100644 --- a/dist/encoder/encoder.mjs +++ b/dist/encoder/encoder.mjs @@ -1 +1 @@ -import{SYMBOLS as t}from"../constants/constants.mjs";import{typeOf as e}from"../utils.mjs";function n(t){return f(t)?t:JSON.stringify(t,r).replace(/'/g,"%27").replace(/"/g,"'")}function r(t,n){const a=e(n);if("object"!==a&&"array"!==a)return c(n);if("object"===a){const t=n;return Object.keys(t).forEach((e=>{t[e]=r("",t[e])})),t}return"array"===a?n.map((t=>r("",t))):n}const c=n=>{switch(e(n)){case"function":case"symbol":return"";case"date":return t.date+new Date(n).toISOString();case"undefined":return t.undefined;default:return n}};function a(t,e){return s(t.replace(/'/g,'"').replace(/%27/g,"'"),e)}function s(t,e){try{return JSON.parse(t,u)}catch{return e}}function u(t,e){return"string"==typeof e?i(e):e}const i=e=>{if(e!==t.undefined)return e?.startsWith?.(t.date)?new Date(e.slice(1)):e},o=new RegExp(`^(${t.undefined}|${t.date})`),f=t=>o.test(String(t));export{a as decode,i as decodePrimitive,n as encode,c as encodePrimitive,s as parseJSON,u as reviver}; +import{SYMBOLS as r}from"../constants/constants.mjs";import{typeOf as u}from"../utils.mjs";function p(e){return typeof e=="function"||typeof e=="symbol"?"":l(e)?e:JSON.stringify(structuredClone(e),i).replace(/'/g,"%27").replace(/"/g,"'")}function i(e,t){const c=u(t);if(c!=="object"&&c!=="array")return a(t);if(c==="object"){const n=t;return Object.keys(n).forEach(o=>{n[o]=i(e,n[o])}),n}return c==="array"?t.map(n=>i(e,n)):t}const a=e=>{switch(u(e)){case"date":return r.date+new Date(e).toISOString();case"undefined":return r.undefined;default:return e}};function y(e,t){return f(e.replace(/'/g,'"').replace(/%27/g,"'"),t)}function f(e,t){try{return JSON.parse(e,d)}catch{return t}}function d(e,t){return typeof t=="string"?s(t):t}const s=e=>{if(e!==r.undefined)return e?.startsWith?.(r.date)?new Date(e.slice(1)):e},g=new RegExp(`^(${r.undefined}|${r.date})`),l=e=>g.test(String(e));export{y as decode,s as decodePrimitive,p as encode,a as encodePrimitive,f as parseJSON,d as reviver}; diff --git a/dist/encoder/encoder.test.d.ts b/dist/encoder/encoder.test.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/encoder/encoder.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/encoder/index.mjs b/dist/encoder/index.mjs index 33037482..199af550 100644 --- a/dist/encoder/index.mjs +++ b/dist/encoder/index.mjs @@ -1 +1 @@ -export{decode,encode}from"./encoder.mjs"; +import{decode as d,encode as c}from"./encoder.mjs";export{d as decode,c as encode}; diff --git a/dist/index.mjs b/dist/index.mjs index f701fcb7..4d3549ce 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1 +1 @@ -export{decode,encode}from"./encoder/encoder.mjs";export{decodeState,encodeState}from"./encodeState/encodeState.mjs";export{useUrlState}from"./next/useUrlState/useUrlState.mjs";export{useSharedState}from"./useSharedState/useSharedState.mjs";export{useUrlEncode}from"./useUrlEncode/useUrlEncode.mjs";export{useUrlStateBase}from"./useUrlStateBase/useUrlStateBase.mjs";export{isSSR,typeOf}from"./utils.mjs"; +import{decode as r,encode as t}from"./encoder/encoder.mjs";import{decodeState as f,encodeState as p}from"./encodeState/encodeState.mjs";import{useUrlState as a}from"./next/useUrlState/useUrlState.mjs";import{useSharedState as x}from"./useSharedState/useSharedState.mjs";import{useUrlEncode as c}from"./useUrlEncode/useUrlEncode.mjs";import{useUrlStateBase as l}from"./useUrlStateBase/useUrlStateBase.mjs";import{isSSR as U,typeOf as h}from"./utils.mjs";export{r as decode,f as decodeState,t as encode,p as encodeState,U as isSSR,h as typeOf,x as useSharedState,c as useUrlEncode,a as useUrlState,l as useUrlStateBase}; diff --git a/dist/next/index.mjs b/dist/next/index.mjs index b8e7f6d9..750925ab 100644 --- a/dist/next/index.mjs +++ b/dist/next/index.mjs @@ -1 +1 @@ -export{useUrlState}from"./useUrlState/useUrlState.mjs"; +import{useUrlState as t}from"./useUrlState/useUrlState.mjs";export{t as useUrlState}; diff --git a/dist/next/useUrlState/index.mjs b/dist/next/useUrlState/index.mjs index 7c0bd8e8..631fb84f 100644 --- a/dist/next/useUrlState/index.mjs +++ b/dist/next/useUrlState/index.mjs @@ -1 +1 @@ -export{useUrlState}from"./useUrlState.mjs"; +import{useUrlState as t}from"./useUrlState.mjs";export{t as useUrlState}; diff --git a/dist/next/useUrlState/useUrlState.mjs b/dist/next/useUrlState/useUrlState.mjs index 2715408d..98e03428 100644 --- a/dist/next/useUrlState/useUrlState.mjs +++ b/dist/next/useUrlState/useUrlState.mjs @@ -1 +1 @@ -import{useRouter as e,useSearchParams as t}from"next/navigation";import a from"react";import{parseSPObj as r}from"../../parseSPObj.mjs";import{useUrlStateBase as s}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as l,isSSR as o,filterUnknownParams as c,filterUnknownParamsClient as u}from"../../utils.mjs";function i(i,p){const n="defaultState"in i?i.defaultState:i,f="defaultState"in i?i.searchParams:p?.searchParams,S="defaultState"in i?i.useHistory:p?.useHistory,d="defaultState"in i?{scroll:i.scroll,replace:i.replace}:{scroll:p?.scroll,replace:p?.replace},j=void 0===S||S?l:e(),{state:U,updateState:b,updateUrl:g,reset:P,getState:h}=s(n,j,(({parse:e})=>o?r(c(n,f),n):e(u(n)))),k=a.useMemo((()=>({...m,...d})),[]),v=a.useCallback(((e,t)=>g(e,{...k,...t})),[g]),x=t();a.useEffect((()=>{b(c(n,r(Object.fromEntries([...x.entries()]),n)))}),[x]);const y=a.useCallback((e=>{P({...k,...e})}),[P]);return{setState:b,updateState:b,setUrl:v,updateUrl:v,urlState:U,state:U,reset:y,getState:h}}const m={replace:!0,scroll:!1};export{i as useUrlState}; +import{useRouter as y,useSearchParams as C}from"next/navigation";import s from"react";import{parseSPObj as S}from"../../parseSPObj.mjs";import{useUrlStateBase as H}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as R,isSSR as g,filterUnknownParams as f,filterUnknownParamsClient as j}from"../../utils.mjs";function w(t,a){const e="defaultState"in t?t.defaultState:t,d="defaultState"in t?t.searchParams:a?.searchParams,o="defaultState"in t?t.useHistory:a?.useHistory,P="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:a?.scroll,replace:a?.replace},U=o===void 0||o?R:y(),{state:u,updateState:l,updateUrl:c,reset:i,getState:b}=H(e,U,({parse:r})=>g?S(f(e,d),e):r(j(e))),n=s.useMemo(()=>({...E,...P}),[]),m=s.useCallback((r,k)=>c(r,{...n,...k}),[c]),p=C();s.useEffect(()=>{l(f(e,S(Object.fromEntries([...p.entries()]),e)))},[p]);const h=s.useCallback(r=>{i({...n,...r})},[i]);return{setState:l,updateState:l,setUrl:m,updateUrl:m,urlState:u,state:u,reset:h,getState:b}}const E={replace:!0,scroll:!1};export{w as useUrlState}; diff --git a/dist/parseSPObj.mjs b/dist/parseSPObj.mjs index 6e7c1cc2..63c76b25 100644 --- a/dist/parseSPObj.mjs +++ b/dist/parseSPObj.mjs @@ -1 +1 @@ -import{decode as r}from"./encoder/encoder.mjs";function t(r,t){return{...t,...n(JSON.stringify(r),t)}}function n(r,t){try{return JSON.parse(r,e)}catch{return t}}const e=(t,n)=>{const e="string"==typeof n,o=e&&r(n?.replace?.(/'/g,'"'));return t&&e?o:n};export{t as parseSPObj}; +import{decode as c}from"./encoder/encoder.mjs";function o(r,t){return{...t,...s(JSON.stringify(r),t)}}function s(r,t){try{return JSON.parse(r,i)}catch{return t}}const i=(r,t)=>{const e=typeof t=="string",n=e&&c(t?.replace?.(/'/g,'"'));return r&&e?n:t};export{o as parseSPObj}; diff --git a/dist/parseSPObj.test.d.ts b/dist/parseSPObj.test.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/parseSPObj.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/react-router/index.mjs b/dist/react-router/index.mjs index b8e7f6d9..750925ab 100644 --- a/dist/react-router/index.mjs +++ b/dist/react-router/index.mjs @@ -1 +1 @@ -export{useUrlState}from"./useUrlState/useUrlState.mjs"; +import{useUrlState as t}from"./useUrlState/useUrlState.mjs";export{t as useUrlState}; diff --git a/dist/react-router/useUrlState/index.mjs b/dist/react-router/useUrlState/index.mjs index 7c0bd8e8..631fb84f 100644 --- a/dist/react-router/useUrlState/index.mjs +++ b/dist/react-router/useUrlState/index.mjs @@ -1 +1 @@ -export{useUrlState}from"./useUrlState.mjs"; +import{useUrlState as t}from"./useUrlState.mjs";export{t as useUrlState}; diff --git a/dist/react-router/useUrlState/useUrlState.mjs b/dist/react-router/useUrlState/useUrlState.mjs index e94f80a8..a9410e77 100644 --- a/dist/react-router/useUrlState/useUrlState.mjs +++ b/dist/react-router/useUrlState/useUrlState.mjs @@ -1 +1 @@ -import e from"react";import{useNavigate as t,useSearchParams as r}from"react-router-dom";import{parseSPObj as a}from"../../parseSPObj.mjs";import{useUrlStateBase as s}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as l,filterUnknownParamsClient as o,assignValue as p,filterUnknownParams as u}from"../../utils.mjs";function c(c,m){const n="defaultState"in c?c.defaultState:c,i="defaultState"in c?c.useHistory:m?.useHistory,f="defaultState"in c?{replace:c.replace,preventScrollReset:c.preventScrollReset}:{replace:m?.replace,preventScrollReset:m?.preventScrollReset},d=e.useMemo((()=>({...S,...f})),[]),j=t(),v=e.useMemo((()=>i?l:{replace:(e,t)=>j(e,{...d,...t}),push:(e,t)=>j(e,{...d,...t})}),[j]),{state:R,updateState:U,updateUrl:b,getState:g,reset:k}=s(n,v,(({parse:e})=>e(o(n)))),y=e.useCallback(((e,t)=>b(e,{...d,...t})),[b]),[B]=r();e.useEffect((()=>{U(p(n,u(n,a(Object.fromEntries([...B.entries()]),n))))}),[B]);const C=e.useCallback((e=>{k({...d,...e})}),[k]);return{setState:U,updateState:U,setUrl:y,updateUrl:y,urlState:R,state:R,reset:C,getState:g}}const S={replace:!0,preventScrollReset:!0};export{c as useUrlState}; +import s from"react";import{useNavigate as b,useSearchParams as g}from"react-router-dom";import{parseSPObj as k}from"../../parseSPObj.mjs";import{useUrlStateBase as C}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as P,filterUnknownParamsClient as y,assignValue as H,filterUnknownParams as h}from"../../utils.mjs";function j(t,o){const r="defaultState"in t?t.defaultState:t,f="defaultState"in t?t.useHistory:o?.useHistory,d="defaultState"in t?{replace:t.replace,preventScrollReset:t.preventScrollReset}:{replace:o?.replace,preventScrollReset:o?.preventScrollReset},l=s.useMemo(()=>({...w,...d}),[]),u=b(),U=s.useMemo(()=>f?P:{replace:(e,a)=>u(e,{...l,...a}),push:(e,a)=>u(e,{...l,...a})},[u]),{state:p,updateState:n,updateUrl:c,getState:v,reset:i}=C(r,U,({parse:e})=>e(y(r))),S=s.useCallback((e,a)=>c(e,{...l,...a}),[c]),[m]=g();s.useEffect(()=>{n(H(r,h(r,k(Object.fromEntries([...m.entries()]),r))))},[m]);const R=s.useCallback(e=>{i({...l,...e})},[i]);return{setState:n,updateState:n,setUrl:S,updateUrl:S,urlState:p,state:p,reset:R,getState:v}}const w={replace:!0,preventScrollReset:!0};export{j as useUrlState}; diff --git a/dist/subscribers.d.ts b/dist/subscribers.d.ts index ed5b8c23..86828e4b 100644 --- a/dist/subscribers.d.ts +++ b/dist/subscribers.d.ts @@ -5,7 +5,7 @@ export declare const subscribers: { remove(obj: JSONCompatible, cb: Cb): void; }; export declare const stateMap: { - get: (objKey: T) => T; + get: (objKey: T) => T | undefined; set: (objKey: T, value: T) => void; }; export type Cb = () => void; diff --git a/dist/subscribers.mjs b/dist/subscribers.mjs index 0f3091c5..c8b2416f 100644 --- a/dist/subscribers.mjs +++ b/dist/subscribers.mjs @@ -1 +1 @@ -const e=new WeakMap,t=new WeakMap,n={get:e=>t.get(e)||[],add(e,n){const s=this.get(e);return t.set(e,s.concat(n)),()=>this.remove(e,n)},remove(e,n){const s=this.get(e).filter((e=>e!==n));s.length?t.set(e,s):t.delete(e)}},s={get:function(t){return e.get(t)},set:function(t,n){e.set(t,n)}};export{s as stateMap,n as subscribers}; +const r=new WeakMap,s=new WeakMap,o={get(t){return s.get(t)||[]},add(t,e){const n=this.get(t);return s.set(t,n.concat(e)),()=>this.remove(t,e)},remove(t,e){const n=this.get(t).filter(c=>c!==e);n.length?s.set(t,n):s.delete(t)}},a={get:function(t){return r.get(t)},set:function(t,e){r.set(t,e)}};export{a as stateMap,o as subscribers}; diff --git a/dist/useSharedState/index.mjs b/dist/useSharedState/index.mjs index 3632f965..5b127007 100644 --- a/dist/useSharedState/index.mjs +++ b/dist/useSharedState/index.mjs @@ -1 +1 @@ -export{useSharedState}from"./useSharedState.mjs"; +import{useSharedState as t}from"./useSharedState.mjs";export{t as useSharedState}; diff --git a/dist/useSharedState/useSharedState.mjs b/dist/useSharedState/useSharedState.mjs index f7cbc3f5..a8bb66f1 100644 --- a/dist/useSharedState/useSharedState.mjs +++ b/dist/useSharedState/useSharedState.mjs @@ -1 +1 @@ -import t from"react";import{stateMap as r,subscribers as e}from"../subscribers.mjs";import{useInsertionEffect as n}from"../useInsertionEffect.mjs";import{isSSR as u,isEqual as c}from"../utils.mjs";function s(s,o){const f=t.useRef(s),[a,i]=t.useState((()=>{if(u)return o?o?.():f.current;const t=r.get(f.current);if(t)return t;{const t=o?.()||f.current;return r.set(f.current,t),t}})),m=t.useCallback((t=>{const n=r.get(f.current),u="function"==typeof t?t(n):{...n,...t};c(n,u)||(r.set(f.current,u),e.get(f.current).forEach((t=>t())))}),[]);n((()=>{const t=e.add(f.current,(()=>{i(r.get(f.current)||f.current)}));return()=>{t()}}),[]);return{state:a,getState:t.useCallback((()=>r.get(f.current)||f.current),[]),setState:m}}export{s as useSharedState}; +import u from"react";import{stateMap as n,subscribers as s}from"../subscribers.mjs";import{useInsertionEffect as g}from"../useInsertionEffect.mjs";import{isSSR as l,isEqual as b}from"../utils.mjs";function d(a,c){const t=u.useRef(a),[f,i]=u.useState(()=>{if(l)return c?c?.():t.current;const e=n.get(t.current);if(e)return e;{const r=c?.()||t.current;return n.set(t.current,r),r}}),m=u.useCallback(e=>{const r=n.get(t.current),o=typeof e=="function"?e(r):{...r,...e};b(r,o)||(n.set(t.current,o),s.get(t.current).forEach(p=>p()))},[]);g(()=>{const e=()=>{i(n.get(t.current)||t.current)},r=s.add(t.current,e);return()=>{r()}},[]);const S=u.useCallback(()=>n.get(t.current)||t.current,[]);return{state:f,getState:S,setState:m}}export{d as useSharedState}; diff --git a/dist/useSharedState/useSharedState.test.d.ts b/dist/useSharedState/useSharedState.test.d.ts new file mode 100644 index 00000000..74ad4470 --- /dev/null +++ b/dist/useSharedState/useSharedState.test.d.ts @@ -0,0 +1,14 @@ +export declare const form: Form; +type Form = { + name: string; + age?: number; + agree_to_terms: boolean; + tags: { + id: string; + value: { + text: string; + time: Date; + }; + }[]; +}; +export {}; diff --git a/dist/useUrlEncode/index.mjs b/dist/useUrlEncode/index.mjs index 7d8b5982..2cd8ac73 100644 --- a/dist/useUrlEncode/index.mjs +++ b/dist/useUrlEncode/index.mjs @@ -1 +1 @@ -export{useUrlEncode}from"./useUrlEncode.mjs"; +import{useUrlEncode as r}from"./useUrlEncode.mjs";export{r as useUrlEncode}; diff --git a/dist/useUrlEncode/useUrlEncode.mjs b/dist/useUrlEncode/useUrlEncode.mjs index 64fa9b7b..0504e084 100644 --- a/dist/useUrlEncode/useUrlEncode.mjs +++ b/dist/useUrlEncode/useUrlEncode.mjs @@ -1 +1 @@ -import t from"react";import{encodeState as e,decodeState as o}from"../encodeState/encodeState.mjs";import{typeOf as r}from"../utils.mjs";function a(a){const c=t.useCallback(((t,o)=>"object"===r(t)?e(t,a,o):""),[a]);return{parse:t.useCallback((t=>o(t,a)),[a]),stringify:c}}export{a as useUrlEncode}; +import o from"react";import{encodeState as a,decodeState as n}from"../encodeState/encodeState.mjs";import{typeOf as f}from"../utils.mjs";function i(e){const c=o.useCallback((t,r)=>f(t)==="object"?a(t,e,r):"",[e]);return{parse:o.useCallback(t=>n(t,e),[e]),stringify:c}}export{i as useUrlEncode}; diff --git a/dist/useUrlEncode/useUrlEncode.test.d.ts b/dist/useUrlEncode/useUrlEncode.test.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/useUrlEncode/useUrlEncode.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/useUrlStateBase/index.mjs b/dist/useUrlStateBase/index.mjs index e767f630..e94b96ba 100644 --- a/dist/useUrlStateBase/index.mjs +++ b/dist/useUrlStateBase/index.mjs @@ -1 +1 @@ -export{useUrlStateBase}from"./useUrlStateBase.mjs"; +import{useUrlStateBase as t}from"./useUrlStateBase.mjs";export{t as useUrlStateBase}; diff --git a/dist/useUrlStateBase/useUrlStateBase.mjs b/dist/useUrlStateBase/useUrlStateBase.mjs index 2b2ee093..3f86220d 100644 --- a/dist/useUrlStateBase/useUrlStateBase.mjs +++ b/dist/useUrlStateBase/useUrlStateBase.mjs @@ -1 +1 @@ -import e from"react";import{TIMEOUT as t}from"../constants/constants.mjs";import{useInsertionEffect as o}from"../useInsertionEffect.mjs";import{useSharedState as r}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as n}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as s}from"../utils.mjs";function a(a,i,u){const{parse:m,stringify:l}=n(a),{state:p,getState:d,setState:f}=r(a,(()=>u?.({parse:m})||a)),h=e.useRef(void 0);o((()=>{const e=()=>{const e=m(s(a));f(e)};return window.addEventListener(c,e),()=>{window.removeEventListener(c,e),clearTimeout(h.current)}}),[f]);const w=e.useRef([]),S=e.useCallback(((e,o)=>{const r="function"==typeof e?e(d()):e?{...d(),...e}:d(),n=l(r,function(e){const t=Object.keys(e),o=window.location.search,r=new URLSearchParams(o),n=new URLSearchParams;return r.forEach(((e,o)=>!t.includes(o)&&n.set(o,e))),n}(a)),s=`${window.location.pathname}${n.length?"?":""}${n}${window.location.hash}`;if(s===`${window.location.pathname}${window.location.search}${window.location.hash}`)return;let c;f(r);const u=o?.replace;delete o?.replace,w.current.push([u?"replace":"push",s,o]),clearTimeout(h.current),h.current=setTimeout((()=>{queueMicrotask((()=>{for(;w.current.length;){const e=w.current.shift();e&&e?.[1]!==c?.[1]&&(c=e)}const[e,t,o]=c||{};c=void 0,e&&i[e](t,o)}))}),t)}),[i,l,d]),$=e.useCallback((e=>{S(a,e)}),[S]);return{updateState:f,updateUrl:S,state:p,reset:$,getState:d}}const c="popstate";export{a as useUrlStateBase}; +import u from"react";import{TIMEOUT as b}from"../constants/constants.mjs";import{useInsertionEffect as L}from"../useInsertionEffect.mjs";import{useSharedState as P}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as y}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as C}from"../utils.mjs";function O(t,a,l){const{parse:s,stringify:o}=y(t),{state:m,getState:r,setState:c}=P(t,()=>l?.({parse:s})||t),p=u.useRef(void 0);L(()=>{const e=()=>{const n=s(C(t));c(n)};return window.addEventListener(E,e),()=>{window.removeEventListener(E,e),clearTimeout(p.current)}},[c]);const f=u.useRef([]),w=u.useCallback((e,n)=>{const h=typeof e=="function"?e(r()):e?{...r(),...e}:r(),S=o(h,I(t)),U=`${window.location.pathname}${S.length?"?":""}${S}${window.location.hash}`,k=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(U===k)return;let i;c(h);const T=n?.replace;delete n?.replace,f.current.push([T?"replace":"push",U,n]),clearTimeout(p.current),p.current=setTimeout(()=>{queueMicrotask(()=>{for(;f.current.length;){const d=f.current.shift();d&&d?.[1]!==i?.[1]&&(i=d)}const[$,v,R]=i||{};i=void 0,$&&a[$](v,R)})},b)},[a,o,r]),g=u.useCallback(e=>{w(t,e)},[w]);return{updateState:c,updateUrl:w,state:m,reset:g,getState:r}}function I(t){const a=Object.keys(t),l=window.location.search,s=new URLSearchParams(l),o=new URLSearchParams;return s.forEach((m,r)=>!a.includes(r)&&o.set(r,m)),o}const E="popstate";export{O as useUrlStateBase}; diff --git a/dist/useUrlStateBase/useUrlStateBase.test.d.ts b/dist/useUrlStateBase/useUrlStateBase.test.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/useUrlStateBase/useUrlStateBase.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/utils.mjs b/dist/utils.mjs index 9e3a3e2c..df4d1d8a 100644 --- a/dist/utils.mjs +++ b/dist/utils.mjs @@ -1 +1 @@ -const t=t=>{const e=typeof t,n=null===t,r=Array.isArray(t),o=t instanceof Date;return(n?"null":o&&"date")||r&&"array"||!n&&!o&&!r&&"object"===e&&"object"||e},e="undefined"==typeof window,n=t=>new URLSearchParams("string"==typeof t?r(t):t?.toString?.()||""),r=t=>t.split("?")?.[1]||t||"",o=(t,e)=>JSON.stringify(t)===JSON.stringify(e);function i(t){const e=new URLSearchParams;return c(t,[...new URLSearchParams(window.location.search).entries()]).forEach((([t,n])=>e.set(t,n))),e.toString()}function s(t,e){return Object.fromEntries(c(t,Object.entries(e||{})))}function c(t,e){const n=Object.keys(t),r=[];for(let t=0;t{const o=r,i=void 0!==e[o];n[o]=i?e[o]:t[o]})),n}const u={push:t=>{window&&window.history.pushState(null,"",t)},replace:t=>{window&&window.history.replaceState(null,"",t)}},l=/apple/i.test(!e&&navigator?.userAgent||"");export{a as assignValue,c as filterUnknown,s as filterUnknownParams,i as filterUnknownParamsClient,n as getParams,o as isEqual,e as isSSR,l as isSafari,u as routerHistory,t as typeOf}; +const c=n=>{const t=typeof n,r=n===null,o=Array.isArray(n),e=n instanceof Date;return r&&"null"||e&&"date"||o&&"array"||!r&&!e&&!o&&t==="object"&&"object"||t},i=typeof window>"u",l=n=>new URLSearchParams(typeof n=="string"?u(n):n?.toString?.()||""),u=n=>n.split("?")?.[1]||n||"",f=(n,t)=>JSON.stringify(n)===JSON.stringify(t);function w(n){const t=new URLSearchParams;return s(n,[...new URLSearchParams(window.location.search).entries()]).forEach(([r,o])=>t.set(r,o)),t.toString()}function p(n,t){return Object.fromEntries(s(n,Object.entries(t||{})))}function s(n,t){const r=Object.keys(n),o=[];for(let e=0;e{const e=o,a=t[e]!==void 0;r[e]=a?t[e]:n[e]}),r}const g={push:n=>{window&&window.history.pushState(null,"",n)},replace:n=>{window&&window.history.replaceState(null,"",n)}},h=/apple/i.test(!i&&navigator?.userAgent||"");export{y as assignValue,s as filterUnknown,p as filterUnknownParams,w as filterUnknownParamsClient,l as getParams,f as isEqual,i as isSSR,h as isSafari,g as routerHistory,c as typeOf}; diff --git a/dist/utils.test.d.ts b/dist/utils.test.d.ts new file mode 100644 index 00000000..cb0ff5c3 --- /dev/null +++ b/dist/utils.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/package.json b/package.json index 0e4ed33c..0c8586d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "state-in-url", - "version": "4.1.2", + "version": "4.1.3", "description": "Easily share complex state objects between unrelated React components, preserve types and structure, with TS validation. Deep links and url state synchronization wthout any hasssle or boilerplate.", "homepage": "https://state-in-url.dev", "repository": {