Skip to content

Commit

Permalink
Remove some SonarCloud code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed May 31, 2024
1 parent 1967236 commit eaeef5c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/TimedEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TimedEvent extends Event {
this.location = location;
const timeFormat = location.getTimeFormatter();
this.eventTimeStr = Zmanim.formatTime(this.eventTime, timeFormat);
const opts = Object.assign({ location }, options);
const opts = {...options, location};
this.fmtTime = reformatTimeStr(this.eventTimeStr, 'pm', opts);
if (typeof linkedEvent !== 'undefined') {
this.linkedEvent = linkedEvent;
Expand Down
11 changes: 9 additions & 2 deletions src/getStartAndEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ function getAbs(d: Date | HDate | number): number {
throw new TypeError(`Invalid date type: ${d}`);
}

function getYear(options: CalOptions): number {
if (typeof options.year !== 'undefined') {
return Number(options.year);
}
return options.isHebrewYear ? new HDate().getFullYear() :
new Date().getFullYear();
}

/**
* Parse options object to determine start & end days
* @private
Expand All @@ -25,8 +33,7 @@ export function getStartAndEnd(options: CalOptions): number[] {
return [getAbs(options.start), getAbs(options.end)];
}
const isHebrewYear = Boolean(options.isHebrewYear);
const theYear = typeof options.year !== 'undefined' ? Number(options.year) :
isHebrewYear ? new HDate().getFullYear() : new Date().getFullYear();
const theYear = getYear(options);
if (isNaN(theYear)) {
throw new RangeError(`Invalid year ${options.year}`);
} else if (isHebrewYear && theYear < 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/hebcal.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ export class HebrewCalendar {
* @return {Event[]}
*/
static calendar(options={}) {
options = Object.assign({}, options); // so we can modify freely
options = {...options}; // so we can modify freely
checkCandleOptions(options);
const location = options.location = options.location || defaultLocation;
const il = options.il = options.il || location.il || false;
Expand Down
13 changes: 7 additions & 6 deletions src/zmanim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function zdtToDate(zdt: Temporal.ZonedDateTime | null): Date {
return res;
}

function getDate(date: Date | HDate): Date {
if (greg.isDate(date)) return date as Date;
if (HDate.isHDate(date)) return (date as HDate).greg();
throw new TypeError(`invalid date: ${date}`);
}

/**
* Calculate halachic times (zmanim / זְמַנִּים) for a given day and location.
* Calculations are available for tzeit / tzais (nightfall),
Expand Down Expand Up @@ -63,12 +69,7 @@ export class Zmanim {
* These zmanim intentionally do not support elevation adjustment.
*/
constructor(gloc: GeoLocation, date: Date | HDate, useElevation: boolean) {
const dt: Date = greg.isDate(date) ? (date as Date) :
HDate.isHDate(date) ? (date as HDate).greg() :
new Date(NaN);
if (isNaN(dt.getTime())) {
throw new TypeError(`invalid date: ${date}`);
}
const dt = getDate(date);
this.date = dt;
this.gloc = gloc;
const plainDate = Temporal.PlainDate.from({
Expand Down
16 changes: 8 additions & 8 deletions test/getHolidaysOnDate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jest.mock('quick-lru', () => {
test('getHolidaysOnDate', () => {
const hyear = 5771;
const expected = [
new HDate(1, 'Tishrei', hyear).abs() - 1, ['Erev Rosh Hashana'],
new HDate(1, 'Tishrei', hyear), ['Rosh Hashana 5771'],
new HDate(10, 'Tishrei', hyear), ['Yom Kippur'],
new HDate(3, 'Cheshvan', hyear), undefined,
new Date(2010, 11, 7), ['Chag HaBanot', 'Chanukah: 7 Candles', 'Rosh Chodesh Tevet'],
{dt: new HDate(1, 'Tishrei', hyear).abs() - 1, desc: ['Erev Rosh Hashana']},
{dt: new HDate(1, 'Tishrei', hyear), desc: ['Rosh Hashana 5771']},
{dt: new HDate(10, 'Tishrei', hyear), desc: ['Yom Kippur']},
{dt: new HDate(3, 'Cheshvan', hyear), desc: undefined},
{dt: new Date(2010, 11, 7), desc: ['Chag HaBanot', 'Chanukah: 7 Candles', 'Rosh Chodesh Tevet']},
];
for (let i = 0; i < expected.length; i += 2) {
const dt = expected[i] as Date | HDate;
const desc = expected[i + 1];
for (const item of expected) {
const dt = item.dt;
const desc = item.desc;
const ev = HebrewCalendar.getHolidaysOnDate(dt);
if (typeof desc === 'undefined') {
expect(ev).toBe(undefined);
Expand Down

0 comments on commit eaeef5c

Please sign in to comment.