react-magic-search-params 2.0.1 → 2.1.2
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/LICENSE +20 -20
- package/README.md +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +1 -1
- package/package.json +110 -98
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Gabriel S.
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Gabriel S.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|

|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Type-safe query/search parameter management for React Router, built as an extension over useSearchParams.
|
|
11
11
|
|
|
12
12
|
## Installation
|
|
13
13
|
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var V=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var ue=Object.getOwnPropertyNames;var me=Object.prototype.hasOwnProperty;var Pe=(m,
|
|
1
|
+
"use strict";var V=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var ue=Object.getOwnPropertyNames;var me=Object.prototype.hasOwnProperty;var Pe=(m,d)=>{for(var g in d)V(m,g,{get:d[g],enumerable:!0})},le=(m,d,g,p)=>{if(d&&typeof d=="object"||typeof d=="function")for(let y of ue(d))!me.call(m,y)&&y!==g&&V(m,y,{get:()=>d[y],enumerable:!(p=ie(d,y))||p.enumerable});return m};var pe=m=>le(V({},"__esModule",{value:!0}),m);var ge={};Pe(ge,{useMagicSearchParams:()=>Y});module.exports=pe(ge);var Q=require("react-router-dom"),P=require("react"),Y=({mandatory:m={},optional:d={},defaultParams:g={},arraySerialization:p="csv",forceParams:y={},omitParamsByValues:q=[],codecs:F={},historyMode:E="push",resetOnChange:G={},paginationStrategy:w,unknownParamsPolicy:A="drop"})=>{let[i,L]=(0,Q.useSearchParams)(),j=(0,P.useRef)({}),U=(0,P.useRef)({}),I=(0,P.useCallback)((e,r)=>{L(e,{replace:(r??E)==="replace"})},[L,E]),l=(0,P.useMemo)(()=>({...m,...d}),[m,d]),J=(0,P.useMemo)(()=>Array.from(Object.keys(l)),[l]),v=(0,P.useCallback)(e=>Object.prototype.hasOwnProperty.call(l,e),[l]),X=e=>!Array.isArray(l[e])||p==="csv"?i.get(e):p==="repeat"?i.getAll(e):i.getAll(`${e}[]`),B=(0,P.useCallback)(()=>{if(A==="drop")return[];let e=[];for(let[r,a]of i.entries()){let s=r.endsWith("[]")?r.replace("[]",""):r;v(s)||e.push([r,a])}return e},[A,i,v]),H=(0,P.useCallback)(e=>{if(A==="drop")return e;let r=new URLSearchParams(e.toString()),a=B();for(let[s,t]of a)r.append(s,t);return r},[A,B]),_=(e,r)=>Array.isArray(e)&&Array.isArray(r)?e.length!==r.length?!1:e.every((a,s)=>a===r[s]):e===r,C=(0,P.useMemo)(()=>Object.keys(l).filter(e=>Array.isArray(l[e])),[l]),Z=(e,r)=>{let a={...e};return C.length===0||C.forEach(s=>{let t=[];switch(p){case"csv":{t=(i.get(s)||"").split(",").map(o=>o.trim()).filter(Boolean);break}case"repeat":{let n=i.getAll(s);t=n.length>0?n:[];break}case"brackets":{let n=i.getAll(`${s}[]`);t=n.length>0?n:[];break}default:t=(i.get(s)??"").split(",").map(o=>o.trim()).filter(Boolean)}if(r[s]!==void 0){let n=r[s],o=[];typeof n=="string"?o=t.includes(n)?t.filter(u=>u!==n):[...t,n]:Array.isArray(n)?o=Array.from(new Set([...n.map(String)])):o=t,a[s]=o}}),a},O=e=>{let r=new URLSearchParams,a=Object.keys(e);for(let s of a){let t=F[s];if(t?.serialize){let n=t.serialize(e[s],{key:s});if(n==null)continue;if(Array.isArray(n))if(Array.isArray(l[s]))if(p==="csv")r.set(s,n.join(","));else if(p==="repeat")for(let o of n)r.append(s,String(o));else for(let o of n)r.append(`${s}[]`,String(o));else n.length>0&&r.set(s,String(n[0]));else r.set(s,String(n));continue}if(Array.isArray(l[s])){let n=e[s];switch(p){case"csv":{r.set(s,n.join(","));break}case"repeat":{for(let o of n)r.append(s,String(o));break}case"brackets":{for(let o of n)r.append(`${s}[]`,String(o));break}default:r.set(s,n.join(","))}}else r.set(s,String(e[s]))}return r},h=(e,r)=>Object.prototype.hasOwnProperty.call(m,e)?m[e]:Object.prototype.hasOwnProperty.call(g,e)?g[e]:r,W=({paramsForced:e,compareParams:r})=>Object.entries(e).every(([s,t])=>r[s]===t),x=e=>{let r=X(e),a=F[e];if(a?.parse)return a.parse(r,{key:e,searchParams:i});if(typeof l[e]=="number"){let s=Number.parseInt(String(r??""),10);if(Number.isNaN(s)){let t=h(e,0);return typeof t=="number"?t:0}return s}return typeof l[e]=="boolean"?String(r)==="true":Array.isArray(l[e])?p==="csv"?String(r??"").split(",").filter(Boolean):Array.isArray(r)?r:r?[r]:[]:Array.isArray(r)?r[0]??"":r??""},$=(e,r)=>{if(Array.isArray(l[e])){if(p==="brackets"){let t=i.getAll(`${e}[]`),n=O({[e]:t}).toString();return decodeURIComponent(n)}if(p==="csv"){let t=i.getAll(e),n=O({[e]:t}).toString();return decodeURIComponent(n)}let s=i.getAll(e);return O({[e]:s}).toString()}return r[e]},z=e=>{let r={};for(let[a,s]of e.entries())if(a.endsWith("[]")){let t=a.replace("[]","");r[t]?r[t].push(s):r[t]=[s]}else r[a]?Array.isArray(r[a])?r[a].push(s):r[a]=[r[a],s]:r[a]=s;return r},S=(0,P.useMemo)(()=>p==="brackets"?z(i):Object.fromEntries(i.entries()),[i,p]),M=({convert:e=!0}={})=>Object.keys(S).reduce((a,s)=>{if(Object.prototype.hasOwnProperty.call(l,s)){let t=p==="brackets"?s.replace("[]",""):s;a[t]=e===!0?x(t):$(s,S)}return a},{}),K=(e,r)=>{let a=String(e);return r?.convert!==!1?x(a):$(a,S)},ee=e=>e==null||typeof e!="object"?!1:Object.prototype.hasOwnProperty.call(e,"newParams")||Object.prototype.hasOwnProperty.call(e,"keepParams")||Object.prototype.hasOwnProperty.call(e,"historyMode"),re=e=>{let r=M({convert:!0});if(typeof e=="function"){let a=e(r);return ee(a)?{newParams:typeof a.newParams=="function"?a.newParams(r):a.newParams??{},keepParams:a.keepParams??{},historyMode:a.historyMode}:{newParams:a,keepParams:{},historyMode:void 0}}return e?{newParams:typeof e.newParams=="function"?e.newParams(r):e.newParams??{},keepParams:e.keepParams??{},historyMode:e.historyMode}:{newParams:{},keepParams:{},historyMode:void 0}},se=({currentParams:e,newParams:r,keepParams:a})=>{let s={...r},t={...a};for(let[n,o]of Object.entries(G))if(!(!o||o.length===0||!Object.prototype.hasOwnProperty.call(s,n)||_(s[n],e[n]))){for(let c of o)if(!Object.prototype.hasOwnProperty.call(y,c)){if(Object.prototype.hasOwnProperty.call(m,c)){s[c]=m[c],delete t[c];continue}if(Object.prototype.hasOwnProperty.call(g,c)){s[c]=g[c],delete t[c];continue}delete s[c],t[c]=!1}}return{newParams:s,keepParams:t}},ae=(e,r)=>{let a=M(),s=Object.entries(e).filter(([o])=>!Array.isArray(l[o])),t=Object.assign({...a,...Object.fromEntries(s)},y),n=Object.keys(t).reduce((o,u)=>{if(Object.prototype.hasOwnProperty.call(r,u)&&r[u]===!1)return o;let c=t[u],b=typeof c=="string"&&q.includes(c);return c!=null&&c!==""&&!b&&(o[u]=c),o},{});return{...m,...n}},te=e=>J.reduce((a,s)=>(Object.prototype.hasOwnProperty.call(e,s)&&(a[s]=e[s]),a),{}),ne=()=>{let e=C.length>0,r=M({convert:e});return Object.keys(r).reduce((s,t)=>(Object.prototype.hasOwnProperty.call(m,t)&&(s[t]=r[t]),s),{})},D=({keepMandatoryParams:e=!0,historyMode:r}={})=>{let a=O({...m,...e&&{...ne()},...y}),s=H(a);I(s,r)},f=e=>{let r=re(e),a=M({convert:!0}),s=se({currentParams:a,newParams:r.newParams,keepParams:r.keepParams}),t=s.newParams,n=s.keepParams;if(Object.keys(t).length===0&&Object.keys(n).length===0){D({historyMode:r.historyMode});return}let o=ae(t,n),u=Z(o,t),c=te(u),b=O(c),T=H(b);I(T,r.historyMode)},k=w?.mode==="page"?w:void 0,R=w?.mode==="offset"?w:void 0,N=w?.mode==="cursor"?w:void 0,oe={mode:w?.mode??"page",next:e=>{let r=w?.mode??"page";if(r==="cursor"){let n=N?.cursorKey??"cursor";typeof e=="string"&&f({newParams:{[n]:e}});return}if(r==="offset"){let n=R?.offsetKey??"offset",o=R?.limitKey??"limit",u=Number(K(n,{convert:!0})??0),c=Number(K(o,{convert:!0})??h(String(o),10)),b=Number.isFinite(u)?u:0,T=Number.isFinite(c)&&c>0?c:10;f({newParams:{[n]:b+T}});return}let a=k?k.pageKey??"page":"page",s=Number(K(a,{convert:!0})??h(String(a),1)),t=Number.isFinite(s)&&s>0?s:1;f({newParams:{[a]:t+1}})},prev:()=>{let e=w?.mode??"page";if(e==="cursor"){let t=N?.cursorKey??"cursor";f({keepParams:{[t]:!1}});return}if(e==="offset"){let t=R?.offsetKey??"offset",n=R?.limitKey??"limit",o=Number(K(t,{convert:!0})??0),u=Number(K(n,{convert:!0})??h(String(n),10)),c=Number.isFinite(o)?o:0,b=Number.isFinite(u)&&u>0?u:10;f({newParams:{[t]:Math.max(0,c-b)}});return}let r=k?k.pageKey??"page":"page",a=Number(K(r,{convert:!0})??h(String(r),1)),s=Number.isFinite(a)&&a>0?a:1;f({newParams:{[r]:Math.max(1,s-1)}})},reset:()=>{let e=w?.mode??"page";if(e==="cursor"){let s=N?.cursorKey??"cursor";f({keepParams:{[s]:!1}});return}if(e==="offset"){let s=R?.offsetKey??"offset",t=Number(h(String(s),0));f({newParams:{[s]:Number.isFinite(t)?t:0}});return}let r=k?k.pageKey??"page":"page",a=Number(h(String(r),1));f({newParams:{[r]:Number.isFinite(a)&&a>0?a:1}})},setCursor:e=>{let a=(w?.mode??"page")==="cursor"?N?.cursorKey??"cursor":"cursor";if(e==null||e===""){f({keepParams:{[a]:!1}});return}f({newParams:{[a]:e}})}},ce=(0,P.useCallback)((e,r)=>{let a=String(e);return j.current[a]=r,()=>{delete j.current[a],delete U.current[a]}},[]);return(0,P.useEffect)(()=>{for(let[e,r]of Object.entries(j.current)){let a=v(e)?x(e):i.get(e),s=U.current[e]??null;if(!_(a,s))for(let t of r)t({key:e,previousValue:s,currentValue:a});U.current[e]=a}},[S,v,i]),(0,P.useEffect)(()=>{let e=Object.keys(g),r=Object.keys(y);if(e.length===0&&r.length===0)return;function a(){let s=O(g).toString(),t=M(),n=O(t).toString();if(!W({paramsForced:y,compareParams:t})){f({newParams:{...g,...y}});return}let u=W({paramsForced:y,compareParams:g});if(e.length>0&&u){if(s===n)return;f({newParams:g})}}a()},[]),{searchParams:i,updateParams:f,clearParams:D,getParams:M,getParam:K,onChange:ce,pagination:oe}};0&&(module.exports={useMagicSearchParams});
|
package/dist/index.d.mts
CHANGED
|
@@ -35,6 +35,8 @@ type PaginationStrategy<TParams extends Record<string, unknown>> = {
|
|
|
35
35
|
type ResetOnChangeRules<TParams extends Record<string, unknown>> = Partial<Record<ParamKey<TParams>, Array<ParamKey<TParams>>>>;
|
|
36
36
|
type UnknownParamsPolicy = 'drop' | 'preserve';
|
|
37
37
|
type HistoryMode = 'push' | 'replace';
|
|
38
|
+
type BuiltInOmitParamValue = 'all' | 'default' | 'unknown' | 'none' | 'void';
|
|
39
|
+
type OmitParamValue = BuiltInOmitParamValue | (string & {});
|
|
38
40
|
type OnChangeEvent<TParams extends Record<string, unknown>> = {
|
|
39
41
|
key: keyof TParams;
|
|
40
42
|
previousValue: unknown;
|
|
@@ -50,7 +52,7 @@ interface UseMagicSearchParamsOptions<M extends Record<string, unknown>, O exten
|
|
|
50
52
|
defaultParams?: Partial<MergeParams<M, O>>;
|
|
51
53
|
forceParams?: Partial<MergeParams<M, O>>;
|
|
52
54
|
arraySerialization?: 'csv' | 'repeat' | 'brackets';
|
|
53
|
-
omitParamsByValues?: Array<
|
|
55
|
+
omitParamsByValues?: Array<OmitParamValue>;
|
|
54
56
|
codecs?: ParamCodecs<MergeParams<M, O>>;
|
|
55
57
|
historyMode?: HistoryMode;
|
|
56
58
|
resetOnChange?: ResetOnChangeRules<MergeParams<M, O>>;
|
|
@@ -96,4 +98,4 @@ declare const useMagicSearchParams: <M extends Record<string, unknown> & CommonP
|
|
|
96
98
|
};
|
|
97
99
|
};
|
|
98
100
|
|
|
99
|
-
export { type HistoryMode, type OnChangeEvent, type PaginationStrategy, type ParamCodec, type ResetOnChangeRules, type UnknownParamsPolicy, type UseMagicSearchParamsOptions, useMagicSearchParams };
|
|
101
|
+
export { type BuiltInOmitParamValue, type HistoryMode, type OmitParamValue, type OnChangeEvent, type PaginationStrategy, type ParamCodec, type ResetOnChangeRules, type UnknownParamsPolicy, type UseMagicSearchParamsOptions, useMagicSearchParams };
|
package/dist/index.d.ts
CHANGED
|
@@ -35,6 +35,8 @@ type PaginationStrategy<TParams extends Record<string, unknown>> = {
|
|
|
35
35
|
type ResetOnChangeRules<TParams extends Record<string, unknown>> = Partial<Record<ParamKey<TParams>, Array<ParamKey<TParams>>>>;
|
|
36
36
|
type UnknownParamsPolicy = 'drop' | 'preserve';
|
|
37
37
|
type HistoryMode = 'push' | 'replace';
|
|
38
|
+
type BuiltInOmitParamValue = 'all' | 'default' | 'unknown' | 'none' | 'void';
|
|
39
|
+
type OmitParamValue = BuiltInOmitParamValue | (string & {});
|
|
38
40
|
type OnChangeEvent<TParams extends Record<string, unknown>> = {
|
|
39
41
|
key: keyof TParams;
|
|
40
42
|
previousValue: unknown;
|
|
@@ -50,7 +52,7 @@ interface UseMagicSearchParamsOptions<M extends Record<string, unknown>, O exten
|
|
|
50
52
|
defaultParams?: Partial<MergeParams<M, O>>;
|
|
51
53
|
forceParams?: Partial<MergeParams<M, O>>;
|
|
52
54
|
arraySerialization?: 'csv' | 'repeat' | 'brackets';
|
|
53
|
-
omitParamsByValues?: Array<
|
|
55
|
+
omitParamsByValues?: Array<OmitParamValue>;
|
|
54
56
|
codecs?: ParamCodecs<MergeParams<M, O>>;
|
|
55
57
|
historyMode?: HistoryMode;
|
|
56
58
|
resetOnChange?: ResetOnChangeRules<MergeParams<M, O>>;
|
|
@@ -96,4 +98,4 @@ declare const useMagicSearchParams: <M extends Record<string, unknown> & CommonP
|
|
|
96
98
|
};
|
|
97
99
|
};
|
|
98
100
|
|
|
99
|
-
export { type HistoryMode, type OnChangeEvent, type PaginationStrategy, type ParamCodec, type ResetOnChangeRules, type UnknownParamsPolicy, type UseMagicSearchParamsOptions, useMagicSearchParams };
|
|
101
|
+
export { type BuiltInOmitParamValue, type HistoryMode, type OmitParamValue, type OnChangeEvent, type PaginationStrategy, type ParamCodec, type ResetOnChangeRules, type UnknownParamsPolicy, type UseMagicSearchParamsOptions, useMagicSearchParams };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useSearchParams as ie}from"react-router-dom";import{useMemo as N,useEffect as Q,useRef as Y,useCallback as k}from"react";var ue=({mandatory:
|
|
1
|
+
import{useSearchParams as ie}from"react-router-dom";import{useMemo as N,useEffect as Q,useRef as Y,useCallback as k}from"react";var ue=({mandatory:g={},optional:V={},defaultParams:f={},arraySerialization:l="csv",forceParams:y={},omitParamsByValues:q=[],codecs:F={},historyMode:E="push",resetOnChange:G={},paginationStrategy:p,unknownParamsPolicy:R="drop"})=>{let[i,L]=ie(),j=Y({}),U=Y({}),I=k((e,r)=>{L(e,{replace:(r??E)==="replace"})},[L,E]),m=N(()=>({...g,...V}),[g,V]),J=N(()=>Array.from(Object.keys(m)),[m]),A=k(e=>Object.prototype.hasOwnProperty.call(m,e),[m]),X=e=>!Array.isArray(m[e])||l==="csv"?i.get(e):l==="repeat"?i.getAll(e):i.getAll(`${e}[]`),B=k(()=>{if(R==="drop")return[];let e=[];for(let[r,a]of i.entries()){let s=r.endsWith("[]")?r.replace("[]",""):r;A(s)||e.push([r,a])}return e},[R,i,A]),H=k(e=>{if(R==="drop")return e;let r=new URLSearchParams(e.toString()),a=B();for(let[s,t]of a)r.append(s,t);return r},[R,B]),_=(e,r)=>Array.isArray(e)&&Array.isArray(r)?e.length!==r.length?!1:e.every((a,s)=>a===r[s]):e===r,C=N(()=>Object.keys(m).filter(e=>Array.isArray(m[e])),[m]),Z=(e,r)=>{let a={...e};return C.length===0||C.forEach(s=>{let t=[];switch(l){case"csv":{t=(i.get(s)||"").split(",").map(o=>o.trim()).filter(Boolean);break}case"repeat":{let n=i.getAll(s);t=n.length>0?n:[];break}case"brackets":{let n=i.getAll(`${s}[]`);t=n.length>0?n:[];break}default:t=(i.get(s)??"").split(",").map(o=>o.trim()).filter(Boolean)}if(r[s]!==void 0){let n=r[s],o=[];typeof n=="string"?o=t.includes(n)?t.filter(u=>u!==n):[...t,n]:Array.isArray(n)?o=Array.from(new Set([...n.map(String)])):o=t,a[s]=o}}),a},d=e=>{let r=new URLSearchParams,a=Object.keys(e);for(let s of a){let t=F[s];if(t?.serialize){let n=t.serialize(e[s],{key:s});if(n==null)continue;if(Array.isArray(n))if(Array.isArray(m[s]))if(l==="csv")r.set(s,n.join(","));else if(l==="repeat")for(let o of n)r.append(s,String(o));else for(let o of n)r.append(`${s}[]`,String(o));else n.length>0&&r.set(s,String(n[0]));else r.set(s,String(n));continue}if(Array.isArray(m[s])){let n=e[s];switch(l){case"csv":{r.set(s,n.join(","));break}case"repeat":{for(let o of n)r.append(s,String(o));break}case"brackets":{for(let o of n)r.append(`${s}[]`,String(o));break}default:r.set(s,n.join(","))}}else r.set(s,String(e[s]))}return r},w=(e,r)=>Object.prototype.hasOwnProperty.call(g,e)?g[e]:Object.prototype.hasOwnProperty.call(f,e)?f[e]:r,W=({paramsForced:e,compareParams:r})=>Object.entries(e).every(([s,t])=>r[s]===t),x=e=>{let r=X(e),a=F[e];if(a?.parse)return a.parse(r,{key:e,searchParams:i});if(typeof m[e]=="number"){let s=Number.parseInt(String(r??""),10);if(Number.isNaN(s)){let t=w(e,0);return typeof t=="number"?t:0}return s}return typeof m[e]=="boolean"?String(r)==="true":Array.isArray(m[e])?l==="csv"?String(r??"").split(",").filter(Boolean):Array.isArray(r)?r:r?[r]:[]:Array.isArray(r)?r[0]??"":r??""},$=(e,r)=>{if(Array.isArray(m[e])){if(l==="brackets"){let t=i.getAll(`${e}[]`),n=d({[e]:t}).toString();return decodeURIComponent(n)}if(l==="csv"){let t=i.getAll(e),n=d({[e]:t}).toString();return decodeURIComponent(n)}let s=i.getAll(e);return d({[e]:s}).toString()}return r[e]},z=e=>{let r={};for(let[a,s]of e.entries())if(a.endsWith("[]")){let t=a.replace("[]","");r[t]?r[t].push(s):r[t]=[s]}else r[a]?Array.isArray(r[a])?r[a].push(s):r[a]=[r[a],s]:r[a]=s;return r},v=N(()=>l==="brackets"?z(i):Object.fromEntries(i.entries()),[i,l]),K=({convert:e=!0}={})=>Object.keys(v).reduce((a,s)=>{if(Object.prototype.hasOwnProperty.call(m,s)){let t=l==="brackets"?s.replace("[]",""):s;a[t]=e===!0?x(t):$(s,v)}return a},{}),O=(e,r)=>{let a=String(e);return r?.convert!==!1?x(a):$(a,v)},ee=e=>e==null||typeof e!="object"?!1:Object.prototype.hasOwnProperty.call(e,"newParams")||Object.prototype.hasOwnProperty.call(e,"keepParams")||Object.prototype.hasOwnProperty.call(e,"historyMode"),re=e=>{let r=K({convert:!0});if(typeof e=="function"){let a=e(r);return ee(a)?{newParams:typeof a.newParams=="function"?a.newParams(r):a.newParams??{},keepParams:a.keepParams??{},historyMode:a.historyMode}:{newParams:a,keepParams:{},historyMode:void 0}}return e?{newParams:typeof e.newParams=="function"?e.newParams(r):e.newParams??{},keepParams:e.keepParams??{},historyMode:e.historyMode}:{newParams:{},keepParams:{},historyMode:void 0}},se=({currentParams:e,newParams:r,keepParams:a})=>{let s={...r},t={...a};for(let[n,o]of Object.entries(G))if(!(!o||o.length===0||!Object.prototype.hasOwnProperty.call(s,n)||_(s[n],e[n]))){for(let c of o)if(!Object.prototype.hasOwnProperty.call(y,c)){if(Object.prototype.hasOwnProperty.call(g,c)){s[c]=g[c],delete t[c];continue}if(Object.prototype.hasOwnProperty.call(f,c)){s[c]=f[c],delete t[c];continue}delete s[c],t[c]=!1}}return{newParams:s,keepParams:t}},ae=(e,r)=>{let a=K(),s=Object.entries(e).filter(([o])=>!Array.isArray(m[o])),t=Object.assign({...a,...Object.fromEntries(s)},y),n=Object.keys(t).reduce((o,u)=>{if(Object.prototype.hasOwnProperty.call(r,u)&&r[u]===!1)return o;let c=t[u],h=typeof c=="string"&&q.includes(c);return c!=null&&c!==""&&!h&&(o[u]=c),o},{});return{...g,...n}},te=e=>J.reduce((a,s)=>(Object.prototype.hasOwnProperty.call(e,s)&&(a[s]=e[s]),a),{}),ne=()=>{let e=C.length>0,r=K({convert:e});return Object.keys(r).reduce((s,t)=>(Object.prototype.hasOwnProperty.call(g,t)&&(s[t]=r[t]),s),{})},D=({keepMandatoryParams:e=!0,historyMode:r}={})=>{let a=d({...g,...e&&{...ne()},...y}),s=H(a);I(s,r)},P=e=>{let r=re(e),a=K({convert:!0}),s=se({currentParams:a,newParams:r.newParams,keepParams:r.keepParams}),t=s.newParams,n=s.keepParams;if(Object.keys(t).length===0&&Object.keys(n).length===0){D({historyMode:r.historyMode});return}let o=ae(t,n),u=Z(o,t),c=te(u),h=d(c),T=H(h);I(T,r.historyMode)},b=p?.mode==="page"?p:void 0,M=p?.mode==="offset"?p:void 0,S=p?.mode==="cursor"?p:void 0,oe={mode:p?.mode??"page",next:e=>{let r=p?.mode??"page";if(r==="cursor"){let n=S?.cursorKey??"cursor";typeof e=="string"&&P({newParams:{[n]:e}});return}if(r==="offset"){let n=M?.offsetKey??"offset",o=M?.limitKey??"limit",u=Number(O(n,{convert:!0})??0),c=Number(O(o,{convert:!0})??w(String(o),10)),h=Number.isFinite(u)?u:0,T=Number.isFinite(c)&&c>0?c:10;P({newParams:{[n]:h+T}});return}let a=b?b.pageKey??"page":"page",s=Number(O(a,{convert:!0})??w(String(a),1)),t=Number.isFinite(s)&&s>0?s:1;P({newParams:{[a]:t+1}})},prev:()=>{let e=p?.mode??"page";if(e==="cursor"){let t=S?.cursorKey??"cursor";P({keepParams:{[t]:!1}});return}if(e==="offset"){let t=M?.offsetKey??"offset",n=M?.limitKey??"limit",o=Number(O(t,{convert:!0})??0),u=Number(O(n,{convert:!0})??w(String(n),10)),c=Number.isFinite(o)?o:0,h=Number.isFinite(u)&&u>0?u:10;P({newParams:{[t]:Math.max(0,c-h)}});return}let r=b?b.pageKey??"page":"page",a=Number(O(r,{convert:!0})??w(String(r),1)),s=Number.isFinite(a)&&a>0?a:1;P({newParams:{[r]:Math.max(1,s-1)}})},reset:()=>{let e=p?.mode??"page";if(e==="cursor"){let s=S?.cursorKey??"cursor";P({keepParams:{[s]:!1}});return}if(e==="offset"){let s=M?.offsetKey??"offset",t=Number(w(String(s),0));P({newParams:{[s]:Number.isFinite(t)?t:0}});return}let r=b?b.pageKey??"page":"page",a=Number(w(String(r),1));P({newParams:{[r]:Number.isFinite(a)&&a>0?a:1}})},setCursor:e=>{let a=(p?.mode??"page")==="cursor"?S?.cursorKey??"cursor":"cursor";if(e==null||e===""){P({keepParams:{[a]:!1}});return}P({newParams:{[a]:e}})}},ce=k((e,r)=>{let a=String(e);return j.current[a]=r,()=>{delete j.current[a],delete U.current[a]}},[]);return Q(()=>{for(let[e,r]of Object.entries(j.current)){let a=A(e)?x(e):i.get(e),s=U.current[e]??null;if(!_(a,s))for(let t of r)t({key:e,previousValue:s,currentValue:a});U.current[e]=a}},[v,A,i]),Q(()=>{let e=Object.keys(f),r=Object.keys(y);if(e.length===0&&r.length===0)return;function a(){let s=d(f).toString(),t=K(),n=d(t).toString();if(!W({paramsForced:y,compareParams:t})){P({newParams:{...f,...y}});return}let u=W({paramsForced:y,compareParams:f});if(e.length>0&&u){if(s===n)return;P({newParams:f})}}a()},[]),{searchParams:i,updateParams:P,clearParams:D,getParams:K,getParam:O,onChange:ce,pagination:oe}};export{ue as useMagicSearchParams};
|
package/package.json
CHANGED
|
@@ -1,98 +1,110 @@
|
|
|
1
|
-
{
|
|
2
|
-
"description": "
|
|
3
|
-
"repository": {
|
|
4
|
-
"type": "git",
|
|
5
|
-
"url": "https://github.com/Gabriel117343/react-magic-search-params"
|
|
6
|
-
},
|
|
7
|
-
"homepage": "https://react-magic-search-params.netlify.app",
|
|
8
|
-
"
|
|
9
|
-
"react"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"query-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
"
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
"
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
"
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"description": "Type-safe React hook to manage URL query/search params with React Router useSearchParams.",
|
|
3
|
+
"repository": {
|
|
4
|
+
"type": "git",
|
|
5
|
+
"url": "git+https://github.com/Gabriel117343/react-magic-search-params.git"
|
|
6
|
+
},
|
|
7
|
+
"homepage": "https://react-magic-search-params.netlify.app",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/Gabriel117343/react-magic-search-params/issues"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"react",
|
|
13
|
+
"react-hook",
|
|
14
|
+
"react-router",
|
|
15
|
+
"react-router-dom",
|
|
16
|
+
"useSearchParams",
|
|
17
|
+
"search-params",
|
|
18
|
+
"query-params",
|
|
19
|
+
"url-search-params",
|
|
20
|
+
"url-state",
|
|
21
|
+
"typescript",
|
|
22
|
+
"type-safe",
|
|
23
|
+
"filters",
|
|
24
|
+
"pagination",
|
|
25
|
+
"sorting"
|
|
26
|
+
],
|
|
27
|
+
"version": "2.1.2",
|
|
28
|
+
"private": false,
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"main": "dist/index.cjs",
|
|
31
|
+
"module": "dist/index.js",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"typings": "dist/index.d.ts",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18"
|
|
40
|
+
},
|
|
41
|
+
"packageManager": "pnpm@10.7.1",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"start": "tsup --watch",
|
|
44
|
+
"dev": "tsup --watch",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"build": "tsup",
|
|
47
|
+
"test": "vitest run --environment jsdom",
|
|
48
|
+
"test:watch": "vitest --environment jsdom",
|
|
49
|
+
"lint": "tsc --noEmit",
|
|
50
|
+
"prepare": "tsup",
|
|
51
|
+
"size": "size-limit",
|
|
52
|
+
"analyze": "size-limit --why",
|
|
53
|
+
"prepack": "node -e \"const fs=require('fs');fs.copyFileSync('README.md','.README.repo.md');fs.copyFileSync('README_NPM.md','README.md');\"",
|
|
54
|
+
"postpack": "node -e \"const fs=require('fs');if(fs.existsSync('.README.repo.md')){fs.copyFileSync('.README.repo.md','README.md');fs.unlinkSync('.README.repo.md');}\""
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"react": ">=18 <20",
|
|
58
|
+
"react-router-dom": ">=6"
|
|
59
|
+
},
|
|
60
|
+
"husky": {
|
|
61
|
+
"hooks": {
|
|
62
|
+
"pre-commit": "tsdx lint"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"prettier": {
|
|
66
|
+
"printWidth": 80,
|
|
67
|
+
"semi": true,
|
|
68
|
+
"singleQuote": true,
|
|
69
|
+
"trailingComma": "es5"
|
|
70
|
+
},
|
|
71
|
+
"name": "react-magic-search-params",
|
|
72
|
+
"author": "Gabriel S.",
|
|
73
|
+
"size-limit": [
|
|
74
|
+
{
|
|
75
|
+
"path": "dist/index.cjs",
|
|
76
|
+
"limit": "10 KB"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"path": "dist/index.js",
|
|
80
|
+
"limit": "10 KB"
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"devDependencies": {
|
|
84
|
+
"@size-limit/preset-small-lib": "^11.2.0",
|
|
85
|
+
"@types/react": "^19.2.2",
|
|
86
|
+
"@types/react-dom": "^19.2.2",
|
|
87
|
+
"@testing-library/react": "^16.3.0",
|
|
88
|
+
"husky": "^9.1.7",
|
|
89
|
+
"jsdom": "^26.1.0",
|
|
90
|
+
"react": "^19.2.0",
|
|
91
|
+
"react-dom": "^19.2.0",
|
|
92
|
+
"react-router-dom": "^7.0.0",
|
|
93
|
+
"size-limit": "^11.2.0",
|
|
94
|
+
"tsup": "^8.5.0",
|
|
95
|
+
"tslib": "^2.8.1",
|
|
96
|
+
"typescript": "^5.8.3",
|
|
97
|
+
"vitest": "^3.2.4"
|
|
98
|
+
},
|
|
99
|
+
"exports": {
|
|
100
|
+
".": {
|
|
101
|
+
"types": "./dist/index.d.ts",
|
|
102
|
+
"import": "./dist/index.js",
|
|
103
|
+
"require": "./dist/index.cjs"
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
"publishConfig": {
|
|
107
|
+
"access": "public",
|
|
108
|
+
"registry": "https://npmjs.org"
|
|
109
|
+
}
|
|
110
|
+
}
|