Skip to content

Commit

Permalink
Add numericDayToIcalDay method
Browse files Browse the repository at this point in the history
  • Loading branch information
kewisch committed Feb 20, 2013
1 parent 847c67c commit fde8a9e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
21 changes: 16 additions & 5 deletions build/ical.js
Expand Up @@ -4201,6 +4201,19 @@ ICAL.TimezoneService = (function() {
return DOW_MAP[string];
};

/**
* Convert a numeric day value into its ical representation (SU, MO, etc..)
*
* @param {Numeric} numeric value of given day.
* @return {String} day ical day.
*/
ICAL.Recur.numericDayToIcalDay = function toIcalDay(num) {
//XXX: this is here so we can deal with possibly invalid number values.
// Also, this allows consistent mapping between day numbers and day
// names for external users.
return REVERSE_DOW_MAP[num];
};

var VALID_DAY_NAMES = /^(SU|MO|TU|WE|TH|FR|SA)$/;
var VALID_BYDAY_PART = /^([+-])?(5[0-3]|[1-4][0-9]|[1-9])?(SU|MO|TU|WE|TH|FR|SA)$/
var ALLOWED_FREQ = ['SECONDLY', 'MINUTELY', 'HOURLY',
Expand Down Expand Up @@ -4435,8 +4448,8 @@ ICAL.RecurIterator = (function() {
this.last.day += dow;
}
} else {
var wkMap = icalrecur_iterator._wkdayMap[this.dtstart.dayOfWeek()];
parts.BYDAY = [wkMap];
var dayName = ICAL.Recur.numericDayToIcalDay(this.dtstart.dayOfWeek());
parts.BYDAY = [dayName];
}
}

Expand Down Expand Up @@ -5434,7 +5447,7 @@ ICAL.RecurIterator = (function() {
return (this.check_contract_restriction("BYSECOND", this.last.second) &&
this.check_contract_restriction("BYMINUTE", this.last.minute) &&
this.check_contract_restriction("BYHOUR", this.last.hour) &&
this.check_contract_restriction("BYDAY", icalrecur_iterator._wkdayMap[dow]) &&
this.check_contract_restriction("BYDAY", ICAL.Recur.numericDayToIcalDay(dow)) &&
this.check_contract_restriction("BYWEEKNO", weekNo) &&
this.check_contract_restriction("BYMONTHDAY", this.last.day) &&
this.check_contract_restriction("BYMONTH", this.last.month) &&
Expand Down Expand Up @@ -5478,8 +5491,6 @@ ICAL.RecurIterator = (function() {

};

icalrecur_iterator._wkdayMap = ["", "SU", "MO", "TU", "WE", "TH", "FR", "SA"];

icalrecur_iterator._indexMap = {
"BYSECOND": 0,
"BYMINUTE": 1,
Expand Down
13 changes: 13 additions & 0 deletions lib/ical/recur.js
Expand Up @@ -203,6 +203,19 @@
return DOW_MAP[string];
};

/**
* Convert a numeric day value into its ical representation (SU, MO, etc..)
*
* @param {Numeric} numeric value of given day.
* @return {String} day ical day.
*/
ICAL.Recur.numericDayToIcalDay = function toIcalDay(num) {
//XXX: this is here so we can deal with possibly invalid number values.
// Also, this allows consistent mapping between day numbers and day
// names for external users.
return REVERSE_DOW_MAP[num];
};

var VALID_DAY_NAMES = /^(SU|MO|TU|WE|TH|FR|SA)$/;
var VALID_BYDAY_PART = /^([+-])?(5[0-3]|[1-4][0-9]|[1-9])?(SU|MO|TU|WE|TH|FR|SA)$/
var ALLOWED_FREQ = ['SECONDLY', 'MINUTELY', 'HOURLY',
Expand Down
8 changes: 3 additions & 5 deletions lib/ical/recur_iterator.js
Expand Up @@ -142,8 +142,8 @@ ICAL.RecurIterator = (function() {
this.last.day += dow;
}
} else {
var wkMap = icalrecur_iterator._wkdayMap[this.dtstart.dayOfWeek()];
parts.BYDAY = [wkMap];
var dayName = ICAL.Recur.numericDayToIcalDay(this.dtstart.dayOfWeek());
parts.BYDAY = [dayName];
}
}

Expand Down Expand Up @@ -1141,7 +1141,7 @@ ICAL.RecurIterator = (function() {
return (this.check_contract_restriction("BYSECOND", this.last.second) &&
this.check_contract_restriction("BYMINUTE", this.last.minute) &&
this.check_contract_restriction("BYHOUR", this.last.hour) &&
this.check_contract_restriction("BYDAY", icalrecur_iterator._wkdayMap[dow]) &&
this.check_contract_restriction("BYDAY", ICAL.Recur.numericDayToIcalDay(dow)) &&
this.check_contract_restriction("BYWEEKNO", weekNo) &&
this.check_contract_restriction("BYMONTHDAY", this.last.day) &&
this.check_contract_restriction("BYMONTH", this.last.month) &&
Expand Down Expand Up @@ -1185,8 +1185,6 @@ ICAL.RecurIterator = (function() {

};

icalrecur_iterator._wkdayMap = ["", "SU", "MO", "TU", "WE", "TH", "FR", "SA"];

icalrecur_iterator._indexMap = {
"BYSECOND": 0,
"BYMINUTE": 1,
Expand Down
21 changes: 21 additions & 0 deletions test/recur_test.js
Expand Up @@ -386,5 +386,26 @@ suite('recur', function() {
}(map));
}
});
suite('ICAL.Recur#numericDayToIcalDay', function() {
var expected = {}
expected[Time.SUNDAY] = 'SU';
expected[Time.MONDAY] = 'MO';
expected[Time.TUESDAY] = 'TU';
expected[Time.WEDNESDAY] = 'WE';
expected[Time.THURSDAY] = 'TH';
expected[Time.FRIDAY] = 'FR';
expected[Time.SATURDAY] = 'SA';

for (var map in expected) {
(function(map) {
test(map + ' to ' + expected[map], function() {
assert.equal(
ICAL.Recur.numericDayToIcalDay(map),
expected[map]
);
});
}(map));
}
});

});

0 comments on commit fde8a9e

Please sign in to comment.