UNPKG

@hebcal/leyning

Version:

Torah Reading API for Parashat HaShavua and holidays

245 lines (242 loc) 8.6 kB
/*! @hebcal/leyning v9.5.4 */ import { flags } from '@hebcal/core/dist/esm/event'; import './locale.js'; import parshiyotObj0 from './aliyot.json.js'; import { BOOK, calculateNumVerses, parshaToString } from './common.js'; import { translateLeyning, translateAliyahOrArray } from './translate.js'; import { makeSummaryFromParts, makeLeyningParts } from './summary.js'; import { cloneHaftara, sumVerses } from './clone.js'; import { specialReadings2 } from './specialReadings.js'; import { Locale } from '@hebcal/core/dist/esm/locale'; const parshiyotObj = parshiyotObj0; /** * on doubled parshiot, read only the second Haftarah * except for Nitzavim-Vayelech and Achrei Mot-Kedoshim * @private */ function getHaftaraKey(parsha) { const first = parsha[0]; if (parsha.length === 2 && first === 'Achrei Mot') { return parshaToString(parsha); // 'Achrei Mot-Kedoshim' } if (parsha.length === 1 || first === 'Nitzavim') { return first; } else { return parsha[1]; } } /** * Transliterated English and Hebrew names of this parsha * @param parsha untranslated name like ['Pinchas'] or ['Matot','Masei'] */ function makeLeyningNames(parsha) { const name = parshaToString(parsha); return { en: name, he: parsha.map(s => Locale.lookupTranslation(s, 'he')).join('־'), }; } /** * Looks up regular leyning for a weekly parsha with no special readings * @private * @param parsha untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei'] * @returns map of aliyot */ function getLeyningForParshaShabbatOnly(parsha, language = 'en') { const raw = lookupParsha(parsha); const fullkriyah = {}; const book = BOOK[raw.book]; for (const [num, src] of Object.entries(raw.fullkriyah)) { const aliyah = { k: book, b: src[0], e: src[1] }; if (src.length === 3) { aliyah.reason = src[2]; } calculateNumVerses(aliyah); fullkriyah[num] = aliyah; } const name = parshaToString(parsha); const parshaNameArray = raw.combined ? [raw.p1, raw.p2] : [name]; const parts = makeLeyningParts(fullkriyah); const summary = makeSummaryFromParts(parts); const result = { name: makeLeyningNames(parshaNameArray), type: 'shabbat', parsha: parshaNameArray, parshaNum: raw.num, summary, fullkriyah: fullkriyah, haftara: '', haft: [], }; if (parts.length > 1) { result.summaryParts = parts; } const hkey = getHaftaraKey(parshaNameArray); const haft0 = parshiyotObj[hkey].haft; if (haft0) { const haft = (result.haft = cloneHaftara(haft0)); result.haftara = makeSummaryFromParts(haft); result.haftaraNumV = sumVerses(haft); } const seph0 = parshiyotObj[hkey].seph; if (seph0) { const seph = (result.seph = cloneHaftara(seph0)); result.sephardic = makeSummaryFromParts(seph); result.sephardicNumV = sumVerses(seph); } const chabad = parshiyotObj[hkey].chabad; if (chabad) { if ('sameas' in chabad) { result.chabad = result.haft; } else { result.chabad = cloneHaftara(chabad); } } return translateLeyning(result, language); } /** * Looks up Monday/Thursday aliyot for a regular parsha * @param parsha untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei'] */ function getWeekdayReading(parsha, language = 'en') { const raw = lookupParsha(parsha); const parshaMeta = raw.combined ? lookupParsha(raw.p1) : raw; const aliyot = parshaMeta.weekday; if (!aliyot) { throw new Error(`Parsha missing weekday: ${parsha}`); } const book = BOOK[raw.book]; const weekday = {}; for (let i = 1; i <= 3; i++) { const num = '' + i; const src = aliyot[num]; const aliyah = { k: book, b: src[0], e: src[1] }; calculateNumVerses(aliyah); weekday[num] = aliyah; } return translateAliyahOrArray(weekday, language); } /** * Looks up regular leyning for a weekly parsha with no special readings * @param parsha untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei'] */ function getLeyningForParsha(parsha, language = 'en') { const result = getLeyningForParshaShabbatOnly(parsha, language); result.weekday = getWeekdayReading(parsha); return result; } const reasonHaftKey = { haftara: 'haft', sephardic: 'seph', chabad: 'chabad', }; /** * 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 */ function getLeyningForParshaHaShavua(ev, il = false, language = 'en') { if (typeof ev !== 'object' || typeof ev.getFlags !== 'function') { 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.p.parsha; const result = getLeyningForParshaShabbatOnly(parsha); const hd = ev.getDate(); // Now, check for special maftir or haftara on same date const special = specialReadings2(parsha, hd, il, result.fullkriyah); const reason = special.reason; if (special.haft) { delete result.chabad; delete result.seph; delete result.sephardic; delete result.sephardicNumV; const haft = (result.haft = cloneHaftara(special.haft)); result.haftara = makeSummaryFromParts(haft); result.haftaraNumV = sumVerses(haft); if (special.seph) { const seph = (result.seph = cloneHaftara(special.seph)); result.sephardic = makeSummaryFromParts(seph); result.sephardicNumV = sumVerses(seph); } if (special.chabad) { result.chabad = cloneHaftara(special.chabad); } } if (reason['7'] || reason['M']) { result.fullkriyah = special.aliyot; const parts = makeLeyningParts(result.fullkriyah); result.summary = makeSummaryFromParts(parts); result.summaryParts = parts; } const reasons = Object.keys(reason); if (reasons.length !== 0) { // Translate reason strings to target language if not English const translatedReason = {}; for (const [key, val] of Object.entries(reason)) { translatedReason[key] = Locale.gettext(val, language); } result.reason = translatedReason; for (const num of reasons) { if (reasonHaftKey[num]) { const haftKey = reasonHaftKey[num]; const haftObj = result[haftKey]; const hafts = Array.isArray(haftObj) ? haftObj : [haftObj]; for (const haft of hafts) { haft.reason = translatedReason[num]; } } else { const aliyah = result.fullkriyah[num]; if (typeof aliyah === 'object') { aliyah.reason = translatedReason[num]; } } } } return translateLeyning(result, language); } /** * Returns the parsha metadata * @param parsha untranslated name like 'Pinchas' or ['Pinchas'] or ['Matot','Masei'] */ function lookupParsha(parsha, language = 'en') { const name = parshaToString(parsha); const raw = parshiyotObj[name]; if (typeof raw !== 'object') { throw new TypeError(`Bad parsha argument: ${parsha}`); } if (raw.combined) { const [p1, p2] = name.split('-'); if (!raw.hebrew) { raw.hebrew = Locale.gettext(p1, 'he') + '־' + Locale.gettext(p2, 'he'); } if (!raw.haft) { const haftKey = p1 === 'Nitzavim' ? p1 : p2; raw.haft = lookupParsha(haftKey, language).haft; } } else { raw.hebrew = Locale.gettext(name, 'he'); } raw.haft = translateAliyahOrArray(raw.haft, language); if (raw.seph) { raw.seph = translateAliyahOrArray(raw.seph, language); } if (raw.chabad && 'sameas' in raw.chabad) { raw.chabad = raw.haft; } if (raw.chabad) { raw.chabad = translateAliyahOrArray(raw.chabad, language); } return raw; } export { getLeyningForParsha, getLeyningForParshaHaShavua, getWeekdayReading, lookupParsha, makeLeyningNames }; //# sourceMappingURL=leyning.js.map