Skip to content

Commit 396ce54

Browse files
committed
bug fix when returning value object and dates are equal
1 parent 7d34bd5 commit 396ce54

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

moment-precise-range.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ if (typeof moment === "undefined" && typeof require === 'function') {
4949
return result.join(STRINGS.delimiter);
5050
}
5151

52+
function buildValueObject(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff, firstDateWasLater) {
53+
return {
54+
"years" : yDiff,
55+
"months" : mDiff,
56+
"days" : dDiff,
57+
"hours" : hourDiff,
58+
"minutes" : minDiff,
59+
"seconds" : secDiff,
60+
"firstDateWasLater" : firstDateWasLater
61+
}
62+
}
5263
moment.fn.preciseDiff = function(d2, returnValueObject) {
5364
return moment.preciseDiff(this, d2, returnValueObject);
5465
};
@@ -59,7 +70,11 @@ if (typeof moment === "undefined" && typeof require === 'function') {
5970
m1.add(m2.utcOffset() - m1.utcOffset(), 'minutes'); // shift timezone of m1 to m2
6071

6172
if (m1.isSame(m2)) {
62-
return STRINGS.nodiff;
73+
if (returnValueObject) {
74+
return buildValueObject(0, 0, 0, 0, 0, 0, false);
75+
} else {
76+
return STRINGS.nodiff;
77+
}
6378
}
6479
if (m1.isAfter(m2)) {
6580
var tmp = m1;
@@ -104,15 +119,7 @@ if (typeof moment === "undefined" && typeof require === 'function') {
104119
}
105120

106121
if (returnValueObject) {
107-
return {
108-
"years" : yDiff,
109-
"months" : mDiff,
110-
"days" : dDiff,
111-
"hours" : hourDiff,
112-
"minutes" : minDiff,
113-
"seconds" : secDiff,
114-
"firstDateWasLater" : firstDateWasLater
115-
};
122+
return buildValueObject(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff, firstDateWasLater);
116123
} else {
117124
return buildStringFromValues(yDiff, mDiff, dDiff, hourDiff, minDiff, secDiff);
118125
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "moment-precise-range-plugin",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "This is a plugin for the moment.js JavaScript library, to display date/time ranges precisely, in a human-readable format.",
55
"main": "moment-precise-range.js",
66
"files": [
@@ -18,7 +18,7 @@
1818
"url": "git+https://github.com/codebox/moment-precise-range.git"
1919
},
2020
"tonicExampleFilename" : "example/example.js",
21-
"author": "Rob Dawson <rob@codebox.org.uk> (http://codebox.org.uk)",
21+
"author": "Rob Dawson <rob@codebox.net> (https://codebox.net)",
2222
"license": "MIT",
2323
"bugs": {
2424
"url": "https://github.com/codebox/moment-precise-range/issues"

test/tests/moment-precise-range-test.js

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ describe("preciseDiff", function() {
139139
it("all values", function () {
140140
test('2001-11-12 13:01:43+0000', '2014-02-01 01:03:01+0000', 12, 2, 19, 12, 1, 18, false);
141141
});
142+
it("equal values", function () {
143+
test('2001-11-12 13:01:43+0000', '2001-11-12 13:01:43+0000', 0, 0, 0, 0, 0, 0, false);
144+
});
142145
it("multiple values", function() {
143146
test('2013-10-21 10:15:40+0000', '2014-02-02 01:01:01+0000', 0, 3, 11, 15, 45, 21, false);
144147
test('2013-12-31 23:58:10+0000', '2014-01-01 00:02:08+0000', 0, 0, 0, 0, 3, 58, false);

0 commit comments

Comments
 (0)