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

Commit

Permalink
Merge pull request #24 from pbeshai/add-optional-equals
Browse files Browse the repository at this point in the history
Add optional equals function
  • Loading branch information
pbeshai authored May 6, 2020
2 parents b2abe26 + d4b8e5d commit 29e1aa8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export const NumberParam: QueryParamConfig<
* For flat objects where values are strings
*/
export const ObjectParam: QueryParamConfig<
| {
[key: string]: string | undefined;
}
| null
| undefined,
{ [key: string]: string | undefined } | null | undefined,
{ [key: string]: string | undefined } | null | undefined
> = {
encode: Serialize.encodeObject,
Expand Down Expand Up @@ -77,6 +73,20 @@ export const DateParam: QueryParamConfig<
> = {
encode: Serialize.encodeDate,
decode: Serialize.decodeDate,
equals: (
valueA: Date | null | undefined,
valueB: Date | null | undefined
) => {
if (valueA === valueB) return true;
if (valueA == null || valueB == null) return valueA === valueB;

// ignore time of day
return (
valueA.getFullYear() === valueB.getFullYear() &&
valueA.getMonth() === valueB.getMonth() &&
valueA.getDate() === valueB.getDate()
);
},
};

/**
Expand All @@ -88,6 +98,15 @@ export const DateTimeParam: QueryParamConfig<
> = {
encode: Serialize.encodeDateTime,
decode: Serialize.decodeDateTime,
equals: (
valueA: Date | null | undefined,
valueB: Date | null | undefined
) => {
if (valueA === valueB) return true;
if (valueA == null || valueB == null) return valueA === valueB;

return valueA.valueOf() === valueB.valueOf();
},
};

/**
Expand All @@ -105,11 +124,7 @@ export const BooleanParam: QueryParamConfig<
* For flat objects where the values are numbers
*/
export const NumericObjectParam: QueryParamConfig<
| {
[key: string]: number | null | undefined;
}
| null
| undefined,
{ [key: string]: number | null | undefined } | null | undefined,
{ [key: string]: number | null | undefined } | null | undefined
> = {
encode: Serialize.encodeNumericObject,
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export interface QueryParamConfig<D, D2 = D> {

/** Convert the query param string value to its native type */
decode: (value: string | (string | null)[] | null | undefined) => D2;

/** Checks if two values are equal (otherwise typically shallowEqual will be used) */
equals?: (valueA: D | D2, valueB: D | D2) => boolean;
}

/**
Expand Down

0 comments on commit 29e1aa8

Please sign in to comment.