Skip to content
This repository was archived by the owner on May 1, 2021. It is now read-only.

Commit d4b8e5d

Browse files
committed
Add optional equals function
1 parent b2abe26 commit d4b8e5d

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/params.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ export const NumberParam: QueryParamConfig<
2727
* For flat objects where values are strings
2828
*/
2929
export const ObjectParam: QueryParamConfig<
30-
| {
31-
[key: string]: string | undefined;
32-
}
33-
| null
34-
| undefined,
30+
{ [key: string]: string | undefined } | null | undefined,
3531
{ [key: string]: string | undefined } | null | undefined
3632
> = {
3733
encode: Serialize.encodeObject,
@@ -77,6 +73,20 @@ export const DateParam: QueryParamConfig<
7773
> = {
7874
encode: Serialize.encodeDate,
7975
decode: Serialize.decodeDate,
76+
equals: (
77+
valueA: Date | null | undefined,
78+
valueB: Date | null | undefined
79+
) => {
80+
if (valueA === valueB) return true;
81+
if (valueA == null || valueB == null) return valueA === valueB;
82+
83+
// ignore time of day
84+
return (
85+
valueA.getFullYear() === valueB.getFullYear() &&
86+
valueA.getMonth() === valueB.getMonth() &&
87+
valueA.getDate() === valueB.getDate()
88+
);
89+
},
8090
};
8191

8292
/**
@@ -88,6 +98,15 @@ export const DateTimeParam: QueryParamConfig<
8898
> = {
8999
encode: Serialize.encodeDateTime,
90100
decode: Serialize.decodeDateTime,
101+
equals: (
102+
valueA: Date | null | undefined,
103+
valueB: Date | null | undefined
104+
) => {
105+
if (valueA === valueB) return true;
106+
if (valueA == null || valueB == null) return valueA === valueB;
107+
108+
return valueA.valueOf() === valueB.valueOf();
109+
},
91110
};
92111

93112
/**
@@ -105,11 +124,7 @@ export const BooleanParam: QueryParamConfig<
105124
* For flat objects where the values are numbers
106125
*/
107126
export const NumericObjectParam: QueryParamConfig<
108-
| {
109-
[key: string]: number | null | undefined;
110-
}
111-
| null
112-
| undefined,
127+
{ [key: string]: number | null | undefined } | null | undefined,
113128
{ [key: string]: number | null | undefined } | null | undefined
114129
> = {
115130
encode: Serialize.encodeNumericObject,

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface QueryParamConfig<D, D2 = D> {
1919

2020
/** Convert the query param string value to its native type */
2121
decode: (value: string | (string | null)[] | null | undefined) => D2;
22+
23+
/** Checks if two values are equal (otherwise typically shallowEqual will be used) */
24+
equals?: (valueA: D | D2, valueB: D | D2) => boolean;
2225
}
2326

2427
/**

0 commit comments

Comments
 (0)