Skip to content

Commit

Permalink
Rework implementation of fix for holidays with dashes in name for sed…
Browse files Browse the repository at this point in the history
…ra find
  • Loading branch information
mjradwin committed Nov 3, 2024
1 parent 95f5e89 commit 000374c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/sedra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,19 @@ export class Sedra {
if (parsha >= parshiot.length || (parsha < 0 && !isValidDouble(parsha))) {
throw new RangeError(`Invalid parsha number: ${parsha}`);
}
const idx = this.theSedraArray.indexOf(parsha);
if (idx === -1) {
return null; // doesn't occur this year
}
return new HDate(this.firstSaturday + idx * 7);
return this.findInternal(parsha);
} else if (typeof parsha === 'string') {
const num = parsha2id.get(parsha);
if (typeof num === 'number') {
return this.find(num);
} else if (parsha.indexOf('-') !== -1) {
if (parsha === CHMPESACH || parsha === CHMSUKOT) {
return this.findInternal(parsha);
}
return this.find(parsha.split('-'));
} else {
// try to find Saturday holiday like 'Yom Kippur'
const idx = this.theSedraArray.indexOf(parsha);
if (idx === -1) {
return null; // doesn't occur this year
}
return new HDate(this.firstSaturday + idx * 7);
return this.findInternal(parsha);
}
} else if (Array.isArray(parsha)) {
const plen = parsha.length;
Expand All @@ -194,16 +189,6 @@ export class Sedra {
const p2 = parsha[1];
const num1 = parsha2id.get(p1);
const num2 = parsha2id.get(p2);
//Attempt to find Holidays with dash such as Sukkot Shabbat Chol ha-Moed
if (
!num1 &&
!num2 &&
this.theSedraArray.indexOf(parsha.join('-')) !== -1
) {
const rejoinedName = parsha.join('-');
const idx = this.theSedraArray.indexOf(rejoinedName);
return new HDate(this.firstSaturday + idx * 7);
}
if (
typeof num1 !== 'number' ||
typeof num2 !== 'number' ||
Expand All @@ -217,6 +202,14 @@ export class Sedra {
return null; /* NOTREACHED */
}

private findInternal(parsha: NumberOrString): HDate | null {
const idx = this.theSedraArray.indexOf(parsha);
if (idx === -1) {
return null; // doesn't occur this year
}
return new HDate(this.firstSaturday + idx * 7);
}

/**
* Returns the date that a parsha (or its doubled or undoubled counterpart)
* occurs, or `null` if the parsha doesn't occur this year
Expand Down
1 change: 1 addition & 0 deletions test/sedra.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ test('find', () => {
expect(sedra.find('Chukat-Balak')).toBe(null);
const sedra5785 = new Sedra(5785, false);
expect(dt(sedra5785.find('Sukkot Shabbat Chol ha-Moed'))).toBe('2024-10-19');
expect(sedra.find('Sukkot Shabbat Chol ha-Moed')).toBeNull();
});

test('findContaining', () => {
Expand Down

0 comments on commit 000374c

Please sign in to comment.