state-in-url 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +9 -7
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ pnpm add state-in-url
|
|
|
94
94
|
|
|
95
95
|
### 2. Edit tsconfig.json
|
|
96
96
|
|
|
97
|
-
set `"moduleResolution": "Node16"` or `"moduleResolution": "NodeNext"`
|
|
97
|
+
In `tsconfig.json` in `compilerOptions` set `"moduleResolution": "Node16"` or `"moduleResolution": "NodeNext"`
|
|
98
98
|
|
|
99
99
|
## useUrlState hook for Next.js
|
|
100
100
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("next/navigation");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(e);const o={string:"◖",boolean:"🗵",null:"∙null",undefined:"∙undefined",number:"∓",date:"⏲"},s=e=>{const t=null===e,n=Array.isArray(e),r=e instanceof Date;return(t?"null":r&&"date")||n&&"array"||!t&&!r&&!n&&"object"==typeof e&&"object"||typeof e},c=()=>"undefined"==typeof window,u=e=>new URLSearchParams("string"==typeof e?e:e?.toString?.()||""),a=(e,t)=>JSON.stringify(e)===JSON.stringify(t);function
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("next/navigation");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(e);const o={string:"◖",boolean:"🗵",null:"∙null",undefined:"∙undefined",number:"∓",date:"⏲"},s=e=>{const t=null===e,n=Array.isArray(e),r=e instanceof Date;return(t?"null":r&&"date")||n&&"array"||!t&&!r&&!n&&"object"==typeof e&&"object"||typeof e},c=()=>"undefined"==typeof window,u=e=>new URLSearchParams("string"==typeof e?a(e):e?.toString?.()||""),a=e=>e.split("?")?.[1]||e||"",i=(e,t)=>JSON.stringify(e)===JSON.stringify(t);function l(e){switch(s(e)){case"function":case"symbol":return"";case"date":return o.date+e.toISOString();case"string":return`${o.string}${encodeURIComponent(e)}`;case"number":return o.number+String(e);case"boolean":return o.boolean+String(e);case"object":case"array":return JSON.stringify(e,d).replaceAll('"',"'");case"null":return o.null;case"undefined":return o.undefined;default:return String(e)}}function f(e,t){return function(e,t){try{return JSON.parse(e,y)}catch{const n=p(e);return n!==g?n:t}}(e.replaceAll("'",'"'),t)}const d=(e,t)=>{const n=s(t);return e&&"object"!==n&&"array"!==n?l(t):t},p=e=>e===o.null?null:e!==o.undefined?e?.startsWith?.(o.number)?Number.parseFloat(e.replace(o.number,"")):e?.startsWith?.(o.boolean)?!!e.includes("true"):e?.startsWith?.(o.date)?new Date(e.slice(1)):e?.startsWith?.(o.string)?decodeURIComponent(e).replace(/^◖/,""):g:void 0,g=Symbol("isError"),y=(e,t)=>{const n="string"==typeof t,r=n&&p(t);return r===g?t:e&&n?r:t};const b=(e,t)=>{const n="string"==typeof t,r=n&&f(t?.replaceAll?.("'",'"'));return e&&n?r:t};function S(e,t){try{return JSON.parse(e,b)}catch{const n=p(e);return n!==g?n:t}}function h(e,t,n){const r=u(n);return Object.entries(e||{}).forEach((([e,n])=>{const o=t?.[e];JSON.stringify(n)!==JSON.stringify(o)&&r.set(e,l(n))})),r.toString()}function w(e,t){return{...t||{},...Object.fromEntries([...u(e).entries()].map((([e,n])=>{const r=t?.[e];return[e,f(n,r)??r]})))}}const m=new WeakMap,O=new WeakMap,v={get:e=>O.get(e)||[],add(e,t){const n=this.get(e);return O.set(e,n.concat(t)),()=>this.remove(e,t)},remove(e,t){const n=this.get(e).filter((e=>e!==t));n.length?O.set(e,n):O.delete(e)}},E={get:function(e){return m.get(e)},set:function(e,t){m.set(e,t)}},j=r.default.useInsertionEffect||r.default.useEffect;function N(e){const t=r.default.useCallback((function(t,n){return"object"===s(t)?h(t,e,n):""}),[e]);return{parse:r.default.useCallback((function(t){return w(t,e)}),[e]),stringify:t}}exports.decode=f,exports.decodeState=w,exports.encode=l,exports.encodeState=h,exports.useUrlEncode=N,exports.useUrlState=function(e,n){const{parse:o,stringify:s}=N(e),{state:u,getState:a,setState:l}=function(e,t){const n=r.default.useRef(e),[o,s]=r.default.useState((()=>{if(c())return t?t?.():n.current;const e=t?.()||E.get(n.current)||n.current,r=E.get(n.current);return i(r,e)||E.set(n.current,e),e})),u=r.default.useCallback((e=>{const t=E.get(n.current);if("function"==typeof e){const r=e(t);if(i(t,r))return;E.set(n.current,r),v.get(n.current).forEach((e=>{e()}))}else{if(i(t,e))return;E.set(n.current,e),v.get(n.current).forEach((e=>{e()}))}}),[]);return j((()=>{const e=v.add(n.current,(()=>{s(E.get(n.current)||n.current)}));return()=>{e()}}),[]),{state:o,getState:r.default.useCallback((()=>E.get(n.current)||n.current),[]),setState:u}}(e,(()=>{return c()?(t=n,r=e,{...r,...S(JSON.stringify(t),r)}):o(window.location.search);var t,r})),f=t.useRouter();r.default.useInsertionEffect((()=>{const e=()=>{const e=o(window.location.search);l(e)},t="popstate";return window.addEventListener(t,e),()=>{window.removeEventListener(t,e)}}),[l]);const d=r.default.useCallback(((e,t)=>{const n=window.location.search,r=`${window.location.pathname}${n.length&&!n.includes("?")?"?":""}${n}`;let o,c;"function"==typeof e?(o=e(a()),c=s(o)):(o=e??a(),c=s(o)),l(o);const u=`${window.location.pathname}${c.length?"?":""}${c}`;if(r!==u){const{replace:e,...n}=t||{};f[e?"replace":"push"](u,{scroll:!1,...n})}}),[f,s,a]);return{updateState:l,updateUrl:d,state:u}};
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"react";import{useRouter as e}from"next/navigation";const n={string:"◖",boolean:"🗵",null:"∙null",undefined:"∙undefined",number:"∓",date:"⏲"},r=t=>{const e=null===t,n=Array.isArray(t),r=t instanceof Date;return(e?"null":r&&"date")||n&&"array"||!e&&!r&&!n&&"object"==typeof t&&"object"||typeof t},c=()=>"undefined"==typeof window,o=t=>new URLSearchParams("string"==typeof t?t:t?.toString?.()||""),s=(t,e)=>JSON.stringify(t)===JSON.stringify(e);function u(t){switch(r(t)){case"function":case"symbol":return"";case"date":return n.date+t.toISOString();case"string":return`${n.string}${encodeURIComponent(t)}`;case"number":return n.number+String(t);case"boolean":return n.boolean+String(t);case"object":case"array":return JSON.stringify(t,
|
|
1
|
+
import t from"react";import{useRouter as e}from"next/navigation";const n={string:"◖",boolean:"🗵",null:"∙null",undefined:"∙undefined",number:"∓",date:"⏲"},r=t=>{const e=null===t,n=Array.isArray(t),r=t instanceof Date;return(e?"null":r&&"date")||n&&"array"||!e&&!r&&!n&&"object"==typeof t&&"object"||typeof t},c=()=>"undefined"==typeof window,o=t=>new URLSearchParams("string"==typeof t?s(t):t?.toString?.()||""),s=t=>t.split("?")?.[1]||t||"",a=(t,e)=>JSON.stringify(t)===JSON.stringify(e);function u(t){switch(r(t)){case"function":case"symbol":return"";case"date":return n.date+t.toISOString();case"string":return`${n.string}${encodeURIComponent(t)}`;case"number":return n.number+String(t);case"boolean":return n.boolean+String(t);case"object":case"array":return JSON.stringify(t,l).replaceAll('"',"'");case"null":return n.null;case"undefined":return n.undefined;default:return String(t)}}function i(t,e){return function(t,e){try{return JSON.parse(t,g)}catch{const n=f(t);return n!==d?n:e}}(t.replaceAll("'",'"'),e)}const l=(t,e)=>{const n=r(e);return t&&"object"!==n&&"array"!==n?u(e):e},f=t=>t===n.null?null:t!==n.undefined?t?.startsWith?.(n.number)?Number.parseFloat(t.replace(n.number,"")):t?.startsWith?.(n.boolean)?!!t.includes("true"):t?.startsWith?.(n.date)?new Date(t.slice(1)):t?.startsWith?.(n.string)?decodeURIComponent(t).replace(/^◖/,""):d:void 0,d=Symbol("isError"),g=(t,e)=>{const n="string"==typeof e,r=n&&f(e);return r===d?e:t&&n?r:e};const p=(t,e)=>{const n="string"==typeof e,r=n&&i(e?.replaceAll?.("'",'"'));return t&&n?r:e};function y(t,e){try{return JSON.parse(t,p)}catch{const n=f(t);return n!==d?n:e}}function b(t,e,n){const r=o(n);return Object.entries(t||{}).forEach((([t,n])=>{const c=e?.[t];JSON.stringify(n)!==JSON.stringify(c)&&r.set(t,u(n))})),r.toString()}function h(t,e){return{...e||{},...Object.fromEntries([...o(t).entries()].map((([t,n])=>{const r=e?.[t];return[t,i(n,r)??r]})))}}const S=new WeakMap,m=new WeakMap,w={get:t=>m.get(t)||[],add(t,e){const n=this.get(t);return m.set(t,n.concat(e)),()=>this.remove(t,e)},remove(t,e){const n=this.get(t).filter((t=>t!==e));n.length?m.set(t,n):m.delete(t)}},O={get:function(t){return S.get(t)},set:function(t,e){S.set(t,e)}},E=t.useInsertionEffect||t.useEffect;function N(e){const n=t.useCallback((function(t,n){return"object"===r(t)?b(t,e,n):""}),[e]);return{parse:t.useCallback((function(t){return h(t,e)}),[e]),stringify:n}}function v(n,r){const{parse:o,stringify:s}=N(n),{state:u,getState:i,setState:l}=function(e,n){const r=t.useRef(e),[o,s]=t.useState((()=>{if(c())return n?n?.():r.current;const t=n?.()||O.get(r.current)||r.current,e=O.get(r.current);return a(e,t)||O.set(r.current,t),t})),u=t.useCallback((t=>{const e=O.get(r.current);if("function"==typeof t){const n=t(e);if(a(e,n))return;O.set(r.current,n),w.get(r.current).forEach((t=>{t()}))}else{if(a(e,t))return;O.set(r.current,t),w.get(r.current).forEach((t=>{t()}))}}),[]);return E((()=>{const t=w.add(r.current,(()=>{s(O.get(r.current)||r.current)}));return()=>{t()}}),[]),{state:o,getState:t.useCallback((()=>O.get(r.current)||r.current),[]),setState:u}}(n,(()=>{return c()?(t=r,e=n,{...e,...y(JSON.stringify(t),e)}):o(window.location.search);var t,e})),f=e();t.useInsertionEffect((()=>{const t=()=>{const t=o(window.location.search);l(t)},e="popstate";return window.addEventListener(e,t),()=>{window.removeEventListener(e,t)}}),[l]);const d=t.useCallback(((t,e)=>{const n=window.location.search,r=`${window.location.pathname}${n.length&&!n.includes("?")?"?":""}${n}`;let c,o;"function"==typeof t?(c=t(i()),o=s(c)):(c=t??i(),o=s(c)),l(c);const a=`${window.location.pathname}${o.length?"?":""}${o}`;if(r!==a){const{replace:t,...n}=e||{};f[t?"replace":"push"](a,{scroll:!1,...n})}}),[f,s,i]);return{updateState:l,updateUrl:d,state:u}}export{i as decode,h as decodeState,u as encode,b as encodeState,N as useUrlEncode,v as useUrlState};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "state-in-url",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Library to store state in browser URL, includes hook for NextJS, hook for pure React, and low level helpers.",
|
|
5
5
|
"homepage": "https://github.com/asmyshlyaev177/state-in-url#readme",
|
|
6
6
|
"keywords": [
|
|
@@ -79,6 +79,7 @@
|
|
|
79
79
|
"build": "wireit",
|
|
80
80
|
"build:demo": "wireit",
|
|
81
81
|
"start:demo": "npm run build && cd packages/example-nextjs && npm run build && npm run start",
|
|
82
|
+
"kill-next": "wireit",
|
|
82
83
|
"cleanup": "rm -rf dist",
|
|
83
84
|
"setup": "playwright install --with-deps",
|
|
84
85
|
"cleanupDist": "wireit",
|
|
@@ -89,9 +90,13 @@
|
|
|
89
90
|
"test": {
|
|
90
91
|
"dependencies": [
|
|
91
92
|
"tsc",
|
|
92
|
-
"test:unit"
|
|
93
|
+
"test:unit",
|
|
94
|
+
"kill-next"
|
|
93
95
|
],
|
|
94
|
-
"command": "npm run test:int"
|
|
96
|
+
"command": "npm run test:int -- --retries=1"
|
|
97
|
+
},
|
|
98
|
+
"kill-next": {
|
|
99
|
+
"command": "ps aux | grep .next | grep -v grep | awk '{print $2}' | xargs kill -9 2>&1; exit 0"
|
|
95
100
|
},
|
|
96
101
|
"test:int": {
|
|
97
102
|
"command": "playwright test",
|
|
@@ -195,8 +200,8 @@
|
|
|
195
200
|
"@commitlint/cli": "^19.0.3",
|
|
196
201
|
"@commitlint/config-conventional": "^19.0.3",
|
|
197
202
|
"@playwright/test": "^1.45.1",
|
|
198
|
-
"@rollup/plugin-commonjs": "^25.0.7",
|
|
199
203
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
204
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
200
205
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
201
206
|
"@testing-library/dom": "^10.2.0",
|
|
202
207
|
"@testing-library/jest-dom": "^6.4.2",
|
|
@@ -220,11 +225,8 @@
|
|
|
220
225
|
"playwright": "^1.45.1",
|
|
221
226
|
"prettier": "^3.2.5",
|
|
222
227
|
"rollup": "^2.79.1",
|
|
223
|
-
"rollup-plugin-copy": "^3.5.0",
|
|
224
228
|
"rollup-plugin-filesize": "^10.0.0",
|
|
225
|
-
"rollup-plugin-ignore": "^1.0.10",
|
|
226
229
|
"rollup-plugin-sourcemaps": "^0.6.3",
|
|
227
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
228
230
|
"standard-version": "^9.5.0",
|
|
229
231
|
"ts-jest": "^29.1.5",
|
|
230
232
|
"ts-node": "^10.9.2",
|