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

fix: check first week of next year, get period by date [LIBS-688] #74

Merged
merged 1 commit into from
Oct 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ describe('Ethiopic Calendar period by date calculation', () => {
expect(actual?.id).toBe('2014W1')
})

it('should return "2018W1" for period type "WEEKLY" on "2017-13-04"', () => {
const actual = getFixedPeriodByDate({
periodType: 'WEEKLY',
date: '2017-13-04',
calendar: 'ethiopic',
locale: 'en',
})

expect(actual?.id).toBe('2018W1')
})

it('should return "2010WedW52" for period type "WEEKLYWED" on "2011-01-01"', () => {
const actual = getFixedPeriodByDate({
periodType: 'WEEKLYWED',
Expand Down Expand Up @@ -136,7 +147,7 @@ describe('Ethiopic Calendar period by date calculation', () => {
expect(actual?.id).toBe('2014BiW26')
})

it('should return "2012SunW1" for period type "BIWEEKLY" on "2012-01-01"', () => {
it('should return "2012BiW1" for period type "BIWEEKLY" on "2012-01-01"', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just fixing typo in test description

const actual = getFixedPeriodByDate({
periodType: 'BIWEEKLY',
date: '2012-01-01',
Expand All @@ -146,6 +157,17 @@ describe('Ethiopic Calendar period by date calculation', () => {

expect(actual?.id).toBe('2012BiW1')
})

it('should return "2018BiW1" for period type "BIWEEKLY" on "2017-13-04"', () => {
const actual = getFixedPeriodByDate({
periodType: 'BIWEEKLY',
date: '2017-13-04',
calendar: 'ethiopic',
locale: 'en',
})

expect(actual?.id).toBe('2018BiW1')
})
})

describe('monthly periods', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('Gregorian Calendar period by date calculation', () => {
expect(actual?.id).toBe('2022W1')
})

it('should return "2025W1" for period type "WEEKLY" on "2024-12-31"', () => {
const periodType = 'WEEKLY'
const date = '2024-12-31'
const actual = getFixedPeriodByDate({ periodType, date, calendar })

expect(actual?.id).toBe('2025W1')
})

it('should return "2018WedW52" for period type "WEEKLYWED" on "2019-01-01"', () => {
const periodType = 'WEEKLYWED'
const date = '2019-01-01'
Expand Down Expand Up @@ -94,6 +102,14 @@ describe('Gregorian Calendar period by date calculation', () => {
expect(actual?.id).toBe('2023SunW1')
})

it('should return "2025SunW1" for period type "WEEKLYSUN" on "2024-12-31"', () => {
const periodType = 'WEEKLYSUN'
const date = '2024-12-31'
const actual = getFixedPeriodByDate({ periodType, date, calendar })

expect(actual?.id).toBe('2025SunW1')
})

it('should return "2020BiW26" for period type "BIWEEKLY" on "2021-01-01"', () => {
const periodType = 'BIWEEKLY'
const date = '2021-01-01'
Expand All @@ -102,13 +118,21 @@ describe('Gregorian Calendar period by date calculation', () => {
expect(actual?.id).toBe('2020BiW26')
})

it('should return "2020SunW1" for period type "BIWEEKLY" on "2020-01-01"', () => {
it('should return "2020BiW1" for period type "BIWEEKLY" on "2020-01-01"', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just fixing typo in test description

const periodType = 'BIWEEKLY'
const date = '2020-01-01'
const actual = getFixedPeriodByDate({ periodType, date, calendar })

expect(actual?.id).toBe('2020BiW1')
})

it('should return "2025BiW1" for period type "BIWEEKLY" on "2024-12-31"', () => {
const periodType = 'BIWEEKLY'
const date = '2024-12-31'
const actual = getFixedPeriodByDate({ periodType, date, calendar })

expect(actual?.id).toBe('2025BiW1')
})
})

describe('monthly periods', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ describe('Nepali Calendar period by date calculation', () => {
expect(actual.id).toBe('2078W1')
})

it('should return "2083W1" for period type "WEEKLY" on "2082-12-30"', () => {
const actual = getFixedPeriodByDate({
periodType: 'WEEKLY',
date: '2082-12-30',
calendar: 'nepali',
locale: 'en',
})

expect(actual.id).toBe('2083W1')
})

it('should return "2075WedW53" for period type "WEEKLYWED" on "2076-01-01"', () => {
const actual = getFixedPeriodByDate({
periodType: 'WEEKLYWED',
Expand Down Expand Up @@ -81,6 +92,17 @@ describe('Nepali Calendar period by date calculation', () => {
expect(actual.id).toBe('2076ThuW1')
})

it('should return "2069ThuW1" for period type "WEEKLYTHU" on "2068-12-30"', () => {
const actual = getFixedPeriodByDate({
periodType: 'WEEKLYTHU',
date: '2068-12-30',
calendar: 'nepali',
locale: 'en',
})

expect(actual.id).toBe('2069ThuW1')
})

it('should return "2077SatW53" for period type "WEEKLYSAT" on "2078-01-01"', () => {
const actual = getFixedPeriodByDate({
periodType: 'WEEKLYSAT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const getWeeklyFixedPeriodByDate: GetWeeklyFixedPeriodByDate = ({
startingDay: 1,
})

// if start date of first period of year is after current date get last
// period of last year
// if current date is before start date of first period of year
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just modified the language of the comment here, so that it more closely matched the actual logical operation performed below

// get last period of previous year
if (date < weeklyPeriods[0].startDate) {
return generateFixedPeriodsWeekly({
year: year - 1,
Expand All @@ -32,6 +32,17 @@ const getWeeklyFixedPeriodByDate: GetWeeklyFixedPeriodByDate = ({
}).slice(-1)[0]
}

// if current date is after end date of last period of year
// get first period of following year
if (date > weeklyPeriods.slice(-1)[0].endDate) {
return generateFixedPeriodsWeekly({
year: year + 1,
calendar,
periodType,
startingDay: 1,
})[0]
}

const fixedPeriod = weeklyPeriods.find((currentPeriod) => {
return (
// On or after start date of current period
Expand Down
Loading