Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add quarter in calendar date #328

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
NicklasKnell marked this conversation as resolved.
Show resolved Hide resolved
}
}
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);
});
});
});