Skip to content

Commit

Permalink
feat: add quarter in calendar date (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkgastromatic authored Sep 18, 2024
1 parent 19d0bf6 commit 79c18e4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/CalendarDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,17 @@ export class CalendarDate {
)
);
}

/**
* The quarter of the year (1-4) based on the month of the date.
*
* Quarter breakdown:
* - 1st Quarter: January to March (1-3)
* - 2nd Quarter: April to June (4-6)
* - 3rd Quarter: July to September (7-9)
* - 4th Quarter: October to December (10-12)
*/
public get quarter(): number {
return Math.floor((this.month - 1) / 3) + 1;
}
}
37 changes: 37 additions & 0 deletions test/CalendarDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1411,4 +1411,41 @@ describe('CalendarDate', () => {
}
});
});

describe('Test of quarter', () => {
test('Result is always between 1 and 4', () => {
fc.assert(
fc.property(
fc.integer({ min: 200, max: 9900 }),
fc.integer({ min: 1, max: 12 }),
fc.integer({ min: 1, max: 31 }),
(year, month, day) => {
// Arrange
const calendarDate = new CalendarDate(year, month, ensureValidDay(year, month, day));

// Act
const quarter = calendarDate.quarter;

// Assert
expect(quarter).toBeLessThan(5);
expect(quarter).toBeGreaterThan(0);
},
),
);
});
test('Correct quarter for each month', () => {
expect(new CalendarDate(2023, 1, 1).quarter).toBe(1);
expect(new CalendarDate(2023, 2, 1).quarter).toBe(1);
expect(new CalendarDate(2023, 3, 1).quarter).toBe(1);
expect(new CalendarDate(2023, 4, 1).quarter).toBe(2);
expect(new CalendarDate(2023, 5, 1).quarter).toBe(2);
expect(new CalendarDate(2023, 6, 1).quarter).toBe(2);
expect(new CalendarDate(2023, 7, 1).quarter).toBe(3);
expect(new CalendarDate(2023, 8, 1).quarter).toBe(3);
expect(new CalendarDate(2023, 9, 1).quarter).toBe(3);
expect(new CalendarDate(2023, 10, 1).quarter).toBe(4);
expect(new CalendarDate(2023, 11, 1).quarter).toBe(4);
expect(new CalendarDate(2023, 12, 1).quarter).toBe(4);
});
});
});

0 comments on commit 79c18e4

Please sign in to comment.