Skip to content

Commit

Permalink
Durations are considered equal with extra zero units. Fixes #809 (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesDebunne committed Oct 21, 2020
1 parent ea641e7 commit 5ab7205
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,14 @@ export default class Duration {
return false;
}

function eq(v1, v2) {
// Consider 0 and undefined as equal
if (v1 === undefined || v1 === 0) return v2 === undefined || v2 === 0;
return v1 === v2;
}

for (const u of orderedUnits) {
if (this.values[u] !== other.values[u]) {
if (!eq(this.values[u], other.values[u])) {
return false;
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/duration/equality.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ test("equals identically constructed but one has sting type values", () => {
expect(l1.equals(l2)).toBe(true);
});

// #809
test("equals with extra zero units", () => {
const l1 = Duration.fromObject({ years: 5, days: 6 }),
l2 = Duration.fromObject({ years: 5, days: 6, minutes: 0, seconds: -0 });
expect(l1.equals(l2)).toBe(true);
expect(l2.equals(l1)).toBe(true);
});

test("does not equal an invalid duration", () => {
const l1 = Duration.fromObject({ years: 5, days: 6 }),
l2 = Duration.invalid("because");
Expand Down

0 comments on commit 5ab7205

Please sign in to comment.