Skip to content

Commit

Permalink
Add simple normalization before encoding to text
Browse files Browse the repository at this point in the history
Closes: #13
  • Loading branch information
JonasWanke committed May 5, 2022
1 parent 9467d57 commit 204085d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/src/codecs/text/encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class RecurrenceRuleToTextEncoder extends Converter<RecurrenceRule, String> {

@override
String convert(RecurrenceRule input) {
input = _normalize(input);

final frequencyIntervalString =
l10n.frequencyInterval(input.frequency, input.actualInterval);
final output = StringBuffer(frequencyIntervalString);
Expand Down Expand Up @@ -58,6 +60,35 @@ class RecurrenceRuleToTextEncoder extends Converter<RecurrenceRule, String> {
return output.toString();
}

RecurrenceRule _normalize(RecurrenceRule input) {
// Incomplete!
input = input.copyWith(clearInterval: input.interval == 1);

if (input.frequency == Frequency.monthly) {
final byEveryWeekDay = {
for (final weekDay in DateTime.monday.rangeTo(DateTime.sunday))
ByWeekDayEntry(weekDay),
};
if (!input.hasBySeconds &&
!input.hasByMinutes &&
!input.hasByHours &&
DeepCollectionEquality.unordered()
.equals(input.byWeekDays, byEveryWeekDay) &&
!input.hasByMonthDays &&
!input.hasByYearDays &&
!input.hasByWeeks &&
!input.hasByMonths &&
input.hasBySetPositions) {
input = input.copyWith(
byWeekDays: {},
byMonthDays: input.bySetPositions,
bySetPositions: {},
);
}
}
return input;
}

void _convertDaily(RecurrenceRule input, StringBuffer output) {
assert(input.byWeekDays.noneHasOccurrence);

Expand Down
7 changes: 7 additions & 0 deletions test/codecs/text/monthly_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ void main() {
string: 'RRULE:FREQ=MONTHLY;INTERVAL=4;BYDAY=MO',
);

// https://github.com/JonasWanke/rrule/issues/13
testText(
'Monthly on the last day',
string:
'RRULE:FREQ=MONTHLY;INTERVAL=1;BYSETPOS=-1;BYDAY=MO,TU,WE,TH,FR,SA,SU',
);

// 0/1 digits in the comment before a text function mean whether each of
// bySetPositions, byMonths, byMonthDays & byWeekDays (in that order) is
// included.
Expand Down

0 comments on commit 204085d

Please sign in to comment.