From 38d09e28cf17bdf0635642b179e0d974f43c86af Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Mon, 30 Sep 2024 15:21:37 +0000 Subject: [PATCH] chore(release): 3.0.0 [skip ci] # [3.0.0](https://github.com/asmyshlyaev177/state-in-url/compare/v2.6.0...v3.0.0) (2024-09-30) ### Code Refactoring * **useurlstate:** remove deprecated things ([87c8c7c](https://github.com/asmyshlyaev177/state-in-url/commit/87c8c7c995c5cd7d9e7aa039c30bfe64b24abe4b)) ### Features * **useurlstate:** useUrlState hook for react-router@6 ([1c7cd02](https://github.com/asmyshlyaev177/state-in-url/commit/1c7cd0227a261dd7594f63cada63a73808df90d2)) ### BREAKING CHANGES * **useurlstate:** 1. `useUrlState` for Next.js now accept only object, eg. `useUrlState({ defaultState: {}})` 2. urls encoded with versions prior to v2.3.0 could stop working --- CHANGELOG.md | 19 +++++++++++++++++++ dist/constants/constants.mjs | 1 + dist/constants/index.mjs | 1 + dist/encodeState/encodeState.mjs | 1 + dist/encodeState/index.mjs | 1 + dist/encoder/encoder.mjs | 1 + dist/encoder/index.mjs | 1 + dist/index.mjs | 1 + dist/next/index.mjs | 1 + dist/next/useUrlState/index.mjs | 1 + dist/next/useUrlState/useUrlState.mjs | 1 + dist/parseSPObj.mjs | 1 + dist/react-router/index.mjs | 1 + dist/react-router/useUrlState/index.mjs | 1 + dist/react-router/useUrlState/useUrlState.mjs | 1 + dist/subscribers.mjs | 1 + dist/useInsertionEffect.mjs | 1 + dist/useSharedState/index.mjs | 1 + dist/useSharedState/useSharedState.mjs | 1 + dist/useUrlEncode/index.mjs | 1 + dist/useUrlEncode/useUrlEncode.mjs | 1 + dist/useUrlStateBase/index.mjs | 1 + dist/useUrlStateBase/useUrlStateBase.mjs | 1 + dist/utils.mjs | 1 + package.json | 2 +- 25 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 dist/constants/constants.mjs create mode 100644 dist/constants/index.mjs create mode 100644 dist/encodeState/encodeState.mjs create mode 100644 dist/encodeState/index.mjs create mode 100644 dist/encoder/encoder.mjs create mode 100644 dist/encoder/index.mjs create mode 100644 dist/index.mjs create mode 100644 dist/next/index.mjs create mode 100644 dist/next/useUrlState/index.mjs create mode 100644 dist/next/useUrlState/useUrlState.mjs create mode 100644 dist/parseSPObj.mjs create mode 100644 dist/react-router/index.mjs create mode 100644 dist/react-router/useUrlState/index.mjs create mode 100644 dist/react-router/useUrlState/useUrlState.mjs create mode 100644 dist/subscribers.mjs create mode 100644 dist/useInsertionEffect.mjs create mode 100644 dist/useSharedState/index.mjs create mode 100644 dist/useSharedState/useSharedState.mjs create mode 100644 dist/useUrlEncode/index.mjs create mode 100644 dist/useUrlEncode/useUrlEncode.mjs create mode 100644 dist/useUrlStateBase/index.mjs create mode 100644 dist/useUrlStateBase/useUrlStateBase.mjs create mode 100644 dist/utils.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index c8aa8e8..f4e9fb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +# [3.0.0](https://github.com/asmyshlyaev177/state-in-url/compare/v2.6.0...v3.0.0) (2024-09-30) + + +### Code Refactoring + +* **useurlstate:** remove deprecated things ([87c8c7c](https://github.com/asmyshlyaev177/state-in-url/commit/87c8c7c995c5cd7d9e7aa039c30bfe64b24abe4b)) + + +### Features + +* **useurlstate:** useUrlState hook for react-router@6 ([1c7cd02](https://github.com/asmyshlyaev177/state-in-url/commit/1c7cd0227a261dd7594f63cada63a73808df90d2)) + + +### BREAKING CHANGES + +* **useurlstate:** 1. `useUrlState` for Next.js now accept only object, eg. `useUrlState({ +defaultState: {}})` + 2. urls encoded with versions prior to v2.3.0 could stop working + # [2.6.0](https://github.com/asmyshlyaev177/state-in-url/compare/v2.5.2...v2.6.0) (2024-09-19) diff --git a/dist/constants/constants.mjs b/dist/constants/constants.mjs new file mode 100644 index 0000000..e67ca5f --- /dev/null +++ b/dist/constants/constants.mjs @@ -0,0 +1 @@ +const e={date:"⏲",undefined:"∙undefined"};export{e as SYMBOLS}; diff --git a/dist/constants/index.mjs b/dist/constants/index.mjs new file mode 100644 index 0000000..cf674f7 --- /dev/null +++ b/dist/constants/index.mjs @@ -0,0 +1 @@ +export{SYMBOLS}from"./constants.mjs"; diff --git a/dist/encodeState/encodeState.mjs b/dist/encodeState/encodeState.mjs new file mode 100644 index 0000000..b67cc33 --- /dev/null +++ b/dist/encodeState/encodeState.mjs @@ -0,0 +1 @@ +import{encode as t,decode as n}from"../encoder/encoder.mjs";import{getParams as o}from"../utils.mjs";function r(t){let n,o=t[0],r=1;for(;ro.call(n,...t))),n=void 0)}return o}function e(n,e,c){const s=o(c);return Object.entries(n||{}).forEach((([n,o])=>{const c=r([e,"optionalAccess",t=>t[n]]);JSON.stringify(o)!==JSON.stringify(c)&&s.set(n,t(o))})),s.toString()}function c(t,e){return{...e||{},...Object.fromEntries([...o(t).entries()].map((([t,o])=>{const c=r([e,"optionalAccess",n=>n[t]]);return[t,(s=n(o,c),i=()=>c,null!=s?s:i())];var s,i})))}}export{c as decodeState,e as encodeState}; diff --git a/dist/encodeState/index.mjs b/dist/encodeState/index.mjs new file mode 100644 index 0000000..bb965cc --- /dev/null +++ b/dist/encodeState/index.mjs @@ -0,0 +1 @@ +export{decodeState,encodeState}from"./encodeState.mjs"; diff --git a/dist/encoder/encoder.mjs b/dist/encoder/encoder.mjs new file mode 100644 index 0000000..8ff1be1 --- /dev/null +++ b/dist/encoder/encoder.mjs @@ -0,0 +1 @@ +import{SYMBOLS as t}from"../constants/constants.mjs";import{typeOf as n}from"../utils.mjs";function e(e){if(l(e))return e;switch(n(e)){case"function":case"symbol":return"";case"date":return r=e,t.date+new Date(r).toISOString();case"undefined":return t.undefined;default:return JSON.stringify(e).replaceAll('"',"'")}var r}function r(t,n){return a(t.replaceAll("'",'"'),n)}const o=n=>{if(n!==t.undefined)return function(t){let n,e=t[0],r=1;for(;re.call(n,...t))),n=void 0)}return e}([n,"optionalAccess",t=>t.startsWith,"optionalCall",n=>n(t.date)])?new Date(n.slice(1)):s},s=Symbol("isError"),c=(t,n)=>{const e="string"==typeof n,r=e&&o(n);return r===s?n:e?r:n};function a(t,n){try{return JSON.parse(t,c)}catch(e){const r=o(t);return r!==s?r:n}}const i=new RegExp(`^(${t.undefined}|${t.date})`),l=t=>i.test(String(t));export{r as decode,o as decodePrimitive,e as encode,s as errorSym,a as parseJSON,c as reviver}; diff --git a/dist/encoder/index.mjs b/dist/encoder/index.mjs new file mode 100644 index 0000000..3303748 --- /dev/null +++ b/dist/encoder/index.mjs @@ -0,0 +1 @@ +export{decode,encode}from"./encoder.mjs"; diff --git a/dist/index.mjs b/dist/index.mjs new file mode 100644 index 0000000..f701fcb --- /dev/null +++ b/dist/index.mjs @@ -0,0 +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"; diff --git a/dist/next/index.mjs b/dist/next/index.mjs new file mode 100644 index 0000000..b8e7f6d --- /dev/null +++ b/dist/next/index.mjs @@ -0,0 +1 @@ +export{useUrlState}from"./useUrlState/useUrlState.mjs"; diff --git a/dist/next/useUrlState/index.mjs b/dist/next/useUrlState/index.mjs new file mode 100644 index 0000000..7c0bd8e --- /dev/null +++ b/dist/next/useUrlState/index.mjs @@ -0,0 +1 @@ +export{useUrlState}from"./useUrlState.mjs"; diff --git a/dist/next/useUrlState/useUrlState.mjs b/dist/next/useUrlState/useUrlState.mjs new file mode 100644 index 0000000..e5f7c92 --- /dev/null +++ b/dist/next/useUrlState/useUrlState.mjs @@ -0,0 +1 @@ +import{useRouter as t,useSearchParams as e}from"next/navigation";import a from"react";import{parseSPObj as r}from"../../parseSPObj.mjs";import{useUrlStateBase as s}from"../../useUrlStateBase/useUrlStateBase.mjs";import{filterUnknownParams as o,isSSR as m,filterUnknownParamsClient as p}from"../../utils.mjs";function u({defaultState:u,searchParams:i,...l}){const n=t(),{state:f,updateState:S,updateUrl:d,getState:j}=s(u,n,(({parse:t})=>m()?r(o(u,i),u):t(p(u)))),U=a.useCallback(((t,e)=>{const a={...c,...l,...e};d(t,a)}),[d,l]),b=e();return a.useEffect((()=>{S(o(u,r(Object.fromEntries([...b.entries()]),u)))}),[b]),{updateState:S,updateUrl:U,state:f,getState:j}}const c={replace:!0,scroll:!1};export{u as useUrlState}; diff --git a/dist/parseSPObj.mjs b/dist/parseSPObj.mjs new file mode 100644 index 0000000..b5b5f13 --- /dev/null +++ b/dist/parseSPObj.mjs @@ -0,0 +1 @@ +import{decodePrimitive as n,errorSym as t,decode as o}from"./encoder/encoder.mjs";function l(n,t){return{...t,...r(JSON.stringify(n),t)}}function r(o,l){try{return JSON.parse(o,c)}catch(r){const c=n(o);return c!==t?c:l}}const c=(n,t)=>{const l="string"==typeof t,r=l&&o(function(n){let t,o=n[0],l=1;for(;lo.call(t,...n))),t=void 0)}return o}([t,"optionalAccess",n=>n.replaceAll,"optionalCall",n=>n("'",'"')]));return n&&l?r:t};export{l as parseSPObj}; diff --git a/dist/react-router/index.mjs b/dist/react-router/index.mjs new file mode 100644 index 0000000..b8e7f6d --- /dev/null +++ b/dist/react-router/index.mjs @@ -0,0 +1 @@ +export{useUrlState}from"./useUrlState/useUrlState.mjs"; diff --git a/dist/react-router/useUrlState/index.mjs b/dist/react-router/useUrlState/index.mjs new file mode 100644 index 0000000..7c0bd8e --- /dev/null +++ b/dist/react-router/useUrlState/index.mjs @@ -0,0 +1 @@ +export{useUrlState}from"./useUrlState.mjs"; diff --git a/dist/react-router/useUrlState/useUrlState.mjs b/dist/react-router/useUrlState/useUrlState.mjs new file mode 100644 index 0000000..4933f48 --- /dev/null +++ b/dist/react-router/useUrlState/useUrlState.mjs @@ -0,0 +1 @@ +import t from"react";import{useNavigate as e,useSearchParams as r}from"react-router-dom";import{parseSPObj as a}from"../../parseSPObj.mjs";import{useUrlStateBase as s}from"../../useUrlStateBase/useUrlStateBase.mjs";import{assignValue as o,filterUnknownParams as m,filterUnknownParamsClient as p}from"../../utils.mjs";function u({defaultState:u,...l}){const f=e(),i=t.useMemo((()=>({replace:(t,e)=>f(t,{...c,...l,...e}),push:(t,e)=>f(t,{...c,...l,...e})})),[f,l]),{state:n,updateState:S,updateUrl:d,getState:j}=s(u,i,(({parse:t})=>t(p(u)))),U=t.useCallback(((t,e)=>{const r={...c,...l,...e};d(t,r)}),[l]),[b]=r();return t.useEffect((()=>{S(o(u,n,m(u,a(Object.fromEntries([...b.entries()]),u))))}),[b]),{updateState:S,updateUrl:U,state:n,getState:j}}const c={replace:!0,preventScrollReset:!0};export{u as useUrlState}; diff --git a/dist/subscribers.mjs b/dist/subscribers.mjs new file mode 100644 index 0000000..0f3091c --- /dev/null +++ b/dist/subscribers.mjs @@ -0,0 +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}; diff --git a/dist/useInsertionEffect.mjs b/dist/useInsertionEffect.mjs new file mode 100644 index 0000000..39000e5 --- /dev/null +++ b/dist/useInsertionEffect.mjs @@ -0,0 +1 @@ +import e from"react";const t=e.useInsertionEffect||e.useEffect;export{t as useInsertionEffect}; diff --git a/dist/useSharedState/index.mjs b/dist/useSharedState/index.mjs new file mode 100644 index 0000000..3632f96 --- /dev/null +++ b/dist/useSharedState/index.mjs @@ -0,0 +1 @@ +export{useSharedState}from"./useSharedState.mjs"; diff --git a/dist/useSharedState/useSharedState.mjs b/dist/useSharedState/useSharedState.mjs new file mode 100644 index 0000000..352c43a --- /dev/null +++ b/dist/useSharedState/useSharedState.mjs @@ -0,0 +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 c,isEqual as o}from"../utils.mjs";function u(t){let r,e=t[0],n=1;for(;ne.call(r,...t))),r=void 0)}return e}function s(s,l){const a=t.useRef(s),[i,f]=t.useState((()=>{if(c())return l?u([l,"optionalCall",t=>t()]):a.current;const t=r.get(a.current);if(t)return t;{const t=u([l,"optionalCall",t=>t()])||a.current;return r.set(a.current,t),t}})),p=t.useCallback((t=>{const n=r.get(a.current),c="function"==typeof t?t(n):{...n,...t};o(n,c)||(r.set(a.current,c),e.get(a.current).forEach((t=>{t()})))}),[]);n((()=>{const t=e.add(a.current,(()=>{f(r.get(a.current)||a.current)}));return()=>{t()}}),[]);return{state:i,getState:t.useCallback((()=>r.get(a.current)||a.current),[]),setState:p}}export{s as useSharedState}; diff --git a/dist/useUrlEncode/index.mjs b/dist/useUrlEncode/index.mjs new file mode 100644 index 0000000..7d8b598 --- /dev/null +++ b/dist/useUrlEncode/index.mjs @@ -0,0 +1 @@ +export{useUrlEncode}from"./useUrlEncode.mjs"; diff --git a/dist/useUrlEncode/useUrlEncode.mjs b/dist/useUrlEncode/useUrlEncode.mjs new file mode 100644 index 0000000..908d7a7 --- /dev/null +++ b/dist/useUrlEncode/useUrlEncode.mjs @@ -0,0 +1 @@ +import t from"react";import{encodeState as r,decodeState as e}from"../encodeState/encodeState.mjs";import{typeOf as o}from"../utils.mjs";function n(n){const c=t.useCallback((function(t,e){return"object"===o(t)?r(t,n,e):""}),[n]);return{parse:t.useCallback((function(t){return e(t,n)}),[n]),stringify:c}}export{n as useUrlEncode}; diff --git a/dist/useUrlStateBase/index.mjs b/dist/useUrlStateBase/index.mjs new file mode 100644 index 0000000..e767f63 --- /dev/null +++ b/dist/useUrlStateBase/index.mjs @@ -0,0 +1 @@ +export{useUrlStateBase}from"./useUrlStateBase.mjs"; diff --git a/dist/useUrlStateBase/useUrlStateBase.mjs b/dist/useUrlStateBase/useUrlStateBase.mjs new file mode 100644 index 0000000..e5159a5 --- /dev/null +++ b/dist/useUrlStateBase/useUrlStateBase.mjs @@ -0,0 +1 @@ +import t from"react";import{useInsertionEffect as e}from"../useInsertionEffect.mjs";import{useSharedState as o}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as n}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as a}from"../utils.mjs";function s(s,c,i){const{parse:l,stringify:p}=n(s),{state:u,getState:m,setState:d}=o(s,(()=>function(t){let e,o=t[0],n=1;for(;no.call(e,...t))),e=void 0)}return o}([i,"optionalCall",t=>t({parse:l})])||s));e((()=>{const t=()=>{const t=l(a(s));d(t)};return window.addEventListener(r,t),()=>{window.removeEventListener(r,t)}}),[d]);const w=t.useCallback(((t,e)=>{const o=`${window.location.pathname}${window.location.search}${window.location.hash}`,n="function"==typeof t,a=function(t){const e=Object.keys(t),o=window.location.search,n=new URLSearchParams(o),a=new URLSearchParams;return n.forEach(((t,o)=>!e.includes(o)&&a.set(o,t))),a}(s),r=n?t(m()):t?{...m(),...t}:m(),i=p(r,a);d(r);const l=`${window.location.pathname}${i.length?"?":""}${i}${window.location.hash}`;if(o!==l){const{replace:t,...o}=e||{};c[t?"replace":"push"](l,{...o})}}),[c,p,m]);return{updateState:d,updateUrl:w,state:u,getState:m}}const r="popstate";export{s as useUrlStateBase}; diff --git a/dist/utils.mjs b/dist/utils.mjs new file mode 100644 index 0000000..30575fd --- /dev/null +++ b/dist/utils.mjs @@ -0,0 +1 @@ +function t(t){let n,e=t[0],o=1;for(;oe.call(n,...t))),n=void 0)}return e}const n=t=>{const n=typeof t,e=null===t,o=Array.isArray(t),c=t instanceof Date;return(e?"null":c&&"date")||o&&"array"||!e&&!c&&!o&&"object"===n&&"object"||n},e=()=>"undefined"==typeof window,o=n=>new URLSearchParams("string"==typeof n?c(n):t([n,"optionalAccess",t=>t.toString,"optionalCall",t=>t()])||""),c=n=>t([n,"access",t=>t.split,"call",t=>t("?"),"optionalAccess",t=>t[1]])||n||"",r=(t,n)=>JSON.stringify(t)===JSON.stringify(n);function a(t){const n=new URLSearchParams;return i(t,[...new URLSearchParams(window.location.search).entries()]).forEach((([t,e])=>n.set(t,e))),n.toString()}function s(t,n){return Object.fromEntries(i(t,Object.entries(n||{})))}function i(t,n){const e=Object.keys(t);return n.filter((([t])=>e.includes(t))).map((([t,n])=>[t.replaceAll("+"," "),n]))}function l(t,n,e){const o=Object.assign({},t,n);return Object.entries(t).forEach((([n])=>{const c=n,r=void 0!==e[c];o[c]=r?e[c]:t[c]})),o}export{l as assignValue,s as filterUnknownParams,a as filterUnknownParamsClient,o as getParams,r as isEqual,e as isSSR,n as typeOf}; diff --git a/package.json b/package.json index cc3ed03..5b26a7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "state-in-url", - "version": "2.6.0", + "version": "3.0.0", "description": "Library to store complex state in browser URL, includes hook for NextJS, hook for pure React, and low level helpers. https://state-in-url-asmyshlyaev177.vercel.app/", "homepage": "https://github.com/asmyshlyaev177/state-in-url", "keywords": [