Closed jfrux closed 6 years ago
Having this same issue. Passing in {trim: "right"} to config removes the 'minutes' string when the value is 0 however, the value 0 still displays. @joshuairl
+1 I've come across this just now as well
{trim: "right"}
actually removes the STRING s
or m
but doesn't remove the :0
.
I managed to handle it this way: ( I could do the same for hours but that's not my usecase)
moment.duration(seconds, "seconds")
.format("h[h]:m[m]:s[s]")
.replace(/\:0s$/, "")
.replace(/\:0m$/, "")
trim left
and right
are implemented to not remove the final value, i.e. to always give some output.
trim both
is implemented in the version now on master
branch, and it does the same thing... it will always give some output.
I think a trim zero
option that automatically trims all zero-value tokens (even down to an empty output) would solve this, and it would be pretty easy to do in the updated plugin.
Just an FYI, I'm working on an updated trim API, which is the last feature I think for version 2.0.0.
The version 1 trim options will be fully supported and not break anything. Here's the docs for the new version:
The default trim
value is "largest"
.
Largest-magnitude tokens are automatically trimmed when they have no value.
moment.duration(123, "minutes").format("d[d] h:mm:ss");
// "2:03:00"
Trimming also functions when the format string is oriented with token magnitude increasing from left to right.
moment.duration(123, "minutes").format("s [seconds], m [minutes], h [hours], d [days]");
// "0 seconds, 3 minutes, 2 hours"
To stop trimming altogether, set { trim: false }
.
moment.duration(123, "minutes").format("d[d] h:mm:ss", { trim: false });
// "0d 2:03:00"
trim
can be a string, a delimited list of strings, an array of strings, or a boolean. Accepted values are as follows:
"large"
Trim largest-magnitude zero-value tokens until finding a token with a value, a token identified as stopTrim
, or the final token of the format string. This is the default trim
value.
moment.duration(123, "minutes").format("d[d] h:mm:ss");
// "2:03:00"
moment.duration(123, "minutes").format("d[d] h:mm:ss", { trim: "large" }); // "2:03:00"
moment.duration(0, "minutes").format("d[d] h:mm:ss", { trim: "large" }); // "0"
- `"small"`
Trim smallest-magnitude zero-value tokens until finding a token with a value, a token identified as `stopTrim`, or the final token of the format string.
moment.duration(123, "minutes").format("d[d] h:mm:ss", { trim: "small" }); // "0d 2:03"
moment.duration(0, "minutes").format("d[d] h:mm:ss", { trim: "small" }); // "0d"
- `"both"`
Execute `"large"` trim then `"small"` trim.
moment.duration(123, "minutes").format("d[d] h[h] m[m] s[s]", { trim: "both" }); // "2h 3m"
moment.duration(0, "minutes").format("d[d] h[h] m[m] s[s]", { trim: "both" }); // "0s"
- `"mid"`
Trim any zero-value tokens that are not the first or last tokens. Usually used in conjunction with `"large"` or `"both"`. e.g. `"large mid"` or `"both mid"`.
moment.duration(1441, "minutes").format("w[w] d[d] h[h] m[m] s[s]", { trim: "mid" }); // "0w 1d 1m 0s"
moment.duration(1441, "minutes").format("w[w] d[d] h[h] m[m] s[s]", { trim: "large mid" }); // "1d 1m 0s"
moment.duration(1441, "minutes").format("w[w] d[d] h[h] m[m] s[s]", { trim: "small mid" }); // "0w 1d 1m"
moment.duration(1441, "minutes").format("w[w] d[d] h[h] m[m] s[s]", { trim: "both mid" }); // "1d 1m"
moment.duration(0, "minutes").format("w[w] d[d] h[h] m[m] s[s]", { trim: "both mid" }); // "0s"
- `"final"`
Trim the final token if it is zero-value. Use this option with `"large"` or `"both"` to output an empty string when formatting a zero-value duration. e.g. `"large final"` or `"both final"`.
moment.duration(0, "minutes").format("d[d] h:mm:ss", { trim: "large final" }); // ""
moment.duration(0, "minutes").format("d[d] h:mm:ss", { trim: "small final" }); // ""
moment.duration(0, "minutes").format("d[d] h[h] m[m] s[s]", { trim: "both final" }); // ""
- `"all"`
Trim all zero-value tokens. Shorthand for `"both mid final"`.
moment.duration(0, "minutes").format("d[d] h[h] m[m] s[s]", { trim: "all" }); // ""
- `"left"`
Maps to `"large"` to support this plugin's version 1 API.
- `"right"`
Maps to `"large"` to support this plugin's version 1 API.
- `true`
Maps to `"large"`.
- `false`
Disables trimming.
I'm trying to figure it out from the documentation but can't seem to find a way to make it clear out that number if it's 0?