state-in-url 4.1.11 → 4.1.13

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.
@@ -1 +1 @@
1
- import{encode as f,decode as m}from"../encoder/encoder.mjs";import{getParams as c}from"../utils.mjs";function a(r,e,n){const o=c(n);for(const[t,i]of Object.entries(r||{})){const s=e?.[t];JSON.stringify(i)!==JSON.stringify(s)&&o.set(t,f(i))}return o.toString()}function d(r,e){return{...e||{},...Object.fromEntries([...c(r).entries()].map(([n,o])=>{const t=e?.[n];return[n,m(o,t)??t]}))}}export{d as decodeState,a as encodeState};
1
+ import{decode as f,encode as m}from"../encoder/encoder.mjs";import{getParams as c}from"../utils.mjs";function a(r,e,n){const o=c(n);for(const[t,i]of Object.entries(r||{})){const s=e?.[t];JSON.stringify(i)!==JSON.stringify(s)&&o.set(t,m(i))}return o.toString()}function d(r,e){return{...e||{},...Object.fromEntries([...c(r).entries()].map(([n,o])=>{const t=e?.[n];return[n,f(o,t)??t]}))}}export{d as decodeState,a as encodeState};
@@ -1 +1 @@
1
- import r from"react";import{useRouter as C,useSearchParams as H}from"next/navigation";import{parseSPObj as f}from"../../parseSPObj.mjs";import{useUrlStateBase as g}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as d,isSSR as v,filterUnknownParams as P,filterUnknownParamsClient as w}from"../../utils.mjs";function E(t,s){const a="defaultState"in t?t.defaultState:t,U="defaultState"in t?t.searchParams:s?.searchParams,l="defaultState"in t?t.useHistory:s?.useHistory,h="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:s?.scroll,replace:s?.replace},o=C(),b=r.useMemo(()=>({push:(...e)=>{l===void 0||l?d.push(...e):o.push(...e)},replace:(...e)=>{l===void 0||l?d.replace(...e):o.replace(...e)}}),[o]),{state:c,updateState:u,updateUrl:p,reset:i,getState:k}=g(a,b,({parse:e})=>v?f(P(a,U),a):e(w(a))),n=r.useMemo(()=>({...M,...h}),[]),m=r.useCallback((e,j)=>p(e,{...n,...j}),[p]),S=H();r.useLayoutEffect(()=>{u(P(a,f(Object.fromEntries([...S.entries()]),a)))},[S]);const y=r.useCallback(e=>{i({...n,...e})},[i]);return{setState:u,updateState:u,setUrl:m,updateUrl:m,urlState:c,state:c,reset:y,getState:k}}const M={replace:!0,scroll:!1};export{E as useUrlState};
1
+ import r from"react";import{useRouter as C,useSearchParams as H}from"next/navigation";import{parseSPObj as d}from"../../parseSPObj.mjs";import{useUrlStateBase as g}from"../../useUrlStateBase/useUrlStateBase.mjs";import{routerHistory as U,filterUnknownParams as h,isSSR as v,filterUnknownParamsClient as w}from"../../utils.mjs";function E(t,s){const a="defaultState"in t?t.defaultState:t,c="defaultState"in t?t.searchParams:s?.searchParams,l="defaultState"in t?t.useHistory:s?.useHistory,P="defaultState"in t?{scroll:t.scroll,replace:t.replace}:{scroll:s?.scroll,replace:s?.replace},u=C(),b=r.useMemo(()=>({push:(...e)=>{l===void 0||l?U.push(...e):u.push(...e)},replace:(...e)=>{l===void 0||l?U.replace(...e):u.replace(...e)}}),[u]),{state:p,updateState:o,updateUrl:i,reset:n,getState:k}=g(a,b,({parse:e})=>v?d(h(a,c),a):e(w(a,c))),m=r.useMemo(()=>({...M,...P}),[]),f=r.useCallback((e,j)=>i(e,{...m,...j}),[i]),S=H();r.useLayoutEffect(()=>{o(h(a,d(Object.fromEntries([...S.entries()]),a)))},[S]);const y=r.useCallback(e=>{n({...m,...e})},[n]);return{setState:o,updateState:o,setUrl:f,updateUrl:f,urlState:p,state:p,reset:y,getState:k}}const M={replace:!0,scroll:!1};export{E as useUrlState};
@@ -1 +1 @@
1
- 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};
1
+ 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]),[n]=g(),{state:c,updateState:p,updateUrl:i,getState:v,reset:S}=C(r,U,({parse:e})=>e(y(r,n.entries()))),m=s.useCallback((e,a)=>i(e,{...l,...a}),[i]);s.useEffect(()=>{p(H(r,h(r,k(Object.fromEntries([...n.entries()]),r))))},[n]);const R=s.useCallback(e=>{S({...l,...e})},[S]);return{setState:p,updateState:p,setUrl:m,updateUrl:m,urlState:c,state:c,reset:R,getState:v}}const w={replace:!0,preventScrollReset:!0};export{j as useUrlState};
@@ -6,6 +6,6 @@ export declare const subscribers: {
6
6
  };
