UNPKG

@hebcal/core

Version:

A perpetual Jewish Calendar API

177 lines (174 loc) 5.8 kB
/*! @hebcal/core v5.10.1, distributed under GPLv2 https://www.gnu.org/licenses/gpl-2.0.txt */ import { isoDateString, Locale } from '@hebcal/hdate'; import { Event, flags } from './event.js'; import { holidayDesc } from './staticHolidays.js'; import './locale.js'; /** Represents a built-in holiday like Pesach, Purim or Tu BiShvat */ class HolidayEvent extends Event { basename() { return this.getDesc() .replace(/ \d{4}$/, '') .replace(/ \(CH''M\)$/, '') .replace(/ \(observed\)$/, '') .replace(/ \(Hoshana Raba\)$/, '') .replace(/ [IV]+$/, '') .replace(/: \d Candles?$/, '') .replace(/: 8th Day$/, '') .replace(/^Erev /, ''); } url() { const year = this.getDate().greg().getFullYear(); if (year < 100) { return undefined; } const url = 'https://www.hebcal.com/holidays/' + this.basename().toLowerCase().replace(/'/g, '').replace(/ /g, '-') + '-' + this.urlDateSuffix(); return this.getFlags() & flags.IL_ONLY ? url + '?i=on' : url; } urlDateSuffix() { const year = this.getDate().greg().getFullYear(); return String(year); } getEmoji() { if (this.emoji) { return this.emoji; } else if (this.getFlags() & flags.SPECIAL_SHABBAT) { return '🕍'; } else { return '✡️'; } } getCategories() { if (this.cholHaMoedDay) { return ['holiday', 'major', 'cholhamoed']; } const cats = super.getCategories(); if (cats[0] !== 'unknown') { return cats; } // Don't depend on flags.MINOR_HOLIDAY always being set. Look for minor holidays. const desc = this.getDesc(); switch (desc) { case holidayDesc.LAG_BAOMER: case holidayDesc.LEIL_SELICHOT: case holidayDesc.PESACH_SHENI: case holidayDesc.EREV_PURIM: case holidayDesc.PURIM_KATAN: case holidayDesc.SHUSHAN_PURIM: case holidayDesc.TU_BAV: case holidayDesc.TU_BISHVAT: case holidayDesc.ROSH_HASHANA_LABEHEMOT: return ['holiday', 'minor']; } return ['holiday', 'major']; } /** * Returns (translated) description of this event * @param [locale] Optional locale name (defaults to active locale). */ render(locale) { const str = super.render(locale); return str.replace(/'/g, '’'); } /** * Returns a brief (translated) description of this event. * For most events, this is the same as render(). For some events, it procudes * a shorter text (e.g. without a time or added description). * @param [locale] Optional locale name (defaults to active locale). */ renderBrief(locale) { const str = super.renderBrief(locale); return str.replace(/'/g, '’'); } /** * Makes a clone of this Event object * @deprecated */ clone() { const ev = new HolidayEvent(this.date, this.desc, this.mask); // overwrite all enumerable properties Object.assign(ev, this); return ev; } } /** * Because Asara B'Tevet often occurs twice in the same Gregorian year, * we subclass HolidayEvent to generate the correct URL. */ class AsaraBTevetEvent extends HolidayEvent { urlDateSuffix() { const isoDate = isoDateString(this.getDate().greg()); return isoDate.replace(/-/g, ''); } } /** * Because Chanukah sometimes starts in December and ends in January, * we subclass HolidayEvent to generate the correct URL. */ class ChanukahEvent extends HolidayEvent { urlDateSuffix() { const dt = this.getDate().greg(); let year = dt.getFullYear(); if (dt.getMonth() === 0) { year--; } return String(year); } } /** Represents Rosh Hashana, the Jewish New Year */ class RoshHashanaEvent extends HolidayEvent { /** * @private * @param date Hebrew date event occurs * @param hyear Hebrew year * @param mask optional holiday flags */ constructor(date, hyear, mask) { super(date, `Rosh Hashana ${hyear}`, mask); this.hyear = hyear; } /** * Returns (translated) description of this event * @param [locale] Optional locale name (defaults to active locale). */ render(locale) { return Locale.gettext('Rosh Hashana', locale) + ' ' + this.hyear; } getEmoji() { return '🍏🍯'; } } const roshChodeshStr = 'Rosh Chodesh'; /** Represents Rosh Chodesh, the beginning of a new month */ class RoshChodeshEvent extends HolidayEvent { /** * Constructs Rosh Chodesh event * @param date Hebrew date event occurs * @param monthName Hebrew month name (not translated) */ constructor(date, monthName) { super(date, `${roshChodeshStr} ${monthName}`, flags.ROSH_CHODESH); } /** * Returns (translated) description of this event * @param [locale] Optional locale name (defaults to active locale). */ render(locale) { const monthName = this.getDesc().substring(roshChodeshStr.length + 1); const monthName0 = Locale.gettext(monthName, locale); const monthName1 = monthName0.replace(/'/g, '’'); return Locale.gettext(roshChodeshStr, locale) + ' ' + monthName1; } basename() { return this.getDesc(); } getEmoji() { return this.emoji || '🌒'; } } export { AsaraBTevetEvent, ChanukahEvent, HolidayEvent, RoshChodeshEvent, RoshHashanaEvent }; //# sourceMappingURL=HolidayEvent.js.map