rruleWrapper = ({
makeRule(config) {
this.configStart = Object.assign({}, config);
this.configEnd = Object.assign({}, config);
const configTemp = Object.assign({}, config);
this.configEnd.dtstart = configTemp.dtend
delete this.configStart.dtend
delete this.configEnd.dtend
this.ruleStart = new rrule.RRule(this.configStart)
this.ruleEnd = new rrule.RRule(this.configEnd)
return this
},
toString() {
return('DTEND:'+this.configEnd
.dtstart.toISOString().replace(/[^\w\s]|_/g, "")
.replace(/\s+/g, " ").slice(0, -4)+'Z\n'+this.ruleStart.toString())
},
fromString (rfcString) {
var dtEndString = rfcString.substr(rfcString.indexOf(":")+1,rfcString.indexOf("\n")- rfcString.indexOf(":")-1)
var dtEnd = luxon.DateTime.fromISO(dtEndString).toJSDate()
var rfcString = rfcString.substr(rfcString.indexOf("\n")+1)
var options = rrule.RRule.parseString(rfcString)
options.dtend = dtEnd
return this.makeRule(options)
},
after({dt, inclusive=false} = {}) {
var dtStart = this.ruleStart.after(dt, inclusive)
var dtEnd = this.ruleEnd.after(dt, inclusive)
// console.log(dt)
// console.log(dtStart)
// console.log(dtEnd)
if (dtStart > dtEnd) {
dtStart = this.ruleStart.before(dt, inclusive)
}
if (inclusive == false & dtEnd >= dt & dtStart <= dt) {
dtStart = this.ruleStart.after(dtStart) // Update dtStart to be the next start date after this one.
dtEnd = this.ruleEnd.after(dtEnd) // Update dtEnd to be the next end date after this one.
}
return {start: dtStart, end: dtEnd};
// return [dtStart, dtEnd];
},
before({dt, inclusive=false} = {}) {
// Returns the last recurrence interval before the given Date instance. Optionally can include the current interval
var dtStart = this.ruleStart.before(dt, inclusive);
var dtEnd = this.ruleEnd.before(dt, inclusive);
if (dtEnd < dtStart) {
dtEnd = this.ruleEnd.after(dt, inclusive)
}
if (inclusive == false & dtEnd >= dt & dtStart <= dt) {
dtStart = this.ruleStart.before(dtStart) // Update dtStart to be the start date right before this one.
dtEnd = this.ruleEnd.before(dtEnd) // Update dtEnd to be end date right before this one.
}
return {start: dtStart, end: dtEnd}; // output object
// return [dtStart, dtEnd];
},
between({sliceStart, sliceEnd, inclusive=false, count = 0} = {}) {
// Returns all the occurrences of the rrule between after and before (optionally for a certain count of occurences)
var startInterval = this.before({dt: sliceStart, inclusive: true})
var endInterval = this.after({dt: sliceEnd, inclusive: true})
if (startInterval.start <= sliceStart & sliceStart <= startInterval.end) {
console.log('here0')
if (inclusive == true) {
sliceStart = startInterval.start // sliceStart. Snap to Start 1
console.log('here1')
} else {
sliceStart = startInterval.end
console.log('here2')
}
}
if (endInterval.start <= sliceEnd & sliceEnd <= endInterval.end) {
if (inclusive == true) {
sliceEnd = endInterval.end // sliceEnd. Snap to End 2
console.log('here3')
} else {
sliceEnd = endInterval.start
console.log('here4')
}
}
if (count <= 0) {
this.startDates = this.ruleStart.between(sliceStart, sliceEnd, inclusive);
this.endDates= this.ruleEnd.between(sliceStart, sliceEnd, inclusive);
} else {
this.startDates = this.ruleStart.between(sliceStart, sliceEnd, inclusive, function (date, i){return i < count});
this.endDates= this.ruleEnd.between(sliceStart, sliceEnd, inclusive, function (date, i){return i < count});
}
var newArray = []
for (var i = 0; i < this.startDates.length; i++) {
newArray.push({start: this.startDates[i], end: this.endDates[i]})
// newArray.push([this.startDates[i], this.endDates[i]])
}
return newArray;
},
})