Skip to content

Commit e8173d1

Browse files
Merge pull request #115 from albert-gonzalez/negative-tenths-fix
Negative tenth of a second counter fix
2 parents 17d8493 + 9446d1c commit e8173d1

File tree

6 files changed

+1296
-1203
lines changed

6 files changed

+1296
-1203
lines changed

package-lock.json

Lines changed: 1231 additions & 1175 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,30 @@
3232
"author": "Albert Gonzalez",
3333
"license": "MIT",
3434
"devDependencies": {
35-
"@babel/core": "^7.12.3",
36-
"@babel/preset-env": "^7.12.1",
35+
"@babel/core": "^7.13.8",
36+
"@babel/preset-env": "^7.13.8",
3737
"babel-plugin-prismjs": "^2.0.1",
38-
"bootstrap": "^4.5.3",
39-
"chai": "^4.2.0",
40-
"eslint": "^7.13.0",
41-
"eslint-config-standard": "^16.0.1",
38+
"bootstrap": "^4.6.0",
39+
"chai": "^4.3.0",
40+
"eslint": "^7.21.0",
41+
"eslint-config-standard": "^16.0.2",
4242
"eslint-plugin-import": "^2.22.1",
4343
"eslint-plugin-node": "^11.1.0",
44-
"eslint-plugin-promise": "^4.2.1",
45-
"eslint-plugin-standard": "^4.1.0",
44+
"eslint-plugin-promise": "^4.3.1",
45+
"eslint-plugin-standard": "^5.0.0",
4646
"http-server": "~0.12.3",
4747
"jquery": "^3.5.1",
48-
"mocha": "^8.2.1",
48+
"mocha": "^8.3.0",
4949
"popper.js": "^1.16.1",
50-
"prismjs": "^1.22.0",
51-
"rollup": "^2.33.1",
50+
"prismjs": "^1.23.0",
51+
"rollup": "^2.40.0",
5252
"rollup-plugin-babel": "^4.4.0",
5353
"rollup-plugin-commonjs": "^10.1.0",
54-
"rollup-plugin-css-only": "^2.1.0",
54+
"rollup-plugin-css-only": "^3.1.0",
5555
"rollup-plugin-license": "^2.2.0",
5656
"rollup-plugin-node-resolve": "^5.2.0",
5757
"rollup-plugin-uglify": "^6.0.4",
58-
"sinon": "^9.2.1"
58+
"sinon": "^9.2.4"
5959
},
6060
"babel": {
6161
"presets": [

rollup.config.examples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313
plugins: [
1414
resolve(),
1515
commonjs(),
16-
css({ output: 'dist/examples.min.css' }),
16+
css({ output: 'examples.min.css' }),
1717
babel({
1818
exclude: 'node_modules/**'
1919
}),

src/easytimer/easytimer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,15 @@ function Timer (defaultParams = {}) {
8787
setParams(defaultParams);
8888

8989
function updateCounters (precision, roundedValue) {
90+
const unitsPerGroup = groupedUnits[precision];
9091
totalCounters[precision] = roundedValue;
9192

9293
if (precision === DAYS) {
93-
counters[precision] = roundedValue;
94+
counters[precision] = Math.abs(roundedValue);
9495
} else if (roundedValue >= 0) {
95-
counters[precision] = mod(roundedValue, groupedUnits[precision]);
96+
counters[precision] = mod(roundedValue, unitsPerGroup);
9697
} else {
97-
counters[precision] = groupedUnits[precision] - mod(roundedValue, groupedUnits[precision]);
98+
counters[precision] = mod(unitsPerGroup - mod(roundedValue, unitsPerGroup), unitsPerGroup);
9899
}
99100
}
100101

src/examples/examples.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ $(function () {
1515
});
1616

1717
$(function () {
18-
var timer = new Timer();
18+
const timer = new Timer();
1919
timer.start();
2020
timer.addEventListener('secondsUpdated', function (e) {
2121
$('#basicUsage').html(timer.getTimeValues().toString());
2222
});
2323
});
2424

2525
$(function () {
26-
var timer = new Timer();
26+
const timer = new Timer();
2727
timer.start({ precision: 'seconds' });
2828
timer.addEventListener('secondsUpdated', function (e) {
2929
$('#gettingValuesExample .days').html(timer.getTimeValues().days);
@@ -41,7 +41,7 @@ $(function () {
4141
});
4242

4343
$(function () {
44-
var timer = new Timer();
44+
const timer = new Timer();
4545

4646
$('#chronoExample .startButton').click(function () {
4747
timer.start();
@@ -73,7 +73,7 @@ $(function () {
7373
});
7474

7575
$(function () {
76-
var timer = new Timer();
76+
const timer = new Timer();
7777
timer.start({ precision: 'seconds', startValues: { seconds: 90 }, target: { seconds: 120 } });
7878
$('#startValuesAndTargetExample .values').html(timer.getTimeValues().toString());
7979
timer.addEventListener('secondsUpdated', function (e) {
@@ -86,7 +86,7 @@ $(function () {
8686
});
8787

8888
$(function () {
89-
var timer = new Timer();
89+
const timer = new Timer();
9090
timer.start({ countdown: true, startValues: { seconds: 30 } });
9191
$('#countdownExample .values').html(timer.getTimeValues().toString());
9292
timer.addEventListener('secondsUpdated', function (e) {
@@ -98,7 +98,7 @@ $(function () {
9898
});
9999

100100
$(function () {
101-
var timer = new Timer();
101+
const timer = new Timer();
102102
timer.start({
103103
callback: function (timer) {
104104
$('#callbackExample .values').html(
@@ -109,15 +109,15 @@ $(function () {
109109
});
110110

111111
$(function () {
112-
var timer = new Timer();
112+
const timer = new Timer();
113113
timer.start({ precision: 'secondTenths' });
114114
timer.addEventListener('secondTenthsUpdated', function (e) {
115115
$('#secondTenthsExample .values').html(timer.getTimeValues().toString(['hours', 'minutes', 'seconds', 'secondTenths']));
116116
});
117117
});
118118

119119
$(function () {
120-
var timer = new Timer({ countdown: true, startValues: { seconds: 5 } });
120+
const timer = new Timer({ countdown: true, startValues: { seconds: 5 } });
121121

122122
timer.start({ startValues: { seconds: 30 }, target: { seconds: 10 } });
123123
$('#defaultParamsExample .values').html(timer.getTimeValues().toString());

test/timer.spec.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ describe('timer.js', function () {
2626
assert.deepEqual(totalTimes.seconds, totalTimesValues[1]);
2727
assert.deepEqual(totalTimes.minutes, totalTimesValues[2]);
2828
assert.deepEqual(totalTimes.hours, totalTimesValues[3]);
29-
assert.deepEqual(totalTimes.days, timesValues[4]);
29+
assert.deepEqual(totalTimes.days, totalTimesValues[4]);
3030
}
3131

32-
function assertEventTriggered (timer, event, millisecons, timesTriggered) {
32+
function assertEventTriggered (timer, event, milliseconds, timesTriggered) {
3333
const callback = sinon.spy();
3434
timer.addEventListener(event, callback);
35-
clock.tick(millisecons);
35+
clock.tick(milliseconds);
3636
sinon.assert.callCount(callback, timesTriggered);
3737
assert.equal(timer, callback.args[0][0].detail.timer);
3838
}
@@ -135,6 +135,15 @@ describe('timer.js', function () {
135135
clock.tick(100);
136136
assert(params.callback.called);
137137
});
138+
139+
it('should allow negative values and update the counter correctly', function () {
140+
const startValues = { seconds: -10 };
141+
timer.stop();
142+
timer.start({ startValues: startValues, precision: 'secondTenths' });
143+
144+
assertEventTriggered(timer, 'secondTenthsUpdated', 1000, 10);
145+
assertTimes(timer, [0, 9, 0, 0, 0], [-90, -9, -0, -0, -0]);
146+
});
138147
});
139148

140149
describe('with seconds precision', function () {
@@ -168,6 +177,15 @@ describe('timer.js', function () {
168177
clock.tick(1000);
169178
assert(params.callback.called);
170179
});
180+
181+
it('should allow negative values and update the counter correctly', function () {
182+
const startValues = { seconds: -10 };
183+
timer.stop();
184+
timer.start({ startValues, precision: 'seconds' });
185+
186+
assertEventTriggered(timer, 'secondsUpdated', 11000, 11);
187+
assertTimes(timer, [0, 1, 0, 0, 0], [10, 1, 0, 0, 0]);
188+
});
171189
});
172190

173191
describe('with minutes precision', function () {
@@ -201,6 +219,15 @@ describe('timer.js', function () {
201219
clock.tick(60000);
202220
sinon.assert.callCount(params.callback, 1);
203221
});
222+
223+
it('should allow negative values and update the counter correctly', function () {
224+
const startValues = { minutes: -2, seconds: -35 };
225+
timer.stop();
226+
timer.start({ startValues, precision: 'minutes' });
227+
228+
assertEventTriggered(timer, 'minutesUpdated', 120000, 2);
229+
assertTimes(timer, [0, 35, 0, 0, 0], [-350, -35, -0, -0, -0]);
230+
});
204231
});
205232

206233
describe('with hours precision', function () {
@@ -229,6 +256,15 @@ describe('timer.js', function () {
229256
clock.tick(3600000);
230257
sinon.assert.callCount(params.callback, 1);
231258
});
259+
260+
it('should allow negative values and update the counter correctly', function () {
261+
const startValues = { hours: -1, minutes: -14 };
262+
timer.stop();
263+
timer.start({ startValues, precision: 'hours' });
264+
265+
assertEventTriggered(timer, 'hoursUpdated', 3600000, 1);
266+
assertTimes(timer, [0, 0, 14, 0, 0], [-8400, -840, -14, -0, -0]);
267+
});
232268
});
233269
});
234270

0 commit comments

Comments
 (0)