Skip to content

Commit

Permalink
Improve getEventCategories() to handle flag-masks better
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Sep 19, 2022
1 parent 383723a commit 5c53990
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 117 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/rest-api",
"version": "4.3.0",
"version": "4.3.1",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal"
Expand Down
45 changes: 25 additions & 20 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ export function toISOString(d) {
return pad4(d.getFullYear()) + '-' + pad2(d.getMonth() + 1) + '-' + pad2(d.getDate());
}

const flagToCategory = [
[flags.MAJOR_FAST, 'holiday', 'major', 'fast'],
[flags.CHANUKAH_CANDLES, 'holiday', 'major'],
[flags.DAF_YOMI, 'dafyomi'],
[flags.HEBREW_DATE, 'hebdate'],
[flags.MINOR_FAST, 'holiday', 'fast'],
[flags.MINOR_HOLIDAY, 'holiday', 'minor'],
[flags.MISHNA_YOMI, 'mishnayomi'],
[flags.MODERN_HOLIDAY, 'holiday', 'modern'],
[flags.MOLAD, 'molad'],
[flags.OMER_COUNT, 'omer'],
[flags.PARSHA_HASHAVUA, 'parashat'], // backwards-compat
[flags.ROSH_CHODESH, 'roshchodesh'],
[flags.SHABBAT_MEVARCHIM, 'mevarchim'],
[flags.SPECIAL_SHABBAT, 'holiday', 'shabbat'],
[flags.USER_EVENT, 'user'],
];

