@hebcal/core
Version:
A perpetual Jewish Calendar API
170 lines (169 loc) • 4.76 kB
JavaScript
/* eslint-disable camelcase */
import { Event, flags } from './event';
import { HDate, Locale, molad, pad2 } from '@hebcal/hdate';
import { reformatTimeStr } from './reformatTimeStr';
import './locale'; // Adds Hebrew and Ashkenazic translations
const enDoW = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
const heDayNames = [
'רִאשׁוֹן',
'שֵׁנִי',
'שְׁלִישִׁי',
'רְבִיעִי',
'חֲמִישִׁי',
'שִׁישִּׁי',
'שַׁבָּת',
];
const frDoW = [
'Dimanche',
'Lundi',
'Mardi',
'Mercredi',
'Jeudi',
'Vendredi',
'Samedi',
];
const night = 'בַּלַּ֥יְלָה';
function getDayNames(locale) {
if (locale === 'he' || locale === 'he-x-nonikud' || locale === 'h') {
return heDayNames;
}
else if (locale === 'fr') {
return frDoW;
}
return enDoW;
}
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"
*/
export 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 empty locale)
* @param options
*/
render(locale, options) {
var _a;
locale = locale !== null && locale !== void 0 ? locale : 'en';
if (typeof locale === 'string') {
locale = locale.toLowerCase();
}
const isHebrewLocale = locale === 'he' || locale === 'he-x-nonikud' || locale === 'h';
const monthName = Locale.gettext(this.getMonthName(), locale);
const dayNames = getDayNames(locale);
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);
if (isHebrewLocale) {
const ampm = getHebrewTimeOfDay(hour);
let result = `${moladStr} ${monthName} יִהְיֶה בַּיּוֹם ${dow} בשָׁבוּעַ, ` +
`בְּשָׁעָה ${hour} ${ampm}, ` +
`ו-${minutes} ${minutesStr}`;
if (chalakim !== 0) {
result += ` ו-${chalakim} ${chalakimStr}`;
}
if (locale === 'he-x-nonikud') {
return Locale.hebrewStripNikkud(result);
}
return result;
}
const fmtTime = reformatTimeStr(`${hour}:${pad2(minutes)}`, 'pm', options);
const month = monthName.replace(/'/g, '’');
const result = `${moladStr} ${month}: ${dow}, ${fmtTime}`;
if (chalakim === 0) {
return result;
}
return result + ` ${and} ${chalakim} ${chalakimStr}`;
}
}
/** Represents a Molad announcement on Shabbat Mevarchim */
export 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 empty locale)
*/
render(locale) {
return this.molad.render(locale, this.options);
}
}