Allow removing the zeroes of durations #1679
Open
+83
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.