Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,21 +488,25 @@ export default class Duration {
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options
* @param {Object} opts - Formatting options. Accepts the same keys as the options parameter of the native `Intl.NumberFormat` constructor, as well as `listStyle`.
* @param {string} [opts.listStyle='narrow'] - How to format the merged list. Corresponds to the `style` property of the options parameter of the native `Intl.ListFormat` constructor.
* @param {boolean} [opts.showZeros=true] - Show all units previously used by the duration even if they are zero
* @example
* ```js
* var dur = Duration.fromObject({ days: 1, hours: 5, minutes: 6 })
* dur.toHuman() //=> '1 day, 5 hours, 6 minutes'
* dur.toHuman({ listStyle: "long" }) //=> '1 day, 5 hours, and 6 minutes'
* dur.toHuman({ unitDisplay: "short" }) //=> '1 day, 5 hr, 6 min'
* var dur = Duration.fromObject({ months: 1, weeks: 0, hours: 5, minutes: 6 })
* dur.toHuman() //=> '1 month, 0 weeks, 5 hours, 6 minutes'
* dur.toHuman({ listStyle: "long" }) //=> '1 month, 0 weeks, 5 hours, and 6 minutes'
* dur.toHuman({ unitDisplay: "short" }) //=> '1 mth, 0 wks, 5 hr, 6 min'
* dur.toHuman({ showZeros: false }) //=> '1 month, 5 hours, 6 minutes'
* ```
*/
toHuman(opts = {}) {
if (!this.isValid) return INVALID;

const showZeros = opts.showZeros !== false;

const l = orderedUnits
.map((unit) => {
const val = this.values[unit];
if (isUndefined(val)) {
if (isUndefined(val) || (val === 0 && !showZeros)) {
return null;
}
return this.loc
Expand Down Expand Up @@ -862,6 +866,17 @@ export default class Duration {
return clone(this, { values: negated }, true);
}

/**
* Removes all units with values equal to 0 from this Duration.
* @example Duration.fromObject({ years: 2, days: 0, hours: 0, minutes: 0 }).removeZeros().toObject() //=> { years: 2 }
* @return {Duration}
*/
removeZeros() {
if (!this.isValid) return this;
const vals = removeZeroes(this.values);
return clone(this, { values: vals }, true);
}

/**
* Get the years.
* @type {number}
Expand Down
30 changes: 30 additions & 0 deletions test/duration/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,36 @@ test("Duration#toHuman accepts number format opts", () => {
);
});

test("Duration#toHuman accepts hiding of zero values", () => {
expect(
Duration.fromObject({
years: 1,
months: 0,
weeks: 1,
days: 0,
hours: 4,
minutes: 0,
seconds: 6,
milliseconds: 0,
}).toHuman({ showZeros: false })
).toEqual("1 year, 1 week, 4 hours, 6 seconds");
});

test("Duration#toHuman handles undefined showZeros", () => {
expect(
Duration.fromObject({
years: 1,
months: 0,
weeks: 1,
days: 0,
hours: 4,
minutes: 0,
seconds: 6,
milliseconds: 0,
}).toHuman({ showZeros: undefined })
).toEqual("1 year, 0 months, 1 week, 0 days, 4 hours, 0 minutes, 6 seconds, 0 milliseconds");
});

test("Duration#toHuman works in differt languages", () => {
expect(dur().reconfigure({ locale: "fr" }).toHuman()).toEqual(
"1 an, 2 mois, 1 semaine, 3 jours, 4 heures, 5 minutes, 6 secondes, 7 millisecondes"
Expand Down
50 changes: 50 additions & 0 deletions test/duration/units.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,53 @@ test("Duration#valueOf value of the duration with lower and higher order units",
const dur = Duration.fromObject({ days: 2, seconds: 1 });
expect(dur.valueOf()).toBe(172801000);
});

//------
// #removeZeroes()
//-------

test("Duration#removeZeros leaves empty object if everything was zero", () => {
expect(Duration.fromObject({ years: 0, days: 0, hours: 0 }).removeZeros().toObject()).toEqual({});
});

test("Duration#removeZeros removes units with zero value", () => {
expect(
Duration.fromObject({
years: 1,
months: 0,
weeks: 1,
days: 0,
hours: 4,
minutes: 0,
seconds: 6,
milliseconds: 0,
})
.removeZeros()
.toObject()
).toEqual({
years: 1,
weeks: 1,
hours: 4,
seconds: 6,
});
});

test("Duration#removeZeros removes nothing if no value is zero", () => {
const dur = {
years: 1,
months: 2,
weeks: 1,
days: 3,
hours: 4,
minutes: 5,
seconds: 6,
milliseconds: 7,
};
expect(Duration.fromObject(dur).removeZeros().toObject()).toEqual(dur);
});

test("Duration#removeZeros maintains invalidity", () => {
const dur = Duration.invalid("because").removeZeros();
expect(dur.isValid).toBe(false);
expect(dur.invalidReason).toBe("because");
});