-
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.
fix: restructuring useCalculateDecimalDate
- Loading branch information
Showing
6 changed files
with
43 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
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,44 +1,32 @@ | ||
export const useCalculateDecimalDate = (date: string, dataset: string | number, dateOfBirth: Date): string => { | ||
const millisecondsInDay = 1000 * 60 * 60 * 24; | ||
|
||
if (dataset === '0 to 13 weeks') { | ||
const millisecondsInWeek = millisecondsInDay * 7; | ||
|
||
const formattedDate: Date = new Date(date); | ||
const diffInMilliseconds = formattedDate.getTime() - dateOfBirth.getTime(); | ||
const diffInWeeks = (diffInMilliseconds / millisecondsInWeek); | ||
|
||
if (diffInWeeks < 0) return null; | ||
if (diffInWeeks > 13) return null; | ||
return diffInWeeks.toFixed(2); | ||
} | ||
|
||
if (dataset === '0 to 2 years' || dataset === '0 to 5 years') { | ||
const millisecondsInMonth = millisecondsInDay * 30.44; | ||
|
||
const formattedDate: Date = new Date(date); | ||
const diffInMilliseconds = formattedDate.getTime() - dateOfBirth.getTime(); | ||
const diffInMonths = (diffInMilliseconds / millisecondsInMonth); | ||
import { DataSetLabels } from '../../../types/chartDataTypes'; | ||
|
||
if (diffInMonths < 0) return null; | ||
interface DatasetMap { | ||
[x: string]: () => string; | ||
} | ||
|
||
if (dataset === '0 to 2 years' && diffInMonths > 24) return null; | ||
if (dataset === '0 to 5 years' && diffInMonths > 60) return null; | ||
|
||
return diffInMonths.toFixed(2); | ||
} | ||
export const useCalculateDecimalDate = (date: string, dataset: string, dateOfBirth: Date): string => { | ||
const millisecondsInDay = 1000 * 60 * 60 * 24; | ||
const formattedDate: Date = new Date(date); | ||
const diffInMilliseconds = formattedDate.getTime() - dateOfBirth.getTime(); | ||
|
||
if (dataset === '2 to 5 years') { | ||
const calculateDiffInMonths = (maxMonths: number | null = null): string => { | ||
const millisecondsInMonth = millisecondsInDay * 30.44; | ||
|
||
const formattedDate: Date = new Date(date); | ||
const diffInMilliseconds = formattedDate.getTime() - dateOfBirth.getTime(); | ||
const diffInMonths = (diffInMilliseconds / millisecondsInMonth); | ||
|
||
if (diffInMonths < 24) return null; | ||
if (diffInMonths > 60) return null; | ||
const diffInMonths = diffInMilliseconds / millisecondsInMonth; | ||
if (diffInMonths < 0 || (maxMonths !== null && diffInMonths > maxMonths)) return null; | ||
return diffInMonths.toFixed(2); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const datasetMap: DatasetMap = { | ||
[DataSetLabels.w_0_13]: () => { | ||
const millisecondsInWeek = millisecondsInDay * 7; | ||
const diffInWeeks = diffInMilliseconds / millisecondsInWeek; | ||
if (diffInWeeks < 0 || diffInWeeks > 13) return null; | ||
return diffInWeeks.toFixed(2); | ||
}, | ||
[DataSetLabels.y_0_2]: () => calculateDiffInMonths(24), | ||
[DataSetLabels.y_0_5]: () => calculateDiffInMonths(60), | ||
[DataSetLabels.y_2_5]: () => calculateDiffInMonths(60), | ||
}; | ||
|
||
return datasetMap[dataset]?.() ?? null; | ||
}; |
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