Skip to content

Commit

Permalink
Reindented to 2 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rocel committed Apr 12, 2018
1 parent 02a63c0 commit 2969c15
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 95 deletions.
16 changes: 8 additions & 8 deletions example.js
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);
166 changes: 83 additions & 83 deletions src/index.js
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());
}

}
11 changes: 7 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { SchedulerBuilder, DAY } from "../src";
import {
SchedulerBuilder,
DAY
} from "../src";

describe("SchedulerBuilder", () => {
it("initialize", () => {
Expand Down Expand Up @@ -104,11 +107,11 @@ describe("SchedulerBuilder", () => {
"R9/2018-04-12T14:00:00.000Z/P0Y0M63DT0H0M",
"R9/2018-04-13T14:00:00.000Z/P0Y0M63DT0H0M"
],
"[1, 4]" : [
"[1, 4]": [
"R8/2018-04-16T14:00:00.000Z/P0Y0M56DT0H0M",
"R9/2018-04-12T14:00:00.000Z/P0Y0M63DT0H0M"
],
"[1]" : ["R8/2018-04-16T14:30:00.000Z/P0Y0M56DT0H0M"],
"[1]": ["R8/2018-04-16T14:30:00.000Z/P0Y0M56DT0H0M"],
"[1,2,3,4,5,6,7]": [
"R8/2018-04-16T10:00:00.000Z/P0Y0M56DT0H0M",
"R9/2018-04-10T10:00:00.000Z/P0Y0M63DT0H0M",
Expand All @@ -132,4 +135,4 @@ describe("SchedulerBuilder", () => {
expect(() => new SchedulerBuilder().unbuild('ahahaha')).toThrow(new TypeError('intervals must be an array of ISO8601 strings'))
});
});
});
});

0 comments on commit 2969c15

Please sign in to comment.