Skip to content

Commit

Permalink
Implement Season next/prev
Browse files Browse the repository at this point in the history
See #12
  • Loading branch information
inukshuk committed Jan 12, 2018
1 parent d2add57 commit e9b4253
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
37 changes: 35 additions & 2 deletions src/season.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,36 @@ class Season extends ExtDateTime {
}

set season(season) {
return this.values[1] = Number(season)
return validate(this.values[1] = Number(season))
}

get values() {
return V.get(this)
}

// TODO next/prev
next(k = 1) {
let { season, year } = this

switch (true) {
case (season >= 21 && season <= 36):
[year, season] = inc(year, season, k, season - (season - 21) % 4, 4)
break
case (season >= 37 && season <= 39):
[year, season] = inc(year, season, k, 37, 3)
break
case (season >= 40 && season <= 41):
[year, season] = inc(year, season, k, 40, 2)
break
default:
throw new RangeError(`Cannot compute next/prev for season ${season}`)
}

return new Season(year, season)
}

prev(k = 1) {
return this.next(-k)
}

get min() { // eslint-disable-line complexity
switch (this.season) {
Expand Down Expand Up @@ -155,4 +177,15 @@ class Season extends ExtDateTime {
}
}

function validate(season) {
if (isNaN(season) || season < 21 || season === Infinity)
throw new RangeError(`invalid division of year: ${season}`)
return season
}

function inc(year, season, by, base, size) {
const m = (season + by) - base
return [year + Math.floor(m / size), validate(base + (m % size + size) % size)]
}

module.exports = Season
13 changes: 9 additions & 4 deletions test/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ describe('Interval', () => {
expect(S80.toEDTF()).to.be.eql('1980-21/1980-24')
})

//it('includes', () => {
// expect(S80.includes(new Date(1980, 8))).to.be.true
// expect(S80.includes(new Season(1980, 22))).to.be.true
//})
it('.covers', () => {
expect(S80.covers(new Season(1980, 22))).to.be.true
expect(S80.covers(new Date(1980, 8))).to.be.true
})

it('.includes', () => {
expect(S80.includes(new Season(1980, 22))).to.be.true
expect(S80.includes(new Date(1980, 8))).to.be.false
})
})
})
18 changes: 18 additions & 0 deletions test/season.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ describe('Season', () => {
})
})

describe('.next', () => {
it('positive', () => {
expect(new Season(2018, 21).next().toEDTF()).to.eql('2018-22')
expect(new Season(2018, 21).next(2).toEDTF()).to.eql('2018-23')
expect(new Season(2018, 21).next(3).toEDTF()).to.eql('2018-24')
expect(new Season(2018, 21).next(4).toEDTF()).to.eql('2019-21')
expect(new Season(2018, 25).next(9).toEDTF()).to.eql('2020-26')
})

it('negative', () => {
expect(new Season(2018, 24).next(-1).toEDTF()).to.eql('2018-23')
expect(new Season(2018, 24).next(-2).toEDTF()).to.eql('2018-22')
expect(new Season(2018, 24).next(-3).toEDTF()).to.eql('2018-21')
expect(new Season(2018, 24).next(-4).toEDTF()).to.eql('2017-24')
expect(new Season(2018, 24).next(-9).toEDTF()).to.eql('2016-23')
})
})

describe('.edtf', () => {
it('default', () =>
expect(new Season().edtf).to.match(/^\d\d\d\d-21$/))
Expand Down

0 comments on commit e9b4253

Please sign in to comment.