formkit / tempo

📆 Parse, format, manipulate, and internationalize dates and times in JavaScript and TypeScript.
https://tempo.formkit.com
MIT License
2.37k stars 33 forks source link

Optional argument in `is*` functions #56

Open Serator opened 6 months ago

Serator commented 6 months ago

In case we are comparing with the current time, the second argument could be omitted.

image

It seems to work as expected, but it doesn't seem obvious from the docs.

It also causes a type error because the second argument is required.

image

Most likely, in functions other than is* you can follow the same way and not write new Date() in case the operation is performed with the current time.

WilcoSp commented 6 months ago

this comes from the date function which converts an undefined value to a new Date object with the current time.

for at least the is* and diff* functions it might be a nice idea to make some argument(s) optional to use the current time

Serator commented 6 months ago

This would also work well with add* functions and such, but I don't see a way to do it without breaking the current design. Unless you use an explicit null as a keyword (addDay(null, 5)) or something like that, but it doesn't look like much. In any case, I would like a consistent solution.

WilcoSp commented 6 months ago

If it would be implemented I do think it would be best to use typescript's function overload feature to provide more jsdoc, but also prevent unexpected behaviour from being created (like isAfter(null,null))

Also it's maybe best to first discuss together with Justin and other contributors which function categories and/or independent functions should allow for null/undefined arguments as this could have a big impact on Tempo