Skip to content

Commit

Permalink
Extract getMonthYear function to reduce copy-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
rikurauhala committed Jun 28, 2024
1 parent cfef76f commit 61f877c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/components/Commits/Commits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import CommitsChart from './CommitsChart'
import Loading from './Loading'
import NoData from './NoData'

const getMonthYear = (date: Date): string => {
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`
}

const getCommitCountsByMonth = (commits: Commit[]): Record<string, number> => {
return commits.reduce(
(counts, commit) => {
const commitDate = new Date(commit.date)
const monthYear = `${commitDate.getFullYear()}-${String(commitDate.getMonth() + 1).padStart(
2,
'0'
)}`
const monthYear = getMonthYear(commitDate)
counts[monthYear] = (counts[monthYear] || 0) + 1
return counts
},
Expand Down Expand Up @@ -98,6 +99,7 @@ const Commits = (): JSX.Element => {
const earliestDate = new Date(
Math.min(...Object.keys(commitCountsByPeriod).map((key) => new Date(key).getTime()))
)

const latestDate = new Date(
Math.max(...Object.keys(commitCountsByPeriod).map((key) => new Date(key).getTime()))
)
Expand All @@ -106,9 +108,7 @@ const Commits = (): JSX.Element => {
const currentDate = new Date(earliestDate)
while (currentDate <= latestDate) {
const period =
mode === CommitsMode.MONTH
? `${currentDate.getFullYear()}-${String(currentDate.getMonth() + 1).padStart(2, '0')}`
: currentDate.getFullYear().toString()
mode === CommitsMode.MONTH ? getMonthYear(currentDate) : currentDate.getFullYear().toString()
if (!(period in commitCountsByPeriod)) {
periodsWithZeroCommits[period] = 0
}
Expand Down

0 comments on commit 61f877c

Please sign in to comment.