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

fix: refactored typing for timezone and utcoffset #808

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 14 additions & 35 deletions src/job.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn } from 'child_process';
import { CronError, ExclusiveParametersError } from './errors';

Check warning on line 2 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

'ExclusiveParametersError' is defined but never used

Check warning on line 2 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

'ExclusiveParametersError' is defined but never used

Check warning on line 2 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

'ExclusiveParametersError' is defined but never used
import { CronTime } from './time';
import {
CronCallback,
Expand All @@ -10,6 +10,7 @@
CronOnCompleteCommand,
WithOnComplete
} from './types/cron.types';
import { getExclusiveTimezoneOrUtcOffset } from './utils';

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

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

Check failure on line 64 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Replace `timeZone,·utcOffset` with `⏎↹↹↹timeZone,⏎↹↹↹utcOffset⏎↹↹`

Check failure on line 64 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Replace `timeZone,·utcOffset` with `⏎↹↹↹timeZone,⏎↹↹↹utcOffset⏎↹↹`

Check failure on line 64 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Replace `timeZone,·utcOffset` with `⏎↹↹↹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 = exclusiveParams.timeZone !== undefined

Check failure on line 66 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Replace `·` with `⏎↹↹↹`

Check failure on line 66 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Replace `·` with `⏎↹↹↹`

Check failure on line 66 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Replace `·` with `⏎↹↹↹`
? new CronTime(cronTime, exclusiveParams.timeZone as string | undefined)

Check failure on line 67 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Insert `↹`

Check failure on line 67 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Insert `↹`

Check failure on line 67 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Insert `↹`
: new CronTime(cronTime, undefined, exclusiveParams.utcOffset as number | undefined);

Check failure on line 68 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Replace `:·new·CronTime(cronTime,·undefined,·exclusiveParams.utcOffset·as·number·|·undefined` with `↹:·new·CronTime(⏎↹↹↹↹↹↹cronTime,⏎↹↹↹↹↹↹undefined,⏎↹↹↹↹↹↹exclusiveParams.utcOffset·as·number·|·undefined⏎↹↹↹↹··`

Check failure on line 68 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Replace `:·new·CronTime(cronTime,·undefined,·exclusiveParams.utcOffset·as·number·|·undefined` with `↹:·new·CronTime(⏎↹↹↹↹↹↹cronTime,⏎↹↹↹↹↹↹undefined,⏎↹↹↹↹↹↹exclusiveParams.utcOffset·as·number·|·undefined⏎↹↹↹↹··`

Check failure on line 68 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Replace `:·new·CronTime(cronTime,·undefined,·exclusiveParams.utcOffset·as·number·|·undefined` with `↹:·new·CronTime(⏎↹↹↹↹↹↹cronTime,⏎↹↹↹↹↹↹undefined,⏎↹↹↹↹↹↹exclusiveParams.utcOffset·as·number·|·undefined⏎↹↹↹↹··`

if (unrefTimeout != null) {
this.unrefTimeout = unrefTimeout;
Expand Down Expand Up @@ -101,46 +95,31 @@
static from<OC extends CronOnCompleteCommand | null = null, C = null>(
params: CronJobParams<OC, C>
) {
// runtime check for JS users
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (params.timeZone != null && params.utcOffset != null) {
throw new ExclusiveParametersError('timeZone', 'utcOffset');
}
const { timeZone, utcOffset } = getExclusiveTimezoneOrUtcOffset(params.timeZone, params.utcOffset);

Check failure on line 98 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Replace `params.timeZone,·params.utcOffset` with `⏎↹↹↹params.timeZone,⏎↹↹↹params.utcOffset⏎↹↹`

Check failure on line 98 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Replace `params.timeZone,·params.utcOffset` with `⏎↹↹↹params.timeZone,⏎↹↹↹params.utcOffset⏎↹↹`

Check failure on line 98 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Replace `params.timeZone,·params.utcOffset` with `⏎↹↹↹params.timeZone,⏎↹↹↹params.utcOffset⏎↹↹`

if (params.timeZone != null) {
return new CronJob<OC, C>(
params.cronTime,
params.onTick,
params.onComplete,
params.start,
params.timeZone,
params.context,
params.runOnInit,
params.utcOffset,
params.unrefTimeout
);
} else if (params.utcOffset != null) {
if (timeZone !== undefined) {
return new CronJob<OC, C>(
params.cronTime,
params.onTick,
params.onComplete,
params.start,
null,
timeZone,
params.context,
params.runOnInit,
params.utcOffset,
null,

Check failure on line 109 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Delete `·`

Check failure on line 109 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Delete `·`

Check failure on line 109 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Delete `·`
params.unrefTimeout
);
} else {
// Only utcOffset is defined or both are undefined
return new CronJob<OC, C>(
params.cronTime,
params.onTick,
params.onComplete,
params.start,
params.timeZone,
null,

Check failure on line 119 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Delete `·`

Check failure on line 119 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Delete `·`

Check failure on line 119 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Delete `·`
params.context,
params.runOnInit,
params.utcOffset,
utcOffset,
params.unrefTimeout
);
}
Expand Down Expand Up @@ -198,8 +177,8 @@
void callback.call(
this.context,
this.onComplete as WithOnComplete<OC> extends true
? CronOnCompleteCallback
: never
? CronOnCompleteCallback

Check failure on line 180 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Insert `↹`

Check failure on line 180 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Insert `↹`

Check failure on line 180 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Insert `↹`
: never

Check failure on line 181 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Insert `↹`

Check failure on line 181 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Insert `↹`

Check failure on line 181 in src/job.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Insert `↹`
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
Acc extends number[] = []
> = Acc['length'] extends N
? WithTail extends true
? [...Acc, Acc['length']][number]
: Acc[number]
? [...Acc, Acc['length']][number]

Check failure on line 12 in src/types/utils.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

Insert `↹`

Check failure on line 12 in src/types/utils.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

Insert `↹`

Check failure on line 12 in src/types/utils.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

Insert `↹`
: Acc[number]
: Enumerate<N, WithTail, [...Acc, Acc['length']]>;

export type ExclusiveTimezoneOrUtcOffset =
| { timeZone: string | null | undefined; utcOffset: undefined }
| { timeZone: undefined; utcOffset: number | null | undefined };
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { Ranges } from './types/cron.types';
import { ExclusiveParametersError } from './errors';
import { CronJobParams, CronOnCompleteCommand } from './types/cron.types';
import { ExclusiveTimezoneOrUtcOffset } from './types/utils'

Check warning on line 4 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 20)

'ExclusiveTimezoneOrUtcOffset' is defined but never used

Check warning on line 4 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 16)

'ExclusiveTimezoneOrUtcOffset' is defined but never used

Check warning on line 4 in src/utils.ts

View workflow job for this annotation

GitHub Actions / test_matrix (ubuntu-latest, 18)

'ExclusiveTimezoneOrUtcOffset' is defined but never used

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 getExclusiveTimezoneOrUtcOffset = <OC extends CronOnCompleteCommand, C>(
timeZone?: CronJobParams<OC, C>['timeZone'],
utcOffset?: CronJobParams<OC, C>['utcOffset']
): { timeZone: string | null | undefined, utcOffset: number | null | undefined } => {
if (timeZone != null && utcOffset != null) {
throw new ExclusiveParametersError('timeZone', 'utcOffset');
}

return {
timeZone: timeZone ?? null,
utcOffset: utcOffset ?? null
};
};
Loading