-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
35 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { toRange } from '@/lib/utils/url.utils'; | ||
|
||
test('extract date range', () => { | ||
expect(toRange('')).toMatchInlineSnapshot(`null`); | ||
|
||
expect(toRange('2022-01-01')).toMatchInlineSnapshot(` | ||
{ | ||
"from": "2022-01-01", | ||
"to": undefined, | ||
} | ||
`); | ||
|
||
expect(toRange('2022-01-01.2022-01-05')).toMatchInlineSnapshot(` | ||
{ | ||
"from": "2022-01-01", | ||
"to": "2022-01-05", | ||
} | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import qs from 'query-string'; | ||
// Note: only checks the format, can still produce invalid dates (like 2022-02-31) | ||
const DATE_RANGE_REGEX = | ||
/(?<from>\d{4}-(\d{2})-(\d{2}))(?:\.(?<to>\d{4}-(\d{2})-(\d{2})))?/; | ||
|
||
/** | ||
* Used to parse a dynamic route segment and use it as query params. | ||
* | ||
* This is done because using `searchParams` (the real query params) will cause | ||
* pages to switch to dynamic mode. | ||
*/ | ||
export const pq = (query: string = '') => qs.parse(decodeURIComponent(query)); | ||
export const toRange = (val: string = '') => { | ||
const result = val.match(DATE_RANGE_REGEX); | ||
return result ? result.groups! : null; | ||
}; |