Skip to content

Commit

Permalink
Make DailyLearning calendar names case-insensitive
Browse files Browse the repository at this point in the history
mjradwin committed Jan 12, 2025
1 parent 8f03446 commit d7d629d
Showing 6 changed files with 149 additions and 111 deletions.
196 changes: 98 additions & 98 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/core",
"version": "5.8.11",
"version": "5.8.12",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"contributors": [
"Eyal Schachter (https://github.com/Scimonster)",
@@ -78,17 +78,17 @@
"@rollup/plugin-node-resolve": "^16.0.0",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^12.1.2",
"@types/node": "^22.10.2",
"@types/node": "^22.10.5",
"@vitest/coverage-v8": "^2.1.8",
"core-js": "^3.39.0",
"core-js": "^3.40.0",
"gettext-parser": "^8.0.0",
"gts": "^6.0.2",
"pretty-bytes": "^6.1.1",
"rollup": "^4.29.1",
"rollup": "^4.30.1",
"rollup-plugin-bundle-size": "^1.0.3",
"rollup-plugin-visualizer": "^5.13.1",
"rollup-plugin-visualizer": "^5.14.0",
"typedoc": "^0.27.6",
"typescript": "^5.7.2",
"typescript": "^5.7.3",
"vitest": "^2.1.8"
},
"dependencies": {
19 changes: 14 additions & 5 deletions src/DailyLearning.ts
Original file line number Diff line number Diff line change
@@ -12,29 +12,38 @@ const cals = new Map<string, Function>();
export class DailyLearning {
/**
* Register a new learning calendar.
* @param name case insensitive
*/
static addCalendar(name: string, calendar: Function) {
if (typeof calendar !== 'function') {
throw new TypeError(`Invalid calendar function: ${calendar}`);
}
cals.set(name, calendar);
cals.set(name.toLowerCase(), calendar);
}

/**
* Returns an event from daily calendar for a given date. Returns `null` if there
* is no learning from this calendar on this date.
* @param name
* @param hd
* @param il
* @param name case insensitive
* @param hd Hebrew Date
* @param il true for Israel, false for Diaspora
*/
static lookup(name: string, hd: HDate, il: boolean): Event | null {
const fn = cals.get(name);
const fn = cals.get(name.toLowerCase());
if (typeof fn === 'function') {
return fn(hd, il);
}
return null;
}

/**
* Tests to see if learning calendar has been registered
* @param name case insensitive
*/
static has(name: string): boolean {
return cals.has(name);
}

/** Returns the names of all calendars registered */
static getCalendars(): string[] {
return Array.from(cals.keys());
8 changes: 8 additions & 0 deletions src/calendar.ts
Original file line number Diff line number Diff line change
@@ -347,6 +347,14 @@ function warnUnrecognizedOptions(options: CalOptions) {
unrecognizedAlreadyWarned.add(k);
}
}
if (options.dailyLearning) {
for (const k of Object.keys(options.dailyLearning)) {
if (!unrecognizedAlreadyWarned.has(k) && !DailyLearning.has(k)) {
console.warn(`Ignoring unrecognized DailyLearning calendar: ${k}`);
unrecognizedAlreadyWarned.add(k);
}
}
}
}

const israelCityOffset: StringIntMap = {
4 changes: 2 additions & 2 deletions test/DailyLearning.spec.ts
Original file line number Diff line number Diff line change
@@ -8,10 +8,10 @@ test('DailyLearning', () => {
const hd = new HDate();
DailyLearning.addCalendar('Foo', dummy);
DailyLearning.addCalendar('Bar', dummy);
expect(DailyLearning.getCalendars()).toEqual(['Foo', 'Bar']);
expect(DailyLearning.getCalendars()).toEqual(['foo', 'bar']);
const dummy2 = () => {return {bogus: true}};
DailyLearning.addCalendar('Quux', dummy2);
expect(DailyLearning.getCalendars()).toEqual(['Foo', 'Bar', 'Quux']);
expect(DailyLearning.getCalendars()).toEqual(['foo', 'bar', 'quux']);
expect(DailyLearning.lookup('Foo', hd, false)).toBeNull();
expect(DailyLearning.lookup('Bar', hd, false)).toBeNull();
expect(DailyLearning.lookup('Quux', hd, false)).toEqual({bogus: true});
21 changes: 21 additions & 0 deletions test/calendar.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {afterAll, expect, test, vi} from 'vitest';
import {CalOptions} from '../src/CalOptions';
import {calendar} from '../src/calendar';

const consoleMock = vi.spyOn(console, 'warn');

afterAll(() => {
consoleMock.mockReset();
});

test('calendar() calls warnUnrecognizedOptions()', () => {
const options: CalOptions = {};
Object.assign(options, {nonsense: true});
calendar(options);
expect(consoleMock).toHaveBeenLastCalledWith('Ignoring unrecognized HebrewCalendar option: nonsense');
});

test('warnUnrecognizedOptions also checks dailyLearning', () => {
calendar({dailyLearning: {foobar: true}});
expect(consoleMock).toHaveBeenLastCalledWith('Ignoring unrecognized DailyLearning calendar: foobar');
});

0 comments on commit d7d629d

Please sign in to comment.