Question: isTimeUnit() helper
Hello! It's me again with my sometimes stupid questions.
Is there any helper method to check if a string is supported as a time-unit? Something like
spacetime.isTimeUnit("days"); // -> true
spacetime.isTimeUnit("daus"); // -> c'mon, of course false
Actually, it's cool that there is typing from TypeScript, but in runtime this typing is useless.
It would be convenient if there was such a method to check if some value can be a time-unit argument.
Thank you for the answer!
hey Vano, you can see the list of supported time units here It hasn't changed in years, so you're probably safe copy+paste that to validate on your end cheers
@spencermountain
Hey Spencer. Thank you for your interest in my crazy questions!
I just saw that there is no validation at all for the time unit values. Also the function simply silently does not change the internal value, as if nothing happened.
console.log(spacetime.now().format('sql'))
console.log(spacetime.now().add(777, 'this_value_is_not_even_close_to_unit_value').format('sql'));
To me, this behavior is a bit strange.
Also there is more correct list of values for my question :)
you're right - I'm not sure what it should do in this case. Future versions should handle invalid inputs much differently, but in the meantime perhaps this should log something when verbose is set to true? wanna have a go at that? I don't wanna throw, or invalidate the date in this case - unless you have a strong opinion on this. cheers
@spencermountain
I think there's really no need to break this behavior for now. This is a major (version) change for sure.
I think there are two useful features that can be implemented:
- as you said, in verbose mode, just write to the console that the parameter is bad or unrecognized
- we can already integrate separate aliases, mustAdd() and mustSubtract(), that will throw exceptions if any argument isn't correct
Sounds good?
cool ok. let's hold-off on mustAdd() for now - that's a clever solution, but I suppose people can just run .isEqual() after an add? Those methods could also be a plugin, if the demand for this arose.
cheers