Skip to content

Commit

Permalink
fix: check first week of subsequent year when geting period by date […
Browse files Browse the repository at this point in the history
…LIBS-688] (#74)
  • Loading branch information
tomzemp authored Oct 9, 2024
1 parent 53663f9 commit 8662fe0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
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"', () => {
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"', () => {
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
// 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

0 comments on commit 8662fe0

Please sign in to comment.