Skip to content

Commit

Permalink
Add search function to getLeyningOnDate() to make weekday Torah readi…
Browse files Browse the repository at this point in the history
…ng work correctly when Shabbat is a chag
  • Loading branch information
mjradwin committed Aug 16, 2022
1 parent d005b07 commit 02ba493
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 19 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ of full kriyah aliyot, special Maftir, special Haftarah</p>
<dd><p>Looks up regular leyning for a weekly parsha with no special readings</p>
</dd>
<dt><a href="#getLeyningForParshaHaShavua">getLeyningForParshaHaShavua(ev, [il])</a> ⇒ <code><a href="#Leyning">Leyning</a></code></dt>
<dd><p>Looks up leyning for a regular Shabbat parsha.</p>
<dd><p>Looks up leyning for a regular Shabbat parsha, including any special
maftir or Haftara.</p>
</dd>
<dt><a href="#getLeyningOnDate">getLeyningOnDate(hdate, il)</a> ⇒ <code><a href="#Leyning">Leyning</a></code></dt>
<dd><p>Looks up leyning for a regular Shabbat or holiday</p>
<dd><p>Looks up leyning for a regular Shabbat, Monday/Thursday weekday or holiday.</p>
<p>If <code>hdate</code> coincides with a holiday that has Torah reading, returns the
reading for that day (see <a href="#getLeyningForHoliday">getLeyningForHoliday</a>)</p>
<p>Otherwise, if <code>hdate</code> is a Saturday, returns <a href="#getLeyningForParshaHaShavua">getLeyningForParshaHaShavua</a></p>
<p>Otherwise, if <code>hdate</code> is a Monday or Thursday, returns <a href="#Leyning">Leyning</a> for the
Parashat haShavua, containing only the <code>weekday</code> aliyot (no <code>fullkriyah</code>).</p>
<p>Otherwise, returns <code>undefined</code>.</p>
</dd>
<dt><a href="#getTriennial">getTriennial(year)</a> ⇒ <code><a href="#Triennial">Triennial</a></code></dt>
<dd><p>Calculates the 3-year readings for a given year</p>
Expand Down Expand Up @@ -320,7 +327,8 @@ Looks up regular leyning for a weekly parsha with no special readings
<a name="getLeyningForParshaHaShavua"></a>

## getLeyningForParshaHaShavua(ev, [il]) ⇒ [<code>Leyning</code>](#Leyning)
Looks up leyning for a regular Shabbat parsha.
Looks up leyning for a regular Shabbat parsha, including any special
maftir or Haftara.

