jkbrzt / rrule

JavaScript library for working with recurrence rules for calendar dates as defined in the iCalendar RFC and more.
https://jkbrzt.github.io/rrule
Other
3.33k stars 511 forks source link

Add count to between functionality #491

Open MosheLOptimove opened 2 years ago

MosheLOptimove commented 2 years ago

Hi,

I would like to recommend adding a count parameter to the "Between" functionality. This functionality exists in the Python version and is quite useful here.

I've seen it while trying to check if today is in the recurrence. The standard way to do that is using "after", testing for null and then checking it happens today:

function isDayInRole(roleSettings, timestamp) {
    const startOfDay = moment(timestamp).startOf('day');
    const rule = new RRule(roleSettings);
    const next = rule.after(start);
    return next && moment(next).isSame(startOfDay, 'day');
}

Which prevent the simple one-liner of the logic (the last two lines).

A second approach is to use Between and see we got results:

function isDayInRole(roleSettings, timestamp) {
    const startOfDay = moment(timestamp).startOf('day');
    const endOfDay = moment(timestamp).endOf('day');
    const rule = new RRule(roleSettings);
    return rule.between(startOfDay, endOfDay).length > 0;
}

But this can take a long time since it runs on all occurrences (think of minutely).

Given the option of count can help these kind of test as between(after,before,count=1) will act as isBetween functionality withouth the overhead of converting/running a long loop. On the same note, isBetween will also help me with my need, but count can also help the issue of infinite loop I've seen: https://github.com/jakubroztocil/rrule/issues/481

Thanks, Moshe