-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { | ||
SchedulerBuilder, | ||
DAY | ||
SchedulerBuilder, | ||
DAY | ||
} from './src' | ||
|
||
|
||
const schedule = new SchedulerBuilder() | ||
.from('2018-04-10T00:00:00Z') | ||
.to('2018-06-10T00:00:00Z') | ||
.every(DAY.MONDAY) | ||
.at('14') | ||
.build() | ||
.from('2018-04-10T00:00:00Z') | ||
.to('2018-06-10T00:00:00Z') | ||
.every(DAY.MONDAY) | ||
.at('14') | ||
.build() | ||
|
||
console.log(schedule); | ||
console.log(schedule); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,107 @@ | ||
import moment from 'moment' | ||
|
||
export const DAY = Object.freeze({ | ||
"MONDAY": 1, | ||
"TUESDAY": 2, | ||
"WEDNESDAY": 3, | ||
"THURSDAY": 4, | ||
"FRIDAY": 5, | ||
"SATURDAY": 6, | ||
"SUNDAY": 7, | ||
"WORKDAY": [1, 2, 3, 4, 5], | ||
"WORKDAYS": [1, 2, 3, 4, 5], | ||
"WEEKEND": [6, 7], | ||
"DAY": [1, 2, 3, 4, 5, 6, 7], | ||
"MONDAY": 1, | ||
"TUESDAY": 2, | ||
"WEDNESDAY": 3, | ||
"THURSDAY": 4, | ||
"FRIDAY": 5, | ||
"SATURDAY": 6, | ||
"SUNDAY": 7, | ||
"WORKDAY": [1, 2, 3, 4, 5], | ||
"WORKDAYS": [1, 2, 3, 4, 5], | ||
"WEEKEND": [6, 7], | ||
"DAY": [1, 2, 3, 4, 5, 6, 7], | ||
}) | ||
|
||
export class SchedulerBuilder { | ||
constructor() { | ||
this.day = [] | ||
this.and = this | ||
} | ||
|
||
from(from) { | ||
this.from = moment(from).utc().startOf('day') | ||
return this | ||
} | ||
constructor() { | ||
this.day = [] | ||
this.and = this | ||
} | ||
|
||
to(to) { | ||
this.to = moment(to).utc().endOf('day') | ||
return this | ||
} | ||
from(from) { | ||
this.from = moment(from).utc().startOf('day') | ||
return this | ||
} | ||
|
||
every(day) { | ||
if (Array.isArray(day)) { // for WORKDAYS | ||
this.day = this.day.concat(day) | ||
} else { | ||
this.day.push(day) | ||
} | ||
to(to) { | ||
this.to = moment(to).utc().endOf('day') | ||
return this | ||
} | ||
|
||
return this | ||
every(day) { | ||
if (Array.isArray(day)) { // for WORKDAYS | ||
this.day = this.day.concat(day) | ||
} else { | ||
this.day.push(day) | ||
} | ||
|
||
at(time) { | ||
this.time = moment(time, 'HH:mmZZ') | ||
return this | ||
} | ||
return this | ||
} | ||
|
||
_getNextFromIoWeekday(day) { | ||
const isoWeekdayToFind = moment().day(day).isoWeekday() | ||
const next = this.from.clone() | ||
while (isoWeekdayToFind !== next.isoWeekday()) { | ||
next.add(1, 'day') | ||
} | ||
return next | ||
} | ||
at(time) { | ||
this.time = moment(time, 'HH:mmZZ') | ||
return this | ||
} | ||
|
||
_getRepeat(day) { | ||
const isoWeekday = moment().day(day).isoWeekday() | ||
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++ | ||
} | ||
start.add(1, 'day') | ||
} | ||
return repeat | ||
_getNextFromIoWeekday(day) { | ||
const isoWeekdayToFind = moment().day(day).isoWeekday() | ||
const next = this.from.clone() | ||
while (isoWeekdayToFind !== next.isoWeekday()) { | ||
next.add(1, 'day') | ||
} | ||
return next | ||
} | ||
|
||
_parseTime(time) { | ||
return { | ||
hour: time.get('hour'), | ||
minute: time.get('minute') | ||
} | ||
_getRepeat(day) { | ||
const isoWeekday = moment().day(day).isoWeekday() | ||
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++ | ||
} | ||
start.add(1, 'day') | ||
} | ||
return repeat | ||
} | ||
|
||
checks() { | ||
// this.to must be after this.from | ||
if (!this.to.isAfter(this.from)) { | ||
throw new Error('The to date must be later than the from') | ||
} | ||
_parseTime(time) { | ||
return { | ||
hour: time.get('hour'), | ||
minute: time.get('minute') | ||
} | ||
} | ||
|
||
build() { | ||
this.checks() | ||
const str = [] | ||
for (let day of this.day) { | ||
const repeat = this._getRepeat(day) | ||
const computedDuration = repeat * 7 | ||
const duration = `P0Y0M${computedDuration}DT0H0M` | ||
const nextWeekDay = this._getNextFromIoWeekday(day).clone() | ||
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 | ||
checks() { | ||
// this.to must be after this.from | ||
if (!this.to.isAfter(this.from)) { | ||
throw new Error('The to date must be later than the from') | ||
} | ||
} | ||
|
||
unbuild(intervals) { | ||
if (!Array.isArray(intervals)) throw new TypeError('intervals must be an array of ISO8601 strings'); | ||
return intervals.map(interval => moment(interval.split('/')[1]).isoWeekday()); | ||
build() { | ||
this.checks() | ||
const str = [] | ||
for (let day of this.day) { | ||
const repeat = this._getRepeat(day) | ||
const computedDuration = repeat * 7 | ||
const duration = `P0Y0M${computedDuration}DT0H0M` | ||
const nextWeekDay = this._getNextFromIoWeekday(day).clone() | ||
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 | ||
} | ||
|
||
unbuild(intervals) { | ||
if (!Array.isArray(intervals)) throw new TypeError('intervals must be an array of ISO8601 strings'); | ||
return intervals.map(interval => moment(interval.split('/')[1]).isoWeekday()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters