UNPKG

@hebcal/leyning

Version:

Torah Reading API for Parashat HaShavua and holidays

120 lines (117 loc) 4.35 kB
/*! @hebcal/leyning v9.2.5 */ import { flags } from '@hebcal/core/dist/esm/event'; import './locale.js'; import { calculateNumVerses, NUM_VERSES } from './common.js'; import { clone, cloneHaftara, sumVerses } from './clone.js'; import { lookupFestival } from './festival.js'; import { HOLIDAY_IGNORE_MASK, getLeyningKeyForEvent } from './getLeyningKeyForEvent.js'; import { makeLeyningParts, makeSummaryFromParts } from './summary.js'; import { Locale } from '@hebcal/core/dist/esm/locale'; /** * Looks up leyning for a given holiday key. Key should be an * (untranslated) string used in holiday-readings.json. Returns some * of full kriyah aliyot, special Maftir, special Haftarah * @param key name from `holiday-readings.json` to find */ function getLeyningForHolidayKey(key, cholHaMoedDay, il) { if (typeof key !== 'string') { return undefined; } const src = lookupFestival(key); if (typeof src === 'undefined') { return undefined; } const israelOnly = src.il; if (typeof israelOnly === 'boolean' && typeof il === 'boolean' && il !== israelOnly) { return undefined; } const leyning = { name: { en: key, he: Locale.lookupTranslation(key, 'he'), }, type: 'holiday', }; if (src.fullkriyah) { leyning.fullkriyah = clone(src.fullkriyah); if (key === 'Sukkot Shabbat Chol ha-Moed' && cholHaMoedDay) { leyning.fullkriyah['M'] = leyning.fullkriyah[`M-day${cholHaMoedDay}`]; for (let day = 1; day <= 5; day++) { delete leyning.fullkriyah[`M-day${day}`]; } } if (typeof leyning.fullkriyah['1'] === 'object') { const parts = makeLeyningParts(leyning.fullkriyah); leyning.summary = makeSummaryFromParts(parts); leyning.summaryParts = parts; } Object.values(leyning.fullkriyah).forEach(aliyah => calculateNumVerses(aliyah)); if (src.alt) { leyning.alt = clone(src.alt); for (const aliyah of Object.values(leyning.alt)) { calculateNumVerses(aliyah); } } } if (src.haft) { const haft = (leyning.haft = cloneHaftara(src.haft)); leyning.haftara = makeSummaryFromParts(haft); leyning.haftaraNumV = sumVerses(haft); } if (src.seph) { const seph = (leyning.seph = cloneHaftara(src.seph)); leyning.sephardic = makeSummaryFromParts(seph); leyning.sephardicNumV = sumVerses(seph); } let megillah = src.megillah; if (il && key === 'Pesach I (on Shabbat)') megillah = 'Song of Songs'; if (megillah) { const chaps = NUM_VERSES[megillah]; const m = {}; for (let i = 1; i < chaps.length; i++) { const numv = chaps[i]; m[`${i}`] = { k: megillah, b: `${i}:1`, e: `${i}:${numv}`, v: numv }; } leyning.megillah = m; const parts = makeLeyningParts(m); if (leyning.summaryParts) { leyning.summaryParts.push(...parts); } const megillahSummary = makeSummaryFromParts(parts); leyning.summary = leyning.summary ? leyning.summary + '; ' + megillahSummary : megillahSummary; } if (src.note) { leyning.note = src.note; } return leyning; } /** * Looks up leyning for a given holiday. Returns some * of full kriyah aliyot, special Maftir, special Haftarah * @param ev the Hebcal event associated with this leyning * @param [il] true if Israel holiday scheme * @returns map of aliyot */ function getLeyningForHoliday(ev, il = false) { if (typeof ev !== 'object' || typeof ev.getFlags !== 'function') { throw new TypeError(`Bad event argument: ${JSON.stringify(ev)}`); } else if (typeof ev.eventTime !== 'undefined') { return undefined; } else if (ev.getFlags() & flags.PARSHA_HASHAVUA) { throw new TypeError(`Event should be a holiday: ${ev.getDesc()}`); } else if (ev.getFlags() & HOLIDAY_IGNORE_MASK) { return undefined; } const key = getLeyningKeyForEvent(ev, il); const leyning = getLeyningForHolidayKey(key, ev.cholHaMoedDay, il); return leyning; } export { getLeyningForHoliday, getLeyningForHolidayKey };