@hebcal/core
Version:
A perpetual Jewish Calendar API
192 lines (189 loc) • 6.11 kB
JavaScript
/*! @hebcal/core v6.0.8, 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.greg().getFullYear();
if (year < 100 || year > 2999) {
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.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 empty 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 empty locale)
*/
renderBrief(locale) {
const str = super.renderBrief(locale);
return str.replace(/'/g, '’');
}
}
/**
* 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.greg());
return isoDate.replace(/-/g, '');
}
}
const chanukahEmoji = '🕎';
const KEYCAP_DIGITS = [
'0️⃣',
'1️⃣',
'2️⃣',
'3️⃣',
'4️⃣',
'5️⃣',
'6️⃣',
'7️⃣',
'8️⃣',
'9️⃣',
];
/**
* Because Chanukah sometimes starts in December and ends in January,
* we subclass HolidayEvent to generate the correct URL.
*/
class ChanukahEvent extends HolidayEvent {
/**
* @param chanukahDay should be undefined for 1st night of Chanukah
*/
constructor(date, desc, mask, chanukahDay) {
super(date, desc, mask);
this.chanukahDay = chanukahDay;
this.emoji = chanukahEmoji;
if (chanukahDay !== 8) {
const candles = chanukahDay ? chanukahDay + 1 : 1;
this.emoji += KEYCAP_DIGITS[candles];
}
}
urlDateSuffix() {
const dt = this.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 empty 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 empty 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