@hebcal/core
Version:
A perpetual Jewish Calendar API
146 lines (143 loc) • 4.59 kB
JavaScript
/*! @hebcal/core v5.10.1, distributed under GPLv2 https://www.gnu.org/licenses/gpl-2.0.txt */
import { Event, flags } from './event.js';
import { molad, HDate, Locale } from '@hebcal/hdate';
import { reformatTimeStr } from './reformatTimeStr.js';
import './locale.js';
/* eslint-disable camelcase */
const shortDayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const heDayNames = [
'רִאשׁוֹן',
'שֵׁנִי',
'שְׁלִישִׁי',
'רְבִיעִי',
'חֲמִישִׁי',
'שִׁישִּׁי',
'שַׁבָּת',
];
const frDayNames = ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'];
const night = 'בַּלַּ֥יְלָה';
function getHebrewTimeOfDay(hour) {
if (hour < 5)
return night;
else if (hour < 12)
return 'בַּבֹּקֶר';
else if (hour < 17)
return 'בַּצׇּהֳרַיִים';
else if (hour < 21)
return 'בָּעֶרֶב';
return night;
}
/**
* Represents a molad, the moment when the new moon is "born"
*/
class Molad {
/**
* Calculates the molad for a Hebrew month
* @param year
* @param month
*/
constructor(year, month) {
this.m = molad(year, month);
}
/**
*/
getYear() {
return this.m.year;
}
/**
*/
getMonth() {
return this.m.month;
}
/**
*/
getMonthName() {
return HDate.getMonthName(this.m.month, this.m.year);
}
/**
* @returns Day of Week (0=Sunday, 6=Saturday)
*/
getDow() {
return this.m.dayOfWeek;
}
/**
* @returns hour of day (0-23)
*/
getHour() {
return this.m.hour;
}
/**
* @returns minutes past hour (0-59)
*/
getMinutes() {
return this.m.minutes;
}
/**
* @returns parts of a minute (0-17)
*/
getChalakim() {
return this.m.chalakim;
}
/**
* @param [locale] Optional locale name (defaults to active locale)
* @param options
*/
render(locale, options) {
var _a;
locale = locale !== null && locale !== void 0 ? locale : Locale.getLocaleName();
if (typeof locale === 'string') {
locale = locale.toLowerCase();
}
const isHebrewLocale = locale === 'he' || locale === 'he-x-nonikud' || locale === 'h';
const isFrenchLocale = locale === 'fr';
const monthName = Locale.gettext(this.getMonthName(), locale);
const dayNames = isHebrewLocale ? heDayNames : (isFrenchLocale ? frDayNames : shortDayNames);
const dow = dayNames[this.getDow()];
const minutes = this.getMinutes();
const hour = this.getHour();
const chalakim = this.getChalakim();
const moladStr = Locale.gettext('Molad', locale);
const minutesStr = (_a = Locale.lookupTranslation('min', locale)) !== null && _a !== void 0 ? _a : 'minutes';
const chalakimStr = Locale.gettext('chalakim', locale);
const and = Locale.gettext('and', locale);
const after = Locale.gettext('after', locale);
if (isHebrewLocale) {
const ampm = getHebrewTimeOfDay(hour);
const result = `${moladStr} ${monthName} יִהְיֶה בַּיּוֹם ${dow} בשָׁבוּעַ, ` +
`בְּשָׁעָה ${hour} ${ampm}, ` +
`ו-${minutes} ${minutesStr} ` +
`ו-${chalakim} ${chalakimStr}`;
if (locale === 'he-x-nonikud') {
return Locale.hebrewStripNikkud(result);
}
return result;
}
const fmtTime = reformatTimeStr(`${hour}:00`, 'pm', options);
const month = monthName.replace(/'/g, '’');
return `${moladStr} ${month}: ${dow}, ${minutes} ${minutesStr} ${and} ${chalakim} ${chalakimStr} ${after} ${fmtTime}`;
}
}
/** Represents a Molad announcement on Shabbat Mevarchim */
class MoladEvent extends Event {
/**
* @param date Hebrew date event occurs
* @param hyear molad year
* @param hmonth molad month
* @param options
*/
constructor(date, hyear, hmonth, options) {
const m = new Molad(hyear, hmonth);
const monthName = m.getMonthName();
super(date, `Molad ${monthName} ${hyear}`, flags.MOLAD);
this.molad = m;
this.options = options;
}
/**
* @param [locale] Optional locale name (defaults to active locale).
*/
render(locale) {
return this.molad.render(locale, this.options);
}
}
export { Molad, MoladEvent };
//# sourceMappingURL=molad.js.map