7
7
  export declare const stateMap: {
8
8
  get: <T extends JSONCompatible | undefined>(objKey: T) => T | undefined;
9
- set: <T_1 extends JSONCompatible>(objKey: T_1, value: T_1) => void;
9
+ set: <T extends JSONCompatible>(objKey: T, value: T) => void;
10
10
  };
11
11
  export type Cb = () => void;
@@ -30,7 +30,7 @@ console.log(state); // Output: { name: 'Tom' }
30
30
  *
31
31
  * * Docs {@link https://github.com/asmyshlyaev177/state-in-url/tree/master/packages/urlstate/useUrlEncode#parse}
32
32
  */
33
- parse: (strOrSearchParams: string | URLSearchParams) => T;
33
+ parse: (strOrSearchParams: string | URLSearchParams) => typeof stateShape;
34
34
  /**
35
35
  Converts a state object to a URL query string.
36
36
  * * Example:
@@ -37,7 +37,7 @@ export declare function useUrlStateBase<T extends JSONCompatible>(defaultState:
37
37
  parse: ReturnType<typeof useUrlEncode<T>>["parse"];
38
38
  }) => T): {
39
39
  updateState: (value: Partial<T> | ((currState: T) => T)) => void;
40
- updateUrl: (value?: Partial<T> | ((currState: T) => T) | undefined, options?: Options) => void;
40
+ updateUrl: (value?: Parameters<(value: Partial<T> | ((currState: T) => T)) => void>[0], options?: Options) => void;
41
41
  state: T;
42
42
  reset: (options?: Options) => void;
43
43
  getState: () => T;
@@ -1 +1 @@
1
- import u from"react";import{TIMEOUT as L}from"../constants/constants.mjs";import{useInsertionEffect as T}from"../useInsertionEffect.mjs";import{useSharedState as v}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as y}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as R}from"../utils.mjs";function b(t,a,l){const{parse:s,stringify:o}=y(t),{state:c,getState:n,setState:r}=v(t,()=>l?.({parse:s})||t),m=u.useRef(void 0);T(()=>{const e=()=>{const f=s(R(t));r(f)};return window.addEventListener(U,e),()=>{window.removeEventListener(U,e),clearTimeout(m.current)}},[r]);const i=u.useRef([]),p=u.useCallback((e,f)=>{const d=typeof e=="function"?e(n()):{...n(),...e},h=o(d,C(t)),S=`${window.location.pathname}${h.length?"?":""}${h}${window.location.hash}`,g=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(S===g)return;r(d);const{replace:k,...E}=f||{};i.current.push([k?"replace":"push",S,E]),clearTimeout(m.current),queueMicrotask(()=>{i.current.length&&(m.current=setTimeout(()=>{const w=i.current.at(-1);i.current=[],a[w[0]](w[1],w[2])},L))})},[a,o,n]),$=u.useCallback(e=>{r(t),p(t,e)},[p,r]);return{updateState:r,updateUrl:p,state:c,reset:$,getState:n}}function C(t){const a=Object.keys(t),l=window.location.search,s=new URLSearchParams(l),o=new URLSearchParams;for(const[c,n]of s)!a.includes(c)&&o.set(c,n);return o}const U="popstate";export{b as useUrlStateBase};
1
+ import u from"react";import{TIMEOUT as y}from"../constants/constants.mjs";import{useInsertionEffect as L}from"../useInsertionEffect.mjs";import{useSharedState as T}from"../useSharedState/useSharedState.mjs";import{useUrlEncode as v}from"../useUrlEncode/useUrlEncode.mjs";import{filterUnknownParamsClient as R}from"../utils.mjs";function b(t,a,l){const{parse:c,stringify:r}=v(t),{state:s,getState:o,setState:n}=T(t,()=>l?.({parse:c})||t),w=u.useRef(void 0);L(()=>{const e=()=>{const m=c(R(t,P()));n(m)};return window.addEventListener(U,e),()=>{window.removeEventListener(U,e),clearTimeout(w.current)}},[n]);const i=u.useRef([]),f=u.useCallback((e,m)=>{const d=typeof e=="function"?e(o()):{...o(),...e},h=r(d,C(t)),S=`${window.location.pathname}${h.length?"?":""}${h}${window.location.hash}`,g=`${window.location.pathname}${window.location.search}${window.location.hash}`;if(S===g)return;n(d);const{replace:k,...E}=m||{};i.current.push([k?"replace":"push",S,E]),clearTimeout(w.current),queueMicrotask(()=>{i.current.length&&(w.current=setTimeout(()=>{const p=i.current.at(-1);i.current=[],a[p[0]](p[1],p[2])},y))})},[a,r,o]),$=u.useCallback(e=>{n(t),f(t,e)},[f,n]);return{updateState:n,updateUrl:f,state:s,reset:$,getState:o}}function C(t){const a=Object.keys(t),l=window.location.search,c=new URLSearchParams(l),r=new URLSearchParams;for(const[s,o]of c)!a.includes(s)&&r.set(s,o);return r}const U="popstate";function P(){return typeof window<"u"&&window.location.search||""}export{b as useUrlStateBase};
package/dist/utils.d.ts CHANGED
@@ -22,7 +22,7 @@ export type UnknownObj = object | {
22
22
  [key: string]: unknown;
23
23
  };
24
24
  export declare const isEqual: (val1: unknown, val2: unknown) => boolean;
25
- export declare function filterUnknownParamsClient<T extends object>(shape: T): string;
25
+ export declare function filterUnknownParamsClient<T extends object>(shape: T, params: string | URLSearchParams | Record<string, string> | object): string;
26
26
  export declare function filterUnknownParams<T extends object>(shape: T, searchParams?: object): T;
27
27
  export declare function filterUnknown<T extends object>(shape: T, entries: [key: string, value: string][]): string[][];
28
28
  export declare function assignValue<T extends object>(shape: T, newVal: Partial<T>): T;
package/dist/utils.mjs CHANGED
@@ -1 +1 @@
1
- const c=n=>{const t=typeof n,e=n===null,o=Array.isArray(n),r=n instanceof Date;return e&&"null"||r&&"date"||o&&"array"||!e&&!r&&!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,e=s(n,[...new URLSearchParams(window.location.search).entries()]);for(const[o,r]of e)t.set(o,r);return t.toString()}function p(n,t){return Object.fromEntries(s(n,Object.entries(t||{})))}function s(n,t){const e=Object.keys(n),o=[];for(let r=0;r<t.length;r++)e.includes(t[r]?.[0])&&o.push([t[r][0].replace(/\+/g," "),t[r][1]]);return o}function y(n,t){const e=Object.assign({},n);for(const[o]of Object.entries(n)){const r=o,a=t[r]!==void 0;e[r]=a?t[r]:n[r]}return e}const g={push:n=>{window&&window.history.pushState(null,"",n)},replace:n=>{window&&window.history.replaceState(null,"",n)}},S=/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,S as isSafari,g as routerHistory,c as typeOf};
1
+ const c=n=>{const t=typeof n,e=n===null,o=Array.isArray(n),r=n instanceof Date;return e&&"null"||r&&"date"||o&&"array"||!e&&!r&&!o&&t==="object"&&"object"||t},a=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,t){const e=new URLSearchParams,o=i(n,[...new URLSearchParams(t||"").entries()]);for(const[r,s]of o)e.set(r,s);return e.toString()}function p(n,t){return Object.fromEntries(i(n,Object.entries(t||{})))}function i(n,t){const e=Object.keys(n),o=[];for(let r=0;r<t.length;r++)e.includes(t[r]?.[0])&&o.push([t[r][0].replace(/\+/g," "),t[r][1]]);return o}function y(n,t){const e=Object.assign({},n);for(const[o]of Object.entries(n)){const r=o,s=t[r]!==void 0;e[r]=s?t[r]:n[r]}return e}const g={push:n=>{window&&window.history.pushState(null,"",n)},replace:n=>{window&&window.history.replaceState(null,"",n)}},S=/apple/i.test(!a&&navigator?.userAgent||"");export{y as assignValue,i as filterUnknown,p as filterUnknownParams,w as filterUnknownParamsClient,l as getParams,f as isEqual,a as isSSR,S as isSafari,g as routerHistory,c as typeOf};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "state-in-url",
3
- "version": "4.1.11",
3
+ "version": "4.1.13",
4
4
  "description": "Store state in URL as in object, types and structure are preserved, with TS validation. Same API as React.useState, wthout any hasssle or boilerplate. Next.js@14-15 and react-router@6-7.",
5
5
  "homepage": "https://state-in-url.dev",
6
6
  "repository": {
@@ -353,7 +353,7 @@
353
353
  "@vitest/coverage-istanbul": "^2.1.5",
354
354
  "auto-changelog": "^2.4.0",
355
355
  "babel-plugin-module-resolver": "^5.0.2",
356
- "chokidar": "^4.0.1",
356
+ "chokidar": "^4.0.3",
357
357
  "commitizen": "^4.3.0",
358
358
  "commitlint": "^19.8.0",
359
359
  "conventional-recommended-bump": "^10.0.0",
@@ -376,18 +376,18 @@
376
376
  "prettier": "^3.2.5",
377
377
  "prettier-plugin-tailwindcss": "^0.6.8",
378
378
  "release-it": "^17.6.0",
379
- "rollup": "^4.23.0",
380
- "rollup-plugin-esbuild": "^6.1.1",
379
+ "rollup": "^4.39.0",
380
+ "rollup-plugin-esbuild": "^6.2.1",
381
381
  "rollup-plugin-filesize": "^10.0.0",
382
382
  "rollup-plugin-sourcemaps": "^0.6.3",
383
383
  "semantic-release": "^24.1.0",
384
384
  "standard-version": "^9.5.0",
385
385
  "ts-node": "^10.9.2",
386
- "typescript": "^5.3.3",
386
+ "typescript": "^5.8.2",
387
387
  "unplugin-auto-import": "^0.18.5",
388
388
  "vitest": "^2.1.5",
389
389
  "wait-on": "^8.0.1",
390
- "wireit": "^0.14.9"
390
+ "wireit": "^0.14.11"
391
391
  },
392
392
  "keywords": [
393
393
  "state in url",