Skip to content

Commit

Permalink
chore: update hook parameters to match props names (#49)
Browse files Browse the repository at this point in the history
* chore: update hook parameters to match props names

* fix: failing test
  • Loading branch information
alaa-yahia authored Aug 1, 2024
1 parent 4da5874 commit a29b0cf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
11 changes: 5 additions & 6 deletions src/hooks/useDatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type DatePickerOptions = {
}) => void
minDate?: string
maxDate?: string
format?: string
validation?: string
format?: 'YYYY-MM-DD' | 'DD-MM-YYYY'
strictValidation?: boolean
}

export type UseDatePickerReturn = UseNavigationReturnType & {
Expand All @@ -57,7 +57,7 @@ export const useDatePicker: UseDatePickerHookType = ({
minDate,
maxDate,
format,
validation,
strictValidation,
options,
}) => {
const calendar = getCustomCalendarIfExists(
Expand All @@ -83,7 +83,8 @@ export const useDatePicker: UseDatePickerHookType = ({
...resolvedOptions,
minDateString: minDate,
maxDateString: maxDate,
validation: validation,
strictValidation,
format,
})

const date = result as Temporal.YearOrEraAndEraYear &
Expand All @@ -95,8 +96,6 @@ export const useDatePicker: UseDatePickerHookType = ({
format?: string
}

date.format = !date.format ? format : date.format

const temporalCalendar = useMemo(
() => Temporal.Calendar.from(resolvedOptions.calendar),
[resolvedOptions.calendar]
Expand Down
19 changes: 14 additions & 5 deletions src/utils/extract-date-parts-from-date-string.ts
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 }
}
3 changes: 2 additions & 1 deletion src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export const extractAndValidateDateString = (
options: PickerOptions & {
minDateString?: string
maxDateString?: string
validation?: string
strictValidation?: boolean
format?: 'YYYY-MM-DD' | 'DD-MM-YYYY'
}
): Temporal.PlainDateLike & {
isValid: boolean
Expand Down
8 changes: 4 additions & 4 deletions src/utils/validate-date-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ describe('validateDateString', () => {
expect(validation.isValid).toBe(false)
})

it('Should give a warning when date is less than the min date and validation is set to "warning"', () => {
it('Should give a warning when date is less than the min date and strictValidation is set to false', () => {
const date = '27-06-2015'
const minDate = '28-06-2015'
const options = { minDateString: minDate, validation: 'warning' }
const options = { minDateString: minDate, strictValidation: false }

const validation = validateDateString(date, options)
expect(validation.errorMessage).toBe('')
Expand All @@ -123,10 +123,10 @@ describe('validateDateString', () => {
expect(validation.isValid).toBe(true)
})

it('Should give a warning when date is greater than the max date and validation is set to "warning"', () => {
it('Should give a warning when date is greater than the max date and strictValidation is set to false"', () => {
const date = '27-06-2015'
const maxDate = '26-06-2015'
const options = { maxDateString: maxDate, validation: 'warning' }
const options = { maxDateString: maxDate, strictValidation: false }

const validation = validateDateString(date, options)
expect(validation.errorMessage).toBe('')
Expand Down
12 changes: 7 additions & 5 deletions src/utils/validate-date-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export function validateDateString(
calendar = 'gregory',
minDateString,
maxDateString,
validation = 'error', // "error" | "warning"
strictValidation = true,
format,
}: {
calendar?: SupportedCalendar
minDateString?: string
maxDateString?: string
validation?: string
strictValidation?: boolean
format?: 'YYYY-MM-DD' | 'DD-MM-YYYY'
} = {}
): {
isValid: boolean
Expand All @@ -66,7 +68,7 @@ export function validateDateString(
if (!dateString) {
throw new Error(`Date is not given`)
}
const dateParts = extractDatePartsFromDateString(dateString)
const dateParts = extractDatePartsFromDateString(dateString, format)

if (resolvedCalendar.toString() === 'nepali') {
const { isValid, errorMessage } = validateNepaliDate(
Expand Down Expand Up @@ -96,7 +98,7 @@ export function validateDateString(
})

if (Temporal.PlainDate.compare(date, minDate) < 0) {
if (validation === 'error') {
if (strictValidation) {
throw new Error(
`Date ${dateString} is less than the minimum allowed date ${minDateString}.`
)
Expand All @@ -114,7 +116,7 @@ export function validateDateString(
})

if (Temporal.PlainDate.compare(date, maxDate) > 0) {
if (validation === 'error') {
if (strictValidation) {
throw new Error(
`Date ${dateString} is greater than the maximum allowed date ${maxDateString}.`
)
Expand Down

0 comments on commit a29b0cf

Please sign in to comment.