**Kind**: global function
**Returns**: [<code>Leyning</code>](#Leyning) - map of aliyot
Expand All @@ -333,7 +341,17 @@ Looks up leyning for a regular Shabbat parsha.
<a name="getLeyningOnDate"></a>

## getLeyningOnDate(hdate, il) ⇒ [<code>Leyning</code>](#Leyning)
Looks up leyning for a regular Shabbat or holiday
Looks up leyning for a regular Shabbat, Monday/Thursday weekday or holiday.

If `hdate` coincides with a holiday that has Torah reading, returns the
reading for that day (see [getLeyningForHoliday](#getLeyningForHoliday))

Otherwise, if `hdate` is a Saturday, returns [getLeyningForParshaHaShavua](#getLeyningForParshaHaShavua)

Otherwise, if `hdate` is a Monday or Thursday, returns [Leyning](#Leyning) for the
Parashat haShavua, containing only the `weekday` aliyot (no `fullkriyah`).

Otherwise, returns `undefined`.

**Kind**: global function
**Returns**: [<code>Leyning</code>](#Leyning) - map of aliyot
Expand Down
27 changes: 19 additions & 8 deletions leyning.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ declare module '@hebcal/leyning' {
};

/**
* Looks up leyning for a regular Shabbat or holiday
* Looks up leyning for a regular Shabbat, Monday/Thursday weekday or holiday.
*
* If `hdate` coincides with a holiday that has Torah reading, returns the
* reading for that day (see {@link getLeyningForHoliday})
*
* Otherwise, if `hdate` is a Saturday, returns {@link getLeyningForParshaHaShavua}
*
* Otherwise, if `hdate` is a Monday or Thursday, returns {@link Leyning} for the
* Parashat haShavua, containing only the `weekday` aliyot (no `fullkriyah`).
*
* Otherwise, returns `undefined`.
* @param hdate Hebrew Date
* @param il in Israel
* @returns map of aliyot
Expand Down Expand Up @@ -93,20 +103,20 @@ declare module '@hebcal/leyning' {

/**
* Based on the event date, type and title, finds the relevant leyning key
* @param e - event
* @param ev - the Hebcal holiday event
* @param [il] - true if Israel holiday scheme
* @returns key to look up in holiday-reading.json
*/
export function getLeyningKeyForEvent(e: Event, il?: boolean): string;
export function getLeyningKeyForEvent(ev: Event, il?: boolean): string;

/**
* Looks up leyning for a given holiday. Returns some
* of full kriyah aliyot, special Maftir, special Haftarah
* @param e - the Hebcal event associated with this leyning
* @param ev - the Hebcal event associated with this leyning
* @param [il] - true if Israel holiday scheme
* @returns map of aliyot
*/
export function getLeyningForHoliday(e: Event, il?: boolean): Leyning;
export function getLeyningForHoliday(ev: Event, il?: boolean): Leyning;

/**
* Looks up leyning for a given holiday key. Key should be an
Expand All @@ -118,12 +128,13 @@ declare module '@hebcal/leyning' {
export function getLeyningForHolidayKey(key: string): Leyning;

/**
* Looks up leyning for a regular Shabbat parsha.
* @param e - the Hebcal event associated with this leyning
* Looks up leyning for a regular Shabbat parsha, including any special
* maftir or Haftara.
* @param ev - the Hebcal event associated with this leyning
* @param [il] - in Israel
* @returns map of aliyot
*/
export function getLeyningForParshaHaShavua(e: Event, il?: boolean): Leyning;
export function getLeyningForParshaHaShavua(ev: Event, il?: boolean): Leyning;

/**
* Looks up regular leyning for a weekly parsha with no special readings
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/leyning",
"version": "4.18.1",
"version": "4.18.2",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal",
Expand Down
54 changes: 54 additions & 0 deletions src/getLeyningOnDate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,57 @@ test('getLeyningOnDate-weekday2', (t) => {
};
t.deepEqual(reading, expected);
});

test('getLeyningOnDate-weekday-search1', (t) => {
const hd = new HDate(28, 'Elul', 5783);
const reading = getLeyningOnDate(hd, false);
const expected = {
name: {en: 'Ha\'Azinu', he: 'הַאֲזִינוּ'},
parsha: ['Ha\'Azinu'],
parshaNum: 53,
weekday: {
'1': {k: 'Deuteronomy', b: '32:1', e: '32:3', v: 3},
'2': {k: 'Deuteronomy', b: '32:4', e: '32:6', v: 3},
'3': {k: 'Deuteronomy', b: '32:7', e: '32:12', v: 6},
},
};
t.deepEqual(reading, expected);
});

test('getLeyningOnDate-weekday-erev-sukkot', (t) => {
const hd = new HDate(14, 'Tishrei', 5786);
const reading = getLeyningOnDate(hd, false);
const expected = {
name: {en: 'Bereshit', he: 'בְּרֵאשִׁית'},
parsha: ['Bereshit'],
parshaNum: 1,
weekday: {
'1': {k: 'Genesis', b: '1:1', e: '1:5', v: 5},
'2': {k: 'Genesis', b: '1:6', e: '1:8', v: 3},
'3': {k: 'Genesis', b: '1:9', e: '1:13', v: 5},
},
};
t.deepEqual(reading, expected);
});

test('getLeyningOnDate-weekday-search2', (t) => {
const hd = new HDate(13, 'Tishrei', 5787);
const reading = getLeyningOnDate(hd, true);
const expected = {
name: {en: 'Bereshit', he: 'בְּרֵאשִׁית'},
parsha: ['Bereshit'],
parshaNum: 1,
weekday: {
'1': {k: 'Genesis', b: '1:1', e: '1:5', v: 5},
'2': {k: 'Genesis', b: '1:6', e: '1:8', v: 3},
'3': {k: 'Genesis', b: '1:9', e: '1:13', v: 5},
},
};
t.deepEqual(reading, expected);
});

test('getLeyningOnDate-weekday-undefined', (t) => {
const hd = new HDate(12, 'Tishrei', 5787);
const reading = getLeyningOnDate(hd, true);
t.is(reading, undefined);
});
63 changes: 57 additions & 6 deletions src/leyning.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HebrewCalendar, months, flags, Event, ParshaEvent, Locale} from '@hebcal/core';
import {HDate, HebrewCalendar, months, flags, Event, ParshaEvent, Locale} from '@hebcal/core';
import {BOOK, calculateNumVerses, clone, cloneHaftara,
parshaToString,
makeHaftaraSummary, makeSummaryFromParts, calculateHaftaraNumV} from './common';
Expand Down Expand Up @@ -270,7 +270,8 @@ export function getLeyningForParsha(parsha) {
}

/**
* Looks up leyning for a regular Shabbat parsha.
* Looks up leyning for a regular Shabbat parsha, including any special
* maftir or Haftara.
* @param {Event} ev the Hebcal event associated with this leyning
* @param {boolean} [il] in Israel
* @return {Leyning} map of aliyot
Expand Down Expand Up @@ -327,7 +328,18 @@ export function getLeyningForParshaHaShavua(ev, il=false) {
}

/**
* Looks up leyning for a regular Shabbat or holiday
* Looks up leyning for a regular Shabbat, Monday/Thursday weekday or holiday.
*
* If `hdate` coincides with a holiday that has Torah reading, returns the
* reading for that day (see {@link getLeyningForHoliday})
*
* Otherwise, if `hdate` is a Saturday, returns {@link getLeyningForParshaHaShavua}
*
* Otherwise, if `hdate` is a Monday or Thursday, returns {@link Leyning} for the
* Parashat haShavua, containing only the `weekday` aliyot (no `fullkriyah`).
*
* Otherwise, returns `undefined`.
*
* @param {HDate} hdate Hebrew Date
* @param {boolean} il in Israel
* @return {Leyning} map of aliyot
Expand All @@ -342,8 +354,19 @@ export function getLeyningOnDate(hdate, il) {
return getLeyningForParshaHaShavua(parshaEvent, il);
}
const events = HebrewCalendar.getHolidaysOnDate(hdate, il);
if (!events && (dow === 1 || dow === 4)) {
const reading = getLeyningForParsha(parsha.parsha);
if (events) {
const reading = getLeyningForHoliday(events[0], il);
if (reading) {
return reading;
} else if (dow !== 1 && dow !== 4) {
return undefined; // today is a non-reading holiday
}
// otherwise continue below for Mon/Thu
}
if (dow === 1 || dow === 4) {
const saturday = hdate.onOrAfter(6);
const parsha2 = findParshaHaShavua(saturday);
const reading = getLeyningForParsha(parsha2.parsha);
const result = {
name: reading.name,
parsha: reading.parsha,
Expand All @@ -352,5 +375,33 @@ export function getLeyningOnDate(hdate, il) {
};
return result;
}
return events ? getLeyningForHoliday(events[0], il) : undefined;
// no reading today: it's not Shabbat, Mon/Thu, or a Torah-reading holiday
return undefined;
}

/**
* @private
* @param {HDate} saturday
* @param {boolean} il
* @return {Leyning}
*/
function findParshaHaShavua(saturday, il) {
const hyear = saturday.getFullYear();
const sedra = HebrewCalendar.getSedra(hyear, il);
const parsha = sedra.lookup(saturday);
if (!parsha.chag) {
return parsha;
}
// Search for next regular parsha, which could even spill into next year
const endOfYear = new HDate(1, months.TISHREI, hyear + 1).abs() - 1;
const endAbs = endOfYear + 30;
for (let sat2 = saturday.abs() + 7; sat2 <= endAbs; sat2 += 7) {
const sedra2 = sat2 > endOfYear ? HebrewCalendar.getSedra(hyear + 1, il) : sedra;
const parsha2 = sedra2.lookup(sat2);
if (!parsha2.chag) {
return parsha2;
}
}
/* NOTREACHED */
return null;
}

0 comments on commit 02ba493

Please sign in to comment.