-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update hook parameters to match props names (#49)
* chore: update hook parameters to match props names * fix: failing test
- Loading branch information
1 parent
4da5874
commit a29b0cf
Showing
5 changed files
with
32 additions
and
21 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
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,31 +1,40 @@ | ||
import { dateStringRegExp } from './date-string-regexp' | ||
|
||
export function extractDatePartsFromDateString(dateString: string) { | ||
export function extractDatePartsFromDateString( | ||
dateString: string, | ||
format?: 'YYYY-MM-DD' | 'DD-MM-YYYY' | ||
) { | ||
const parts = dateString.match(dateStringRegExp) | ||
|
||
if (!parts) { | ||
throw new Error(`Date string is invalid, received "${dateString}"`) | ||
} | ||
|
||
let yearStr, monthStr, dayStr, format | ||
let yearStr, monthStr, dayStr, detectedFormat | ||
|
||
if (parts[1]) { | ||
// Match for YYYY-MM-DD | ||
yearStr = parts[1] | ||
monthStr = parts[3] | ||
dayStr = parts[5] | ||
format = 'YYYY-MM-DD' | ||
detectedFormat = 'YYYY-MM-DD' | ||
} else { | ||
// Match for DD-MM-YYYY | ||
dayStr = parts[6] | ||
monthStr = parts[8] | ||
yearStr = parts[10] | ||
format = 'DD-MM-YYYY' | ||
detectedFormat = 'DD-MM-YYYY' | ||
} | ||
|
||
if (format && detectedFormat !== format.toUpperCase()) { | ||
throw new Error( | ||
`Date string format does not match the specified format. Expected ${format}, but got ${detectedFormat}` | ||
) | ||
} | ||
|
||
const year = parseInt(yearStr, 10) | ||
const month = parseInt(monthStr, 10) | ||
const day = parseInt(dayStr, 10) | ||
|
||
return { year, month, day, format } | ||
return { year, month, day, format: detectedFormat } | ||
} |
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