Skip to content

Commit

Permalink
Add getLeyningForParsha() to look up regular leyning for a weekly par…
Browse files Browse the repository at this point in the history
…sha with no special readings
  • Loading branch information
mjradwin committed Jul 18, 2021
1 parent a8aafad commit b616ec9
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 65 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ CAUTION: Modifies the <code>aliyot</code> parameter instead of making a copy.</p
<dd><p>Looks up leyning for a given holiday. Returns some
of full kriyah aliyot, special Maftir, special Haftarah</p>
</dd>
<dt><a href="#makeLeyningSummary">makeLeyningSummary(aliyot)</a> ⇒ <code>string</code></dt>
<dd></dd>
<dt><a href="#getLeyningForHolidayKey">getLeyningForHolidayKey(key)</a> ⇒ <code><a href="#Leyning">Leyning</a></code></dt>
<dd><p>Looks up leyning for a given holiday key. Key should be an
(untranslated) string used in holiday-readings.json. Returns some
Expand All @@ -75,12 +77,18 @@ except for Nitzavim-Vayelech</p>
<dt><a href="#getHolidayEvents">getHolidayEvents(hd, il)</a> ⇒ <code>Array.&lt;Event&gt;</code></dt>
<dd><p>Filters out Rosh Chodesh and events that don&#39;t occur in this location</p>
</dd>
<dt><a href="#getLeyningForParsha">getLeyningForParsha(parsha)</a> ⇒ <code><a href="#Leyning">Leyning</a></code></dt>
<dd><p>Looks up leyning for a regular Shabbat parsha.</p>
</dd>
<dt><a href="#getLeyningForParshaHaShavua">getLeyningForParshaHaShavua(e, [il])</a> ⇒ <code><a href="#Leyning">Leyning</a></code></dt>
<dd><p>Looks up leyning for a regular Shabbat parsha.</p>
</dd>
<dt><a href="#formatAliyahWithBook">formatAliyahWithBook(a)</a> ⇒ <code>string</code></dt>
<dd><p>Formats an aliyah object like &quot;Numbers 28:9 - 28:15&quot;</p>
</dd>
<dt><a href="#formatAliyahShort">formatAliyahShort(aliyah, showBook)</a> ⇒ <code>string</code></dt>
<dd><p>Formats an aliyah object like &quot;Numbers 28:9-15&quot;</p>
</dd>
<dt><a href="#getDoubledName">getDoubledName(id)</a> ⇒ <code>string</code></dt>
<dd><p>takes a 0-based (Bereshit=0) parsha ID</p>
</dd>
Expand Down Expand Up @@ -268,6 +276,15 @@ of full kriyah aliyot, special Maftir, special Haftarah
| e | <code>Event</code> | | the Hebcal event associated with this leyning |
| [il] | <code>boolean</code> | <code>false</code> | true if Israel holiday scheme |

<a name="makeLeyningSummary"></a>

## makeLeyningSummary(aliyot) ⇒ <code>string</code>
**Kind**: global function

| Param | Type |
| --- | --- |
| aliyot | <code>Object.&lt;string, Aliyah&gt;</code> |

<a name="getLeyningForHolidayKey"></a>

## getLeyningForHolidayKey(key) ⇒ [<code>Leyning</code>](#Leyning)
Expand Down Expand Up @@ -327,6 +344,18 @@ Filters out Rosh Chodesh and events that don't occur in this location
| hd | <code>HDate</code> | Hebrew date |
| il | <code>boolean</code> | in Israel |

<a name="getLeyningForParsha"></a>

## getLeyningForParsha(parsha) ⇒ [<code>Leyning</code>](#Leyning)
Looks up leyning for a regular Shabbat parsha.

