Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ♻️ refactor parameters timeZone & utcOffset #841

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CronOnCompleteCommand,
WithOnComplete
} from './types/cron.types';
import { getTimeZoneAndOffset } from './utils';

export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
cronTime: CronTime;
Expand Down Expand Up @@ -61,17 +62,12 @@ export class CronJob<OC extends CronOnCompleteCommand | null = null, C = null> {
this.context = (context ?? this) as CronContext<C>;

// runtime check for JS users
if (timeZone != null && utcOffset != null) {
throw new ExclusiveParametersError('timeZone', 'utcOffset');
}
const { timeZone: tz, utcOffset: uo } = getTimeZoneAndOffset(
timeZone,
utcOffset
);

if (timeZone != null) {
this.cronTime = new CronTime(cronTime, timeZone, null);
} else if (utcOffset != null) {
this.cronTime = new CronTime(cronTime, null, utcOffset);
} else {
this.cronTime = new CronTime(cronTime, timeZone, utcOffset);
}
this.cronTime = new CronTime(cronTime, tz, uo as null | undefined);

if (unrefTimeout != null) {
this.unrefTimeout = unrefTimeout;
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { ExclusiveParametersError } from './errors';
import { Ranges } from './types/cron.types';

export const getRecordKeys = <K extends Ranges[keyof Ranges]>(
record: Partial<Record<K, boolean>>
) => {
return Object.keys(record) as unknown as (keyof typeof record)[];
};

export const getTimeZoneAndOffset = (
timeZone?: string | null,
utcOffset?: number | null
) => {
if (timeZone != null && utcOffset != null) {
throw new ExclusiveParametersError('timeZone', 'utcOffset');
}

if (timeZone != null) {
return { timeZone, utcOffset: null };
} else if (utcOffset != null) {
return { timeZone: null, utcOffset };
} else {
return { timeZone: null, utcOffset: null };
}
};