Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter method #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const ICAL = require('ical.js');
// See also https://github.com/mozilla-comm/ical.js/issues/195
const timezones = require('./zones-compiled.json');

const T = () => true;
const F = () => false;

class IcalExpander {
constructor(opts) {
this.maxIterations = opts.maxIterations != null ? opts.maxIterations : 1000;
Expand All @@ -30,10 +33,9 @@ class IcalExpander {
}
}

between(after, before) {
function isEventWithinRange(startTime, endTime) {
return (!after || endTime >= after.getTime()) &&
(!before || startTime <= before.getTime());
filter(till, predicate) {
function isEventIncluded(times) {
return !till(times) && predicate(times);
}

function getTimes(eventOrOccurrence) {
Expand Down Expand Up @@ -81,18 +83,18 @@ class IcalExpander {
if (next) {
const occurrence = event.getOccurrenceDetails(next);

const { startTime, endTime } = getTimes(occurrence);

const times = getTimes(occurrence);
const { startTime } = times;
const isOccurrenceExcluded = exdates.indexOf(startTime) !== -1;

// TODO check that within same day?
const exception = exceptions.find(ex => ex.uid === event.uid && ex.recurrenceId.toJSDate().getTime() === occurrence.startDate.toJSDate().getTime());

// We have passed the max date, stop
if (before && startTime > before.getTime()) break;
if (till(times)) break;

// Check that we are within our range
if (isEventWithinRange(startTime, endTime)) {
if (isEventIncluded(times)) {
if (exception) {
ret.events.push(exception);
} else if (!isOccurrenceExcluded) {
Expand All @@ -106,15 +108,22 @@ class IcalExpander {
return;
}

// Non-recurring event:
const { startTime, endTime } = getTimes(event);

if (isEventWithinRange(startTime, endTime)) ret.events.push(event);
if (isEventIncluded(getTimes(event))) ret.events.push(event);
});

return ret;
}

between(after, before) {
const beforeTime = before && before.getTime();
const till = before ? ({ startTime }) => startTime > beforeTime : F;

const afterTime = after && after.getTime();
const predicate = after ? ({ endTime }) => endTime >= afterTime : T;

return this.filter(till, predicate);
}

before(before) {
return this.between(undefined, before);
}
Expand Down