-
Notifications
You must be signed in to change notification settings - Fork 751
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
Allow removing the zeroes of durations #1679
base: master
Are you sure you want to change the base?
Conversation
Thank you for your contribution and the thorough analysis! |
Thanks for your kind reply! I will implement this tomorrow and update the pull request. Is there other things you think the PR might lack? As mentioned previously I was unable to auto generate the documentation. |
@diesieben07 I amended better tests for Let me know what you think! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am very very sorry for the delayed review.
Thank you for the changes, the showZeros
option looks fine except for the default handling that I noted.
No worries and thanks for the feedback. |
Changes
With this merge request the internal
removeZeroes
function of aDuration
will also be accessible publically.Why?
If you end up having zero values within your duration they are almost impossible to "destruct" with the current state of the Api.
At the moment the only Option is to use
rescale()
. But this is not compatible / wont help with some of the other use cases offered by the Api.One concrete example of that is the following:
year
,month
anddays
if they are not zeroyears
andmonths
(I dont want 24 months for example)Try#1 -
shiftTo()
Intuitively one would use
shiftTo()
like this:When using something like
toHuman()
this will produce an output like2 years, 0 months, 8 days
which I dont want to show to the user. This solution breaks rule#1Try#2 -
rescale()
rescale()
is the only function currently capable of removing zero values.This breaks rule#1 and displays units other than
year
,month
anddays
.Try#3 - Best of both worlds?
Either combination does not work.
Solution with this MR
Adding
removeZeroes()
at the end will get rid of the leftover 0 months created byshiftTo()
.In a real world example using
toHuman()
would produce a proper output like2 years, 8 days
.Alternatives
As shown there is no alternative to really get rid of something like 0 months or years if you want to shift to years or months.
There are two possible alternatives but the offer less flexbility to the user:
toHuman()
shiftTo()
But overall I think the ability to remove zeroes whenever wanted warrents the addition of this function.
I am open to feedback, let me know what you think!
I had some troubles generating the docs, so I am happy if you can help me out there.