/**
* Returns a category and subcategory name
* @param {Event} ev
Expand All @@ -129,27 +147,16 @@ export function getEventCategories(ev) {
if (desc === 'Fast begins' || desc === 'Fast ends') {
return ['zmanim', 'fast'];
}
switch (ev.getFlags()) {
case flags.OMER_COUNT: return ['omer'];
case flags.HEBREW_DATE: return ['hebdate'];
case flags.PARSHA_HASHAVUA: return ['parashat']; // backwards-compat
case flags.DAF_YOMI: return ['dafyomi'];
case flags.ROSH_CHODESH: return ['roshchodesh'];
case flags.SPECIAL_SHABBAT: return ['holiday', 'shabbat'];
case flags.MINOR_FAST: return ['holiday', 'fast'];
case flags.MAJOR_FAST: return ['holiday', 'fast', 'major'];
case flags.MODERN_HOLIDAY: return ['holiday', 'modern'];
case flags.SHABBAT_MEVARCHIM: return ['mevarchim'];
case flags.MOLAD: return ['molad'];
case flags.USER_EVENT: return ['user'];
case flags.MINOR_HOLIDAY: return ['holiday', 'minor'];
case flags.MISHNA_YOMI: return ['mishnayomi'];
default:
break; // fall through to string-based category
}
if (ev.cholHaMoedDay) {
return ['holiday', 'major', 'cholhamoed'];
}
const mask = ev.getFlags();
for (let i = 0; i < flagToCategory.length; i++) {
const attrs = flagToCategory[i];
if (mask & attrs[0]) {
return attrs.slice(1);
}
}
switch (desc) {
case 'Havdalah':
return ['havdalah'];
Expand All @@ -165,8 +172,6 @@ export function getEventCategories(ev) {
case 'Tu BiShvat':
case 'Rosh Hashana LaBehemot':
return ['holiday', 'minor'];
case 'Erev Tish\'a B\'Av':
return ['holiday', 'fast', 'major'];
default:
return ['holiday', 'major'];
}
Expand Down
185 changes: 89 additions & 96 deletions src/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,102 +106,95 @@ test('makeTorahMemoText-userEvent', (t) => {
});

test('getEventCategories', (t) => {
const events = HebrewCalendar.calendar({year: 2022});
const actual = events.map((ev) => {
return {
h: ev.getDesc(),
c: getEventCategories(ev),
};
});
const expected = [
{h: 'Rosh Chodesh Sh\'vat', c: ['roshchodesh']},
{h: 'Shabbat Shirah', c: ['holiday', 'shabbat']},
{h: 'Tu BiShvat', c: ['holiday', 'minor']},
{h: 'Rosh Chodesh Adar I', c: ['roshchodesh']},
{h: 'Rosh Chodesh Adar I', c: ['roshchodesh']},
{h: 'Purim Katan', c: ['holiday', 'minor']},
{h: 'Shabbat Shekalim', c: ['holiday', 'shabbat']},
{h: 'Rosh Chodesh Adar II', c: ['roshchodesh']},
{h: 'Rosh Chodesh Adar II', c: ['roshchodesh']},
{h: 'Shabbat Zachor', c: ['holiday', 'shabbat']},
{h: 'Ta\'anit Esther', c: ['holiday', 'fast']},
{h: 'Erev Purim', c: ['holiday', 'minor']},
{h: 'Purim', c: ['holiday', 'minor']},
{h: 'Shushan Purim', c: ['holiday', 'minor']},
{h: 'Shabbat Parah', c: ['holiday', 'shabbat']},
{h: 'Shabbat HaChodesh', c: ['holiday', 'shabbat']},
{h: 'Rosh Chodesh Nisan', c: ['roshchodesh']},
{h: 'Shabbat HaGadol', c: ['holiday', 'shabbat']},
{h: 'Yom HaAliyah', c: ['holiday', 'modern']},
{h: 'Ta\'anit Bechorot', c: ['holiday', 'fast']},
{h: 'Erev Pesach', c: ['holiday', 'major']},
{h: 'Pesach I', c: ['holiday', 'major']},
{h: 'Pesach II', c: ['holiday', 'major']},
{h: 'Pesach III (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Pesach IV (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Pesach V (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Pesach VI (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Pesach VII', c: ['holiday', 'major']},
{h: 'Pesach VIII', c: ['holiday', 'major']},
{h: 'Yom HaShoah', c: ['holiday', 'modern']},
{h: 'Rosh Chodesh Iyyar', c: ['roshchodesh']},
{h: 'Rosh Chodesh Iyyar', c: ['roshchodesh']},
{h: 'Yom HaZikaron', c: ['holiday', 'modern']},
{h: 'Yom HaAtzma\'ut', c: ['holiday', 'modern']},
{h: 'Pesach Sheni', c: ['holiday', 'minor']},
{h: 'Lag BaOmer', c: ['holiday', 'minor']},
{h: 'Yom Yerushalayim', c: ['holiday', 'modern']},
{h: 'Rosh Chodesh Sivan', c: ['roshchodesh']},
{h: 'Erev Shavuot', c: ['holiday', 'major']},
{h: 'Shavuot I', c: ['holiday', 'major']},
{h: 'Shavuot II', c: ['holiday', 'major']},
{h: 'Rosh Chodesh Tamuz', c: ['roshchodesh']},
{h: 'Rosh Chodesh Tamuz', c: ['roshchodesh']},
{h: 'Tzom Tammuz', c: ['holiday', 'fast']},
{h: 'Rosh Chodesh Av', c: ['roshchodesh']},
{h: 'Shabbat Chazon', c: ['holiday', 'shabbat']},
{h: 'Erev Tish\'a B\'Av', c: ['holiday', 'fast', 'major']},
{h: 'Tish\'a B\'Av (observed)', c: ['holiday', 'fast', 'major']},
{h: 'Tu B\'Av', c: ['holiday', 'minor']},
{h: 'Shabbat Nachamu', c: ['holiday', 'shabbat']},
{h: 'Rosh Chodesh Elul', c: ['roshchodesh']},
{h: 'Rosh Hashana LaBehemot', c: ['holiday', 'minor']},
{h: 'Rosh Chodesh Elul', c: ['roshchodesh']},
{h: 'Leil Selichot', c: ['holiday', 'minor']},
{h: 'Erev Rosh Hashana', c: ['holiday', 'major']},
{h: 'Rosh Hashana 5783', c: ['holiday', 'major']},
{h: 'Rosh Hashana II', c: ['holiday', 'major']},
{h: 'Tzom Gedaliah', c: ['holiday', 'fast']},
{h: 'Shabbat Shuva', c: ['holiday', 'shabbat']},
{h: 'Erev Yom Kippur', c: ['holiday', 'major']},
{h: 'Yom Kippur', c: ['holiday', 'major']},
{h: 'Erev Sukkot', c: ['holiday', 'major']},
{h: 'Sukkot I', c: ['holiday', 'major']},
{h: 'Sukkot II', c: ['holiday', 'major']},
{h: 'Sukkot III (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Sukkot IV (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Sukkot V (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Sukkot VI (CH\'\'M)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Sukkot VII (Hoshana Raba)', c: ['holiday', 'major', 'cholhamoed']},
{h: 'Shmini Atzeret', c: ['holiday', 'major']},
{h: 'Simchat Torah', c: ['holiday', 'major']},
{h: 'Rosh Chodesh Cheshvan', c: ['roshchodesh']},
{h: 'Rosh Chodesh Cheshvan', c: ['roshchodesh']},
{h: 'Sigd', c: ['holiday', 'modern']},
{h: 'Rosh Chodesh Kislev', c: ['roshchodesh']},
{h: 'Rosh Chodesh Kislev', c: ['roshchodesh']},
{h: 'Chanukah: 1 Candle', c: ['holiday', 'major']},
{h: 'Chanukah: 2 Candles', c: ['holiday', 'major']},
{h: 'Chanukah: 3 Candles', c: ['holiday', 'major']},
{h: 'Chanukah: 4 Candles', c: ['holiday', 'major']},
{h: 'Chanukah: 5 Candles', c: ['holiday', 'major']},
{h: 'Chanukah: 6 Candles', c: ['holiday', 'major']},
{h: 'Chanukah: 7 Candles', c: ['holiday', 'major']},
{h: 'Rosh Chodesh Tevet', c: ['roshchodesh']},
{h: 'Chanukah: 8 Candles', c: ['holiday', 'major']},
{h: 'Rosh Chodesh Tevet', c: ['roshchodesh']},
{h: 'Chanukah: 8th Day', c: ['holiday', 'minor']},
];
const events = HebrewCalendar.calendar({year: 2022, il: true});
const actual = {};
for (const ev of events) {
actual[ev.getDesc()] = getEventCategories(ev);
}
const expected = {
'Rosh Chodesh Sh\'vat': ['roshchodesh'],
'Shabbat Shirah': ['holiday', 'shabbat'],
'Tu BiShvat': ['holiday', 'minor'],
'Family Day': ['holiday', 'modern'],
'Rosh Chodesh Adar I': ['roshchodesh'],
'Purim Katan': ['holiday', 'minor'],
'Shabbat Shekalim': ['holiday', 'shabbat'],
'Rosh Chodesh Adar II': ['roshchodesh'],
'Shabbat Zachor': ['holiday', 'shabbat'],
'Ta\'anit Esther': ['holiday', 'fast'],
'Erev Purim': ['holiday', 'minor'],
'Purim': ['holiday', 'minor'],
'Shushan Purim': ['holiday', 'minor'],
'Shabbat Parah': ['holiday', 'shabbat'],
'Shabbat HaChodesh': ['holiday', 'shabbat'],
'Rosh Chodesh Nisan': ['roshchodesh'],
'Shabbat HaGadol': ['holiday', 'shabbat'],
'Yom HaAliyah': ['holiday', 'modern'],
'Ta\'anit Bechorot': ['holiday', 'fast'],
'Erev Pesach': ['holiday', 'major'],
'Pesach I': ['holiday', 'major'],
'Pesach II (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Pesach III (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Pesach IV (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Pesach V (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Pesach VI (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Pesach VII': ['holiday', 'major'],
'Yom HaShoah': ['holiday', 'modern'],
'Rosh Chodesh Iyyar': ['roshchodesh'],
'Yom HaZikaron': ['holiday', 'modern'],
'Yom HaAtzma\'ut': ['holiday', 'modern'],
'Herzl Day': ['holiday', 'modern'],
'Pesach Sheni': ['holiday', 'minor'],
'Lag BaOmer': ['holiday', 'minor'],
'Yom Yerushalayim': ['holiday', 'modern'],
'Rosh Chodesh Sivan': ['roshchodesh'],
'Erev Shavuot': ['holiday', 'major'],
'Shavuot': ['holiday', 'major'],
'Rosh Chodesh Tamuz': ['roshchodesh'],
'Tzom Tammuz': ['holiday', 'fast'],
'Jabotinsky Day': ['holiday', 'modern'],
'Rosh Chodesh Av': ['roshchodesh'],
'Shabbat Chazon': ['holiday', 'shabbat'],
'Erev Tish\'a B\'Av': ['holiday', 'major', 'fast'],
'Tish\'a B\'Av (observed)': ['holiday', 'major', 'fast'],
'Tu B\'Av': ['holiday', 'minor'],
'Shabbat Nachamu': ['holiday', 'shabbat'],
'Rosh Chodesh Elul': ['roshchodesh'],
'Rosh Hashana LaBehemot': ['holiday', 'minor'],
'Leil Selichot': ['holiday', 'minor'],
'Erev Rosh Hashana': ['holiday', 'major'],
'Rosh Hashana 5783': ['holiday', 'major'],
'Rosh Hashana II': ['holiday', 'major'],
'Tzom Gedaliah': ['holiday', 'fast'],
'Shabbat Shuva': ['holiday', 'shabbat'],
'Erev Yom Kippur': ['holiday', 'major'],
'Yom Kippur': ['holiday', 'major', 'fast'],
'Erev Sukkot': ['holiday', 'major'],
'Sukkot I': ['holiday', 'major'],
'Sukkot II (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Sukkot III (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Sukkot IV (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Sukkot V (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Sukkot VI (CH\'\'M)': ['holiday', 'major', 'cholhamoed'],
'Sukkot VII (Hoshana Raba)': ['holiday', 'major', 'cholhamoed'],
'Shmini Atzeret': ['holiday', 'major'],
'Rosh Chodesh Cheshvan': ['roshchodesh'],
'Yom HaAliyah School Observance': ['holiday', 'modern'],
'Yitzhak Rabin Memorial Day': ['holiday', 'modern'],
'Sigd': ['holiday', 'modern'],
'Rosh Chodesh Kislev': ['roshchodesh'],
'Ben-Gurion Day': ['holiday', 'modern'],
'Chanukah: 1 Candle': ['holiday', 'major'],
'Chanukah: 2 Candles': ['holiday', 'major'],
'Chanukah: 3 Candles': ['holiday', 'major'],
'Chanukah: 4 Candles': ['holiday', 'major'],
'Chanukah: 5 Candles': ['holiday', 'major'],
'Chanukah: 6 Candles': ['holiday', 'major'],
'Chanukah: 7 Candles': ['holiday', 'major'],
'Rosh Chodesh Tevet': ['roshchodesh'],
'Chanukah: 8 Candles': ['holiday', 'major'],
'Chanukah: 8th Day': ['holiday', 'minor'],
};
t.deepEqual(actual, expected);
});

Expand Down

0 comments on commit 5c53990

Please sign in to comment.