Skip to content

Commit

Permalink
Make getMonth() return strong string types
Browse files Browse the repository at this point in the history
I didn't change setMonth(), because that accepts more variants
  • Loading branch information
SLaks authored and mjradwin committed Nov 8, 2024
1 parent 6e20f9e commit d032c2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/hdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {
MonthName,
SimpleHebrewDate,
abs2hebrew,
daysInMonth,
Expand Down Expand Up @@ -324,7 +325,7 @@ export class HDate {
* const hd = new HDate(new Date(2008, 10, 13)); // 15 Cheshvan 5769
* hd.getMonthName(); // 'Cheshvan'
*/
getMonthName(): string {
getMonthName(): MonthName {
return getMonthName(this.getMonth(), this.getFullYear());
}

Expand Down Expand Up @@ -610,7 +611,7 @@ export class HDate {
* import {HDate, months} from '@hebcal/hdate';
* HDate.getMonthName(months.CHESHVAN, 5769); // 'Cheshvan'
*/
static getMonthName(month: number, year: number): string {
static getMonthName(month: number, year: number): MonthName {
return getMonthName(month, year);
}

Expand Down
13 changes: 8 additions & 5 deletions src/hdateBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const months = {
ADAR_II,
} as const;

const monthNames0: string[] = [
const monthNames0 = [
'',
'Nisan',
'Iyyar',
Expand All @@ -70,11 +70,14 @@ const monthNames0: string[] = [
* Regular years are index 0 and leap years are index 1.
* @private
*/
const monthNames: string[][] = [
monthNames0.concat(['Adar', 'Nisan']),
monthNames0.concat(['Adar I', 'Adar II', 'Nisan']),
const monthNames = [
[...monthNames0, 'Adar', 'Nisan'],
[...monthNames0, 'Adar I', 'Adar II', 'Nisan'],
] as const;

/** Transliterated Hebrew month names. */
export type MonthName = (typeof monthNames)[number][number];

const edCache: Map<number, number> = new Map<number, number>();

const EPOCH = -1373428;
Expand Down Expand Up @@ -227,7 +230,7 @@ export function daysInMonth(month: number, year: number): number {
* @param month Hebrew month (e.g. months.TISHREI)
* @param year Hebrew year
*/
export function getMonthName(month: number, year: number): string {
export function getMonthName(month: number, year: number): MonthName {
assertNumber(month, 'month');
assertNumber(year, 'year');
if (month < 1 || month > 14) {
Expand Down

0 comments on commit d032c2b

Please sign in to comment.