Skip to content

Commit 3c4e42e

Browse files
committed
Test and simplify convert24to12, including making it resistant to out of bounds values.
1 parent ea4252e commit 3c4e42e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,9 +1814,7 @@
18141814
* Returns 12 hour without leading 0
18151815
*/
18161816
var convert24to12 = function(hour) {
1817-
if (hour > 12) {
1818-
hour = hour - 12;
1819-
}
1817+
hour %= 12;
18201818

18211819
if (hour === 0) {
18221820
hour = 12;
@@ -2112,7 +2110,8 @@
21122110
*/
21132111
$.timepicker.util = {
21142112
_extendRemove: extendRemove,
2115-
_isEmptyObject: isEmptyObject
2113+
_isEmptyObject: isEmptyObject,
2114+
_convert24to12: convert24to12
21162115
};
21172116

21182117
/*

test/jquery-ui-timepicker-addon_spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,29 @@ describe('datetimepicker', function() {
6565
expect(util._isEmptyObject(testObject)).toBe(true);
6666
})
6767
});
68+
69+
describe('convert24to12', function() {
70+
it('should return the value for a non-zero value less than 12', function() {
71+
var expectedHour = 6;
72+
73+
expect(util._convert24to12(expectedHour)).toBe("" + expectedHour);
74+
});
75+
76+
it('should return 12 hours less if the value is greater than 12 and less than 24', function() {
77+
var expectedHour = 7;
78+
79+
expect(util._convert24to12(expectedHour + 12)).toBe("" + expectedHour);
80+
});
81+
82+
it('should return 12 if the normalized value is 0', function() {
83+
expect(util._convert24to12(0)).toBe('12');
84+
});
85+
86+
it('should normalize values that are clearly out of the expected range', function() {
87+
var expectedValue = 11;
88+
89+
expect(util._convert24to12(expectedValue + 12 * 3)).toBe("" + expectedValue);
90+
})
91+
});
6892
});
6993
});

0 commit comments

Comments
 (0)