Skip to content

Commit

Permalink
Fix several Typescript lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed May 1, 2024
1 parent 0c532bf commit 1ed1bcc
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 117 deletions.
78 changes: 58 additions & 20 deletions src/anniversary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {hebrew2abs, abs2hebrew, isLeapYear, months, monthsInYear,
shortKislev, longCheshvan, SimpleHebrewDate} from './hdate';
import {
hebrew2abs,
abs2hebrew,
isLeapYear,
months,
monthsInYear,
shortKislev,
longCheshvan,
SimpleHebrewDate,
} from './hdate';
import {greg} from './greg';

const NISAN = months.NISAN;
Expand All @@ -11,21 +19,26 @@ const ADAR_I = months.ADAR_I;
const ADAR_II = months.ADAR_II;

/**
* Returns true if the object is a Javascript Date
* Returns true if the object is a SimpleHebrewDate
* @private
* @param {Object} obj
*/
function isSimpleHebrewDate(obj: any): boolean {
return typeof obj === 'object' && obj !== null &&
return (
typeof obj === 'object' &&
obj !== null &&
typeof obj.yy === 'number' &&
typeof obj.mm === 'number' &&
typeof obj.dd === 'number';
typeof obj.dd === 'number'
);
}

/**
* @private
*/
function toSimpleHebrewDate(obj: Date | SimpleHebrewDate | number): SimpleHebrewDate {
function toSimpleHebrewDate(
obj: Date | SimpleHebrewDate | number
): SimpleHebrewDate {
if (isSimpleHebrewDate(obj)) {
return obj as SimpleHebrewDate;
} else if (typeof obj === 'number') {
Expand Down Expand Up @@ -71,33 +84,47 @@ function toSimpleHebrewDate(obj: Date | SimpleHebrewDate | number): SimpleHebrew
* @param {Date | SimpleHebrewDate | number} date Gregorian or Hebrew date of death
* @return {Date} anniversary occurring in `hyear`
*/
export function getYahrzeit(hyear: number, date: Date | SimpleHebrewDate | number): Date | undefined {
export function getYahrzeit(
hyear: number,
date: Date | SimpleHebrewDate | number
): Date | undefined {
const hd = getYahrzeitHD(hyear, date);
if (typeof hd === 'undefined') {
return hd;
}
return greg.abs2greg(hebrew2abs(hd.yy, hd.mm, hd.dd));
}

export function getYahrzeitHD(hyear: number, date: Date | SimpleHebrewDate | number): SimpleHebrewDate | undefined {
export function getYahrzeitHD(
hyear: number,
date: Date | SimpleHebrewDate | number
): SimpleHebrewDate | undefined {
let hDeath = toSimpleHebrewDate(date);
if (hyear <= hDeath.yy) {
// Hebrew year ${hyear} occurs on or before original date in ${hDeath.yy}
return undefined;
}

if (hDeath.mm == CHESHVAN && hDeath.dd == 30 && !longCheshvan(hDeath.yy + 1)) {
if (
hDeath.mm === CHESHVAN &&
hDeath.dd === 30 &&
!longCheshvan(hDeath.yy + 1)
) {
// If it's Heshvan 30 it depends on the first anniversary;
// if that was not Heshvan 30, use the day before Kislev 1.
hDeath = abs2hebrew(hebrew2abs(hyear, KISLEV, 1) - 1);
} else if (hDeath.mm == KISLEV && hDeath.dd == 30 && shortKislev(hDeath.yy + 1)) {
} else if (
hDeath.mm === KISLEV &&
hDeath.dd === 30 &&
shortKislev(hDeath.yy + 1)
) {
// If it's Kislev 30 it depends on the first anniversary;
// if that was not Kislev 30, use the day before Teveth 1.
hDeath = abs2hebrew(hebrew2abs(hyear, TEVET, 1) - 1);
} else if (hDeath.mm == ADAR_II) {
} else if (hDeath.mm === ADAR_II) {
// If it's Adar II, use the same day in last month of year (Adar or Adar II).
hDeath.mm = monthsInYear(hyear);
} else if (hDeath.mm == ADAR_I && hDeath.dd == 30 && !isLeapYear(hyear)) {
} else if (hDeath.mm === ADAR_I && hDeath.dd === 30 && !isLeapYear(hyear)) {
// If it's the 30th in Adar I and year is not a leap year
// (so Adar has only 29 days), use the last day in Shevat.
hDeath.dd = 30;
Expand All @@ -106,10 +133,10 @@ export function getYahrzeitHD(hyear: number, date: Date | SimpleHebrewDate | num
// In all other cases, use the normal anniversary of the date of death.

// advance day to rosh chodesh if needed
if (hDeath.mm == CHESHVAN && hDeath.dd == 30 && !longCheshvan(hyear)) {
if (hDeath.mm === CHESHVAN && hDeath.dd === 30 && !longCheshvan(hyear)) {
hDeath.mm = KISLEV;
hDeath.dd = 1;
} else if (hDeath.mm == KISLEV && hDeath.dd == 30 && shortKislev(hyear)) {
} else if (hDeath.mm === KISLEV && hDeath.dd === 30 && shortKislev(hyear)) {
hDeath.mm = TEVET;
hDeath.dd = 1;
}
Expand Down Expand Up @@ -143,15 +170,21 @@ export function getYahrzeitHD(hyear: number, date: Date | SimpleHebrewDate | num
* @param {Date | SimpleHebrewDate | number} date Gregorian or Hebrew date of event
* @return {Date} anniversary occurring in `hyear`
*/
export function getBirthdayOrAnniversary(hyear: number, date: Date | SimpleHebrewDate): Date | undefined {
export function getBirthdayOrAnniversary(
hyear: number,
date: Date | SimpleHebrewDate
): Date | undefined {
const hd = getBirthdayHD(hyear, date);
if (typeof hd === 'undefined') {
return hd;
}
return greg.abs2greg(hebrew2abs(hd.yy, hd.mm, hd.dd));
}

export function getBirthdayHD(hyear: number, date: Date | SimpleHebrewDate): SimpleHebrewDate | undefined {
export function getBirthdayHD(
hyear: number,
date: Date | SimpleHebrewDate
): SimpleHebrewDate | undefined {
const orig = toSimpleHebrewDate(date);
const origYear = orig.yy;
if (hyear === origYear) {
Expand All @@ -164,15 +197,20 @@ export function getBirthdayHD(hyear: number, date: Date | SimpleHebrewDate): Sim
let month = orig.mm;
let day = orig.dd;

if ((month == ADAR_I && !isOrigLeap) || (month == ADAR_II && isOrigLeap)) {
if ((month === ADAR_I && !isOrigLeap) || (month === ADAR_II && isOrigLeap)) {
month = monthsInYear(hyear);
} else if (month == CHESHVAN && day == 30 && !longCheshvan(hyear)) {
} else if (month === CHESHVAN && day === 30 && !longCheshvan(hyear)) {
month = KISLEV;
day = 1;
} else if (month == KISLEV && day == 30 && shortKislev(hyear)) {
} else if (month === KISLEV && day === 30 && shortKislev(hyear)) {
month = TEVET;
day = 1;
} else if (month == ADAR_I && day == 30 && isOrigLeap && !isLeapYear(hyear)) {
} else if (
month === ADAR_I &&
day === 30 &&
isOrigLeap &&
!isLeapYear(hyear)
) {
month = NISAN;
day = 1;
}
Expand Down
52 changes: 26 additions & 26 deletions src/gematriya.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
const GERESH: string = '׳';
const GERSHAYIM: string = '״';
const GERESH = '׳';
const GERSHAYIM = '״';

const alefbet = {
'א': 1,
'ב': 2,
'ג': 3,
'ד': 4,
'ה': 5,
'ו': 6,
'ז': 7,
'ח': 8,
'ט': 9,
'י': 10,
'כ': 20,
'ל': 30,
'מ': 40,
'נ': 50,
'ס': 60,
'ע': 70,
'פ': 80,
'צ': 90,
'ק': 100,
'ר': 200,
'ש': 300,
'ת': 400,
א: 1,
ב: 2,
ג: 3,
ד: 4,
ה: 5,
ו: 6,
ז: 7,
ח: 8,
ט: 9,
י: 10,
כ: 20,
ל: 30,
מ: 40,
נ: 50,
ס: 60,
ע: 70,
פ: 80,
צ: 90,
ק: 100,
ר: 200,
ש: 300,
ת: 400,
};
const heb2num = new Map<string, number>();
const num2heb = new Map<number, string>();
Expand Down Expand Up @@ -83,7 +83,7 @@ export function gematriya(num: number | string): string {
str += GERESH;
}
const digits = num2digits(num1 % 1000);
if (digits.length == 1) {
if (digits.length === 1) {
return str + num2heb.get(digits[0]) + GERESH;
}
for (let i = 0; i < digits.length; i++) {
Expand All @@ -106,7 +106,7 @@ export function gematriya(num: number | string): string {
* @return {number}
*/
export function gematriyaStrToNum(str: string): number {
let num: number = 0;
let num = 0;
const gereshIdx: number = str.indexOf(GERESH);
if (gereshIdx !== -1 && gereshIdx !== str.length - 1) {
const thousands = str.substring(0, gereshIdx);
Expand Down
67 changes: 36 additions & 31 deletions src/greg.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable @typescript-eslint/no-namespace, no-inner-declarations */
/** @private */
const lengths: number[] = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
/** @private */
const monthLengths: number[][] = [
lengths,
lengths.slice(),
];
const monthLengths: number[][] = [lengths, lengths.slice()];
monthLengths[1][2] = 29;

/**
Expand All @@ -21,6 +19,22 @@ function quotient(x: number, y: number): number {
return Math.floor(x / y);
}

/**
* @private
* @param abs - R.D. number of days
*/
function yearFromFixed(abs: number): number {
const l0: number = abs - 1;
const n400: number = quotient(l0, 146097);
const d1: number = mod(l0, 146097);
const n100: number = quotient(d1, 36524);
const d2: number = mod(d1, 36524);
const n4: number = quotient(d2, 1461);
const d3: number = mod(d2, 1461);
const n1: number = quotient(d3, 365);
const year: number = 400 * n400 + 100 * n100 + 4 * n4 + n1;
return n100 !== 4 && n1 !== 4 ? year + 1 : year;
}
/*
const ABS_14SEP1752 = 639797;
const ABS_2SEP1752 = 639785;
Expand Down Expand Up @@ -49,32 +63,16 @@ export namespace greg {
// 1 based months
return monthLengths[+isLeapYear(year)][month];
}

/**
* Returns true if the object is a Javascript Date
* @param {Object} obj
* @return {boolean}
*/
export function isDate(obj: any): boolean {
// eslint-disable-next-line no-prototype-builtins
return typeof obj === 'object' && Date.prototype.isPrototypeOf(obj);
}

/**
* @private
* @param abs - R.D. number of days
*/
function yearFromFixed(abs: number): number {
const l0: number = abs - 1;
const n400: number = quotient(l0, 146097);
const d1: number = mod(l0, 146097);
const n100: number = quotient(d1, 36524);
const d2: number = mod(d1, 36524);
const n4: number = quotient(d2, 1461);
const d3: number = mod(d2, 1461);
const n1: number = quotient(d3, 365);
const year: number = 400 * n400 + 100 * n100 + 4 * n4 + n1;
return n100 != 4 && n1 != 4 ? year + 1 : year;
}

/**
* @private
Expand All @@ -84,13 +82,15 @@ export namespace greg {
*/
function toFixed(year: number, month: number, day: number): number {
const py: number = year - 1;
return 365 * py +
return (
365 * py +
quotient(py, 4) -
quotient(py, 100) +
quotient(py, 400) +
quotient((367 * month - 362), 12) +
(month <= 2 ? 0 : (isLeapYear(year) ? -1 : -2)) +
day;
quotient(367 * month - 362, 12) +
(month <= 2 ? 0 : isLeapYear(year) ? -1 : -2) +
day
);
}

/**
Expand All @@ -102,15 +102,19 @@ export namespace greg {
if (!isDate(date)) {
throw new TypeError(`Argument not a Date: ${date}`);
}
const abs = toFixed(date.getFullYear(), date.getMonth() + 1, date.getDate());
const abs = toFixed(
date.getFullYear(),
date.getMonth() + 1,
date.getDate()
);
/*
if (abs < ABS_14SEP1752 && abs > ABS_2SEP1752) {
throw new RangeError(`Invalid Date: ${date}`);
}
*/
return abs;
}

/**
* Converts from Rata Die (R.D. number) to Gregorian date.
* See the footnote on page 384 of ``Calendrical Calculations, Part II:
Expand All @@ -132,13 +136,14 @@ export namespace greg {
*/
const year: number = yearFromFixed(abs);
const priorDays: number = abs - toFixed(year, 1, 1);
const correction: number = abs < toFixed(year, 3, 1) ? 0 : (isLeapYear(year) ? 1 : 2);
const month: number = quotient((12 * (priorDays + correction) + 373), 367);
const correction: number =
abs < toFixed(year, 3, 1) ? 0 : isLeapYear(year) ? 1 : 2;
const month: number = quotient(12 * (priorDays + correction) + 373, 367);
const day: number = abs - toFixed(year, month, 1) + 1;
const dt: Date = new Date(year, month - 1, day);
if (year < 100 && year >= 0) {
dt.setFullYear(year);
}
return dt;
}
};
}

0 comments on commit 1ed1bcc

Please sign in to comment.