From 1b330115e5d6eb75937de5f4338b6fadf386da8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Jes=C3=BAs=20Rosario=20V=C3=A1squez?= <46900196+Victor1890@users.noreply.github.com> Date: Tue, 23 Jan 2024 08:33:35 -0400 Subject: [PATCH] refactor: :recycle: refactor parameters timeZone & utcOffset refactor runtime check & typings of exclusive parameters timeZone & utcOffset and Add getTimeZoneAndOffset function to utils.ts --- src/job.ts | 16 ++++++---------- src/utils.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/job.ts b/src/job.ts index b1d8be75..09564130 100644 --- a/src/job.ts +++ b/src/job.ts @@ -10,6 +10,7 @@ import { CronOnCompleteCommand, WithOnComplete } from './types/cron.types'; +import { getTimeZoneAndOffset } from './utils'; export class CronJob { cronTime: CronTime; @@ -61,17 +62,12 @@ export class CronJob { this.context = (context ?? this) as CronContext; // 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; diff --git a/src/utils.ts b/src/utils.ts index 6f53350b..551c2e48 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,4 @@ +import { ExclusiveParametersError } from './errors'; import { Ranges } from './types/cron.types'; export const getRecordKeys = ( @@ -5,3 +6,20 @@ export const getRecordKeys = ( ) => { 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 }; + } +};