Skip to content

Commit

Permalink
Added minutes to at() and fixed duration computing
Browse files Browse the repository at this point in the history
  • Loading branch information
rocel committed Apr 9, 2018
1 parent e474bae commit 6666bf2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
18 changes: 14 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class SchedulerBuilder {
return this
}

at(hours) {
this.hours = hours
at(time) {
this.time = moment(time, 'HH:mmZZ')
return this
}

Expand All @@ -57,7 +57,9 @@ export class SchedulerBuilder {
const nbDays = this.to.diff(this.from, 'days')
let start = this.from.clone()
let repeat = 0
const d = []
for (let i = 0; i <= nbDays; i++) {
d.push(start.format('DD-MM'))
if (isoWeekday === start.isoWeekday()) {
repeat++
}
Expand All @@ -66,6 +68,13 @@ export class SchedulerBuilder {
return repeat
}

_parseTime(time) {
return {
hour: time.get('hour'),
minute: time.get('minute')
}
}

checks() {
// this.to must be after this.from
if (!this.to.isAfter(this.from)) {
Expand All @@ -78,10 +87,11 @@ export class SchedulerBuilder {
const str = []
for (let day of this.day) {
const repeat = this._getRepeat(day)
const computedDuration = 22
const computedDuration = repeat * 7
const duration = `P0Y0M${computedDuration}DT0H0M`
const nextWeekDay = this._getNextFromIoWeekday(day).clone()
const start = nextWeekDay.set('hour', this.hours).toISOString()
const time = this._parseTime(this.time)
const start = nextWeekDay.set('hour', time.hour).set('minute', time.minute).toISOString()
str.push(`R${repeat}/${start}/${duration}`)
}
return str
Expand Down
26 changes: 18 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ describe('SchedulerBuilder', () => {
.every(DAY.MONDAY)
.at('14')
.build()
expect(schedule).toEqual(['R8/2018-04-16T14:00:00.000Z/P0Y0M22DT0H0M'])
expect(schedule).toEqual(['R8/2018-04-16T14:00:00.000Z/P0Y0M56DT0H0M'])
})

it('Gives the correct schedule for a single day at a specific time with minutes', () => {
const schedule = new SchedulerBuilder()
.from('2018-04-10T00:00:00Z')
.to('2018-06-10T00:00:00Z')
.every(DAY.MONDAY)
.at('14:30')
.build()
expect(schedule).toEqual(['R8/2018-04-16T14:30:00.000Z/P0Y0M56DT0H0M'])
})

it('Gives the correct schedule for 2 given days', () => {
Expand All @@ -48,8 +58,8 @@ describe('SchedulerBuilder', () => {
.at('14')
.build()
expect(schedule).toEqual([
'R8/2018-04-16T14:00:00.000Z/P0Y0M22DT0H0M',
'R9/2018-04-12T14:00:00.000Z/P0Y0M22DT0H0M'
'R8/2018-04-16T14:00:00.000Z/P0Y0M56DT0H0M',
'R9/2018-04-12T14:00:00.000Z/P0Y0M63DT0H0M'
])
})

Expand All @@ -61,11 +71,11 @@ describe('SchedulerBuilder', () => {
.at('14')
.build()
expect(schedule).toEqual([
'R8/2018-04-16T14:00:00.000Z/P0Y0M22DT0H0M',
'R9/2018-04-10T14:00:00.000Z/P0Y0M22DT0H0M',
'R9/2018-04-11T14:00:00.000Z/P0Y0M22DT0H0M',
'R9/2018-04-12T14:00:00.000Z/P0Y0M22DT0H0M',
'R9/2018-04-13T14:00:00.000Z/P0Y0M22DT0H0M'
'R8/2018-04-16T14:00:00.000Z/P0Y0M56DT0H0M',
'R9/2018-04-10T14:00:00.000Z/P0Y0M63DT0H0M',
'R9/2018-04-11T14:00:00.000Z/P0Y0M63DT0H0M',
'R9/2018-04-12T14:00:00.000Z/P0Y0M63DT0H0M',
'R9/2018-04-13T14:00:00.000Z/P0Y0M63DT0H0M'
])
})
})
Expand Down

0 comments on commit 6666bf2

Please sign in to comment.