From b6f160e90fa81f39172e78b2f71bc351924a988f Mon Sep 17 00:00:00 2001 From: Nabh Khanna Date: Mon, 16 Sep 2024 17:41:13 +0200 Subject: [PATCH 1/4] feat: add getQuarter in calendar date --- src/CalendarDate.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/CalendarDate.ts b/src/CalendarDate.ts index f8711b0..2469680 100644 --- a/src/CalendarDate.ts +++ b/src/CalendarDate.ts @@ -424,4 +424,11 @@ export class CalendarDate { ) ); } + + /** + * Returns the quarter of the year that the day is in. + */ + public getQuarter(): number { + return Math.floor((this.month - 1) / 3) + 1; + } } From e9afaabd7e93ffaaf725203798cb2ae96611e380 Mon Sep 17 00:00:00 2001 From: Nabh Khanna Date: Tue, 17 Sep 2024 13:01:02 +0200 Subject: [PATCH 2/4] feat: add tests --- src/CalendarDate.ts | 11 +++++++++-- test/CalendarDate.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/CalendarDate.ts b/src/CalendarDate.ts index 2469680..56532c4 100644 --- a/src/CalendarDate.ts +++ b/src/CalendarDate.ts @@ -426,9 +426,16 @@ export class CalendarDate { } /** - * Returns the quarter of the year that the day is in. + * Determines 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 getQuarter(): number { + public get quarter(): number { return Math.floor((this.month - 1) / 3) + 1; } } diff --git a/test/CalendarDate.test.ts b/test/CalendarDate.test.ts index 6b1db12..cb8c4ee 100644 --- a/test/CalendarDate.test.ts +++ b/test/CalendarDate.test.ts @@ -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 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); + }); + }); }); From 939be4bef5d23f20cd34c1138f3b19ca83e83c7a Mon Sep 17 00:00:00 2001 From: Nabh Khanna Date: Tue, 17 Sep 2024 13:07:26 +0200 Subject: [PATCH 3/4] fix: fix test description --- test/CalendarDate.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CalendarDate.test.ts b/test/CalendarDate.test.ts index cb8c4ee..c5a1842 100644 --- a/test/CalendarDate.test.ts +++ b/test/CalendarDate.test.ts @@ -1413,7 +1413,7 @@ describe('CalendarDate', () => { }); describe('Test of quarter', () => { - test('result is always between 1 and 4', () => { + test('Result is always between 1 and 4', () => { fc.assert( fc.property( fc.integer({ min: 200, max: 9900 }), @@ -1433,7 +1433,7 @@ describe('CalendarDate', () => { ), ); }); - test('Correct quarter for month', () => { + 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); From 0fc997453c3f8c34089cbc67dccdf6d285743965 Mon Sep 17 00:00:00 2001 From: Nabh Khanna Date: Wed, 18 Sep 2024 13:07:47 +0200 Subject: [PATCH 4/4] fix: fix comment --- src/CalendarDate.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/CalendarDate.ts b/src/CalendarDate.ts index 56532c4..fa3a016 100644 --- a/src/CalendarDate.ts +++ b/src/CalendarDate.ts @@ -426,14 +426,13 @@ export class CalendarDate { } /** - * Determines the quarter of the year (1-4) based on the month of the date. + * 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;