Skip to content

Commit

Permalink
feat: add quarter dates to pivot dates
Browse files Browse the repository at this point in the history
- this quarter start
- this quarter end
- last quarter start
- last quarter end
  • Loading branch information
alioguzhan committed Nov 16, 2023
1 parent d6633d3 commit a6f4d2f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/-temporal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ describe('PivotDate queries', () => {
expect(getPivotDate(PivotDate.THIS_YEAR_END, asofStr)).toBe('2022-12-31');
expect(getPivotDate(PivotDate.NEXT_YEAR_START, asofStr)).toBe('2023-01-01');
expect(getPivotDate(PivotDate.NEXT_YEAR_END, asofStr)).toBe('2023-12-31');
expect(getPivotDate(PivotDate.THIS_QUARTER_START, asofStr)).toBe('2022-07-01');
expect(getPivotDate(PivotDate.THIS_QUARTER_END, asofStr)).toBe('2022-09-30');
expect(getPivotDate(PivotDate.LAST_QUARTER_START, asofStr)).toBe('2022-04-01');
expect(getPivotDate(PivotDate.LAST_QUARTER_END, asofStr)).toBe('2022-06-30');
});

test('over Dayjs values', () => {
Expand All @@ -45,6 +49,10 @@ describe('PivotDate queries', () => {
expect(getPivotDate(PivotDate.THIS_YEAR_END, asofDjs)).toStrictEqual(dayjs('2022-12-31').endOf('day'));
expect(getPivotDate(PivotDate.NEXT_YEAR_START, asofDjs)).toStrictEqual(dayjs('2023-01-01').endOf('day'));
expect(getPivotDate(PivotDate.NEXT_YEAR_END, asofDjs)).toStrictEqual(dayjs('2023-12-31').endOf('day'));
expect(getPivotDate(PivotDate.THIS_QUARTER_START, asofDjs)).toStrictEqual(dayjs('2022-07-01').endOf('day'));
expect(getPivotDate(PivotDate.THIS_QUARTER_END, asofDjs)).toStrictEqual(dayjs('2022-09-30').endOf('day'));
expect(getPivotDate(PivotDate.LAST_QUARTER_START, asofDjs)).toStrictEqual(dayjs('2022-04-01').endOf('day'));
expect(getPivotDate(PivotDate.LAST_QUARTER_END, asofDjs)).toStrictEqual(dayjs('2022-06-30').endOf('day'));
});

test('over empty values', () => {
Expand Down
16 changes: 16 additions & 0 deletions src/-temporal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export enum PivotDate {
THIS_YEAR_END = 'this year end',
NEXT_YEAR_START = 'next year start',
NEXT_YEAR_END = 'next year end',
THIS_QUARTER_START = 'this quarter start',
THIS_QUARTER_END = 'this quarter end',
LAST_QUARTER_START = 'last quarter start',
LAST_QUARTER_END = 'last quarter end',
}

/* Signature of getPivotDate specialized over Dayjs argument. */
Expand Down Expand Up @@ -151,6 +155,18 @@ export function getPivotDate(p: PivotDate, x?: Dayjs | SDate): Dayjs | SDate {
case PivotDate.NEXT_YEAR_END:
pivot = asof.add(1, 'year').endOf('year');
break;
case PivotDate.THIS_QUARTER_START:
pivot = asof.startOf('quarter');
break;
case PivotDate.THIS_QUARTER_END:
pivot = asof.endOf('quarter');
break;
case PivotDate.LAST_QUARTER_START:
pivot = asof.subtract(1, 'quarter').startOf('quarter');
break;
case PivotDate.LAST_QUARTER_END:
pivot = asof.subtract(1, 'quarter').endOf('quarter');
break;
default:
throw Error('Unreachable case');
}
Expand Down

0 comments on commit a6f4d2f

Please sign in to comment.