**Kind**: global function
**Returns**: [<code>Leyning</code>](#Leyning) - map of aliyot

| Param | Type | Description |
| --- | --- | --- |
| parsha | <code>string</code> \| <code>Array.&lt;string&gt;</code> | untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei'] |

<a name="getLeyningForParshaHaShavua"></a>

## getLeyningForParshaHaShavua(e, [il]) ⇒ [<code>Leyning</code>](#Leyning)
Expand All @@ -351,6 +380,18 @@ Formats an aliyah object like "Numbers 28:9 - 28:15"
| --- | --- | --- |
| a | [<code>Aliyah</code>](#Aliyah) | aliyah |

<a name="formatAliyahShort"></a>

## formatAliyahShort(aliyah, showBook) ⇒ <code>string</code>
Formats an aliyah object like "Numbers 28:9-15"

**Kind**: global function

| Param | Type |
| --- | --- |
| aliyah | [<code>Aliyah</code>](#Aliyah) |
| showBook | <code>boolean</code> |

<a name="getDoubledName"></a>

## getDoubledName(id) ⇒ <code>string</code>
Expand Down
15 changes: 13 additions & 2 deletions leyning.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,22 @@ declare module '@hebcal/leyning' {
*/
export function getLeyningForParshaHaShavua(e: Event, il?: boolean): Leyning;

/**
* Looks up regular leyning for a weekly parsha with no special readings
* @param parsha untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei']
* @returns map of aliyot
*/
export function getLeyningForParsha(parsha: string|string[]): Leyning;

/**
* Formats an aliyah object like "Numbers 28:9 - 28:15"
* @param a - aliyah
*/
export function formatAliyahWithBook(a: Aliyah): string;
export function formatAliyahWithBook(aliyah: Aliyah): string;

/**
* Formats an aliyah object like "Numbers 28:9-15"
*/
export function formatAliyahShort(aliyah: Aliyah, showBook: boolean): string;

/**
* Triennial Torah readings
Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/leyning",
"version": "4.7.2",
"version": "4.8.0",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"keywords": [
"hebcal",
Expand Down Expand Up @@ -66,10 +66,10 @@
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^19.0.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.1",
"@rollup/plugin-node-resolve": "^13.0.2",
"ava": "^3.15.0",
"core-js": "^3.15.2",
"eslint": "^7.30.0",
"eslint": "^7.31.0",
"eslint-config-google": "^0.14.0",
"jsdoc": "^3.6.7",
"jsdoc-to-markdown": "^7.0.1",
Expand Down
3 changes: 3 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import numverses from './numverses.json';

/**
* Names of the books of the Torah. BOOK[1] === 'Genesis'
*/
export const BOOK = ['', 'Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy'];

/**
Expand Down
8 changes: 1 addition & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
export {
getLeyningForHoliday,
getLeyningForHolidayKey,
getLeyningForParshaHaShavua,
getLeyningKeyForEvent,
formatAliyahWithBook,
} from './leyning';
export * from './leyning';
export {getTriennialForParshaHaShavua, getTriennial, Triennial} from './triennial';
export {writeFullKriyahCsv, writeTriennialCsv} from './csv';
export {addSefariaLinksToLeyning} from './common';
Expand Down
104 changes: 68 additions & 36 deletions src/leyning.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export function getLeyningForHolidayKey(key) {

/**
* Formats parsha as a string
* @private
* @param {string[]} parsha
* @return {string}
*/
Expand Down Expand Up @@ -339,42 +340,29 @@ function getSpecialHaftara(ev) {
}

/**
* Looks up leyning for a regular Shabbat parsha.
* @param {Event} e the Hebcal event associated with this leyning
* @param {boolean} [il] in Israel
* Looks up regular leyning for a weekly parsha with no special readings
* @param {string|string[]} parsha untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei']
* @return {Leyning} map of aliyot
*/
export function getLeyningForParshaHaShavua(e, il=false) {
if (!e instanceof Event) {
throw new TypeError(`Bad event argument: ${e}`);
} else if (e.getFlags() != flags.PARSHA_HASHAVUA) {
throw new TypeError(`Event must be parsha hashavua: ${e.getDesc()}`);
export function getLeyningForParsha(parsha) {
const isParshaString = typeof parsha === 'string';
if (!isParshaString &&
(!Array.isArray(parsha) || (parsha.length !== 1 && parsha.length !== 2))) {
throw new TypeError(`Bad parsha argument: ${parsha}`);
}
// first, collect the default aliyot and haftara
const parsha = e.parsha;
const name = parshaToString(parsha); // untranslated
const name = isParshaString ? parsha : parshaToString(parsha);
const raw = parshiyotObj[name];
let haftara = parshiyotObj[getHaftaraKey(parsha)].haftara;
const fullkriyah = Object.create(null);
const book = BOOK[raw.book];
Object.keys(raw.fullkriyah).forEach((num) => {
const src = raw.fullkriyah[num];
const reading = {k: book, b: src.b, e: src.e};
fullkriyah[num] = reading;
});
const reason = Object.create(null);
const hd = e.getDate();
// Now, check for special maftir or haftara on same date
const specialHaftara = specialReadings(hd, il, fullkriyah, reason);
if (specialHaftara) {
haftara = specialHaftara;
}
const specialHaftara2 = getSpecialHaftara(e);
if (specialHaftara2) {
haftara = specialHaftara2.haftara;
reason.haftara = specialHaftara2.reason;
}
Object.values(fullkriyah).map((aliyah) => calculateNumVerses(aliyah));
const parshaNameArray = isParshaString ? raw.combined ? [raw.p1, raw.p2] : [parsha] : parsha;
const hkey = getHaftaraKey(parshaNameArray);
const haftara = parshiyotObj[hkey].haftara;
const result = {
summary: makeLeyningSummary(fullkriyah),
fullkriyah: fullkriyah,
Expand All @@ -393,6 +381,46 @@ export function getLeyningForParshaHaShavua(e, il=false) {
result.sephardicNumV = numv;
}
}
return result;
}

/**
* Looks up leyning for a regular Shabbat parsha.
* @param {Event} ev the Hebcal event associated with this leyning
* @param {boolean} [il] in Israel
* @return {Leyning} map of aliyot
*/
export function getLeyningForParshaHaShavua(ev, il=false) {
if (!ev instanceof Event) {
throw new TypeError(`Bad event argument: ${ev}`);
} else if (ev.getFlags() != flags.PARSHA_HASHAVUA) {
throw new TypeError(`Event must be parsha hashavua: ${ev.getDesc()}`);
}
// first, collect the default aliyot and haftara
const parsha = ev.parsha;
const result = getLeyningForParsha(parsha);
const reason = Object.create(null);
const hd = ev.getDate();
// Now, check for special maftir or haftara on same date
const origHaftara = result.haftara;
const specialHaftara = specialReadings(hd, il, result.fullkriyah, reason);
if (specialHaftara) {
result.haftara = specialHaftara;
}
if (reason['7'] || reason['M']) {
result.summary = makeLeyningSummary(result.fullkriyah);
}
const specialHaftara2 = getSpecialHaftara(ev);
if (specialHaftara2) {
result.haftara = specialHaftara2.haftara;
reason.haftara = specialHaftara2.reason;
}
if (origHaftara !== result.haftara) {
const numv = calculateHaftarahNumVerses(result.haftara);
if (numv) {
result.haftaraNumV = numv;
}
}
if (Object.keys(reason).length) {
result.reason = reason;
}
Expand All @@ -405,19 +433,23 @@ export function getLeyningForParshaHaShavua(e, il=false) {
* @return {number}
*/
function calculateHaftarahNumVerses(haftara) {
const matches = haftara.match(/^([^\d]+)\s+(\d.+)$/);
if (matches !== null) {
const hbook = matches[1].trim();
const hverses = matches[2].replace(/;.+$/, '').trim();
const cv = hverses.match(/^(\d+:\d+)\s*-\s*(\d+:\d+)$/);
if (cv) {
const haft = {k: hbook, b: cv[1], e: cv[2]};
return calculateNumVerses(haft);
} else {
throw new RangeError(hverses);
const sections = haftara.split(/[;,]/);
let total = 0;
sections.forEach((haft) => {
const matches = haft.match(/^([^\d]+)\s+(\d.+)$/);
if (matches !== null) {
const hbook = matches[1].trim();
const hverses = matches[2].trim();
const cv = hverses.match(/^(\d+:\d+)\s*-\s*(\d+:\d+)$/);
if (cv) {
const haft = {k: hbook, b: cv[1], e: cv[2]};
total += calculateNumVerses(haft);
} else {
total++; // Something like "Jeremiah 3:4" is 1 verse
}
}
}
return undefined;
});
return total || undefined;
}

/**
Expand Down
Loading

0 comments on commit b616ec9

Please sign in to comment.