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 runtime check & typings of exclusive parameters timeZone & utcOffset #704

Open
gomez-git opened this issue Sep 30, 2023 · 3 comments
Labels
good first issue Good for newcomers hacktoberfest This issue is registered for Hacktoberfest! help wanted Extra attention is needed status:ready Ready to start implementation type:refactor Changes that neither fixes a bug nor adds a feature

Comments

@gomez-git
Copy link

gomez-git commented Sep 30, 2023

Does it? First condition precludes the second

if (timeZone != null && utcOffset != null) {
throw new ExclusiveParametersError('timeZone', 'utcOffset');

this.cronTime = new CronTime(cronTime, timeZone, utcOffset);

@sheerlox
Copy link
Collaborator

hey @gomez-git 👋 thank you for your suggestion!

I liked the solution you first suggested:

if (timeZone != null && utcOffset != null) {
	throw new ExclusiveParametersError('timeZone', 'utcOffset');
} else if (timeZone != null) {
	this.cronTime = new CronTime(cronTime, timeZone, null);
} else {
	this.cronTime = new CronTime(cronTime, null, utcOffset);
}

but then I thought (again), why not do:

if (timeZone != null && utcOffset != null) {
	throw new ExclusiveParametersError('timeZone', 'utcOffset');
} else {
	this.cronTime = new CronTime(cronTime, null, utcOffset);
}

the reason we can't is that even though TypeScript knows timeZone and utcOffset are mutually exclusive in the CronJobParams interface, it seems impossible to implement a similar behavior outside of interfaces (like two variables or function arguments).

so I'm thinking of a helper function, which would take timeZone and utcOffset as parameters, throw the error if both are defined and run the if/else if/else logic you suggested to return an object {timeZone, utcOffset} were they are mutually exclusive (like in CronJobParams).

this way we centralize the timeZone != null && utcOffset != null and error throwing into a single place, and that would theoretically allow us to refactor this horror I made into a single new CronJob call 🎉

would you be interested in submitting a PR for this change? this repository is going to be part of Hacktoberbest, so if you're planning to participate we can register that issue/PR as a participation 😉

@sheerlox sheerlox reopened this Sep 30, 2023
@sheerlox sheerlox changed the title Extra line in job.ts if timeZone and utcOffset is set refactor runtime check and improve typings of exclusive parameters timeZone & utcOffset Sep 30, 2023
@sheerlox sheerlox added type:feature New feature or feature improvement & requests hacktoberfest This issue is registered for Hacktoberfest! type:refactor Changes that neither fixes a bug nor adds a feature status:ready Ready to start implementation and removed type:feature New feature or feature improvement & requests labels Sep 30, 2023
@sheerlox sheerlox changed the title refactor runtime check and improve typings of exclusive parameters timeZone & utcOffset refactor runtime check & typings of exclusive parameters timeZone & utcOffset Oct 1, 2023
@sheerlox sheerlox added help wanted Extra attention is needed good first issue Good for newcomers labels Oct 12, 2023
@rharshit82
Copy link
Contributor

picking this up

@Victor1890
Copy link

Victor1890 commented Jan 23, 2024

Hi @gomez-git and @sheerlox, I have seen that issue and this is my proposal to refactor code using your suggestion

PR: #841

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers hacktoberfest This issue is registered for Hacktoberfest! help wanted Extra attention is needed status:ready Ready to start implementation type:refactor Changes that neither fixes a bug nor adds a feature
Projects
None yet
Development

No branches or pull requests

4 participants