Skip to content

Commit

Permalink
Add some range checks for invalid parsha names and year numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Jul 8, 2024
1 parent 7a4a5f7 commit 147f251
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/parshaHaShavua.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getTriennialForParshaHaShavua(
if (!(ev instanceof Event)) {
throw new TypeError(`Bad event argument: ${ev}`);
} else if (ev.getFlags() !== flags.PARSHA_HASHAVUA) {
throw new TypeError(`Event must be parsha hashavua: ${ev.getDesc()}`);
throw new TypeError(`Bad event argument: ${ev.getDesc()}`);
}
const hd = ev.getDate();
const hyear0 = hd.getFullYear();
Expand Down
9 changes: 8 additions & 1 deletion src/triennial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,15 @@ export class Triennial {
* @returns result, including a map of aliyot 1-7 plus "M"
*/
getReading(parsha: string, yearNum: number): TriennialAliyot {
if (yearNum < 0 || yearNum > 2) {
throw new RangeError(`invalid year number: ${yearNum}`);
}
const years = this.readings.get(parsha);
if (!years) {
throw new RangeError(`invalid parsha: ${parsha}`);
}
// don't use clone() here because we want to preserve HDate objects
const reading0 = this.readings.get(parsha)![yearNum];
const reading0 = years[yearNum];
const reading: TriennialAliyot = {...reading0};
if (reading.aliyot) {
Object.values(reading.aliyot).map((aliyah: Aliyah) =>
Expand Down
10 changes: 9 additions & 1 deletion test/parshaHaShavua.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {HebrewCalendar, HDate, months, flags, ParshaEvent} from '@hebcal/core';
import {HebrewCalendar, HDate, months, flags, ParshaEvent, Event} from '@hebcal/core';
import {getTriennialForParshaHaShavua} from '../src/parshaHaShavua';
import {formatAliyahWithBook} from '@hebcal/leyning';

Expand Down Expand Up @@ -257,3 +257,11 @@ test('no-triennial-haft-on-special', () => {
expect(reading.haftara).not.toBeDefined();
expect(reading.haftaraNumV).not.toBeDefined();
});

test('getTriennialForParshaHaShavua-throws', () => {
const hd = new HDate(29, 'Tishrei', 5784);
const ev = new Event(hd, 'Bogus', flags.DAF_YOMI);
expect(() => {
getTriennialForParshaHaShavua(ev, false);
}).toThrow('Bad event argument: Bogus');
});
17 changes: 17 additions & 0 deletions test/triennial.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,20 @@ test('Yitro', () => {
expect(reading2.aliyot).toEqual(expected23);
expect(reading3.aliyot).toEqual(expected23);
});

test('getReading-throws', () => {
const tri = new Triennial(5780);
expect(() => {
tri.getReading('Noach', 3);
}).toThrow('invalid year number: 3');

expect(() => {
tri.getReading('Bogus', 1);
}).toThrow('invalid parsha: Bogus');
});

test('getYearNumber-throws', () => {
expect(() => {
Triennial.getYearNumber(1234);
}).toThrow('Invalid Triennial year 1234');
});

0 comments on commit 147f251

Please sign in to comment.