Skip to content

Commit

Permalink
Make Interval API take seasons
Browse files Browse the repository at this point in the history
See #12
  • Loading branch information
inukshuk committed Jan 12, 2018
1 parent 9255bb8 commit faa12e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const assert = require('assert')
const ExtDate = require('./date')
const ExtDateTime = require('./interface')

const Season = require('./season')
const V = new WeakMap()


Expand Down Expand Up @@ -68,7 +68,7 @@ class Interval extends ExtDateTime {
if (value === Infinity || value === -Infinity)
return this.values[1] = Infinity

value = ExtDate.from(value)
value = getDateOrSeasonFrom(value)

if (value >= this.upper && this.upper != null)
throw new RangeError(`invalid lower bound: ${value}`)
Expand All @@ -87,7 +87,7 @@ class Interval extends ExtDateTime {
if (value === Infinity)
return this.values[1] = Infinity

value = ExtDate.from(value)
value = getDateOrSeasonFrom(value)

if (value <= this.lower)
throw new RangeError(`invalid upper bound: ${value}`)
Expand Down Expand Up @@ -130,4 +130,12 @@ class Interval extends ExtDateTime {
}
}

function getDateOrSeasonFrom(value) {
try {
return ExtDate.from(value)
} catch (de) {
return Season.from(value)
}
}

module.exports = Interval
19 changes: 18 additions & 1 deletion test/interval.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { Interval, Date } = require('..')
const { Interval, Date, Season } = require('..')

describe('Interval', () => {

Expand Down Expand Up @@ -62,4 +62,21 @@ describe('Interval', () => {
it('default', () =>
expect(new Interval().edtf).to.eql('/'))
})

describe('seasons (non-standard)', () => {
const S80 = new Interval('1980-21', '1980-24')

it('.lower', () => {
expect(S80.lower).to.be.instanceof(Season)
})

it('.edtf', () => {
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
//})
})
})

0 comments on commit faa12e8

Please sign in to comment.