From 37d40e0aa3cc02f4a852c81f9389eba30d82a9c8 Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:18:21 -0500 Subject: [PATCH 1/7] swithc reset approach Signed-off-by: Jason Ray --- lib/Stopwatch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Stopwatch.js b/lib/Stopwatch.js index da1d258..982ef69 100644 --- a/lib/Stopwatch.js +++ b/lib/Stopwatch.js @@ -49,9 +49,9 @@ Stopwatch.prototype.start = function () { self._verifyState([STATES.STOPPED, STATES.INIT], "Cannot start a stopwatch that is currently running"); + self.reset() self._state = STATES.RUNNING; self.startTime = now(); - self.stopTime = 0; }; Stopwatch.prototype.stop = function () { From 87c766d1952fce9cc583811d67cf815abccaf161 Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:18:38 -0500 Subject: [PATCH 2/7] support read with units Signed-off-by: Jason Ray --- lib/Stopwatch.js | 6 +++++- test/stopwatch-test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Stopwatch.js b/lib/Stopwatch.js index 982ef69..9d7522e 100644 --- a/lib/Stopwatch.js +++ b/lib/Stopwatch.js @@ -131,7 +131,7 @@ Stopwatch.prototype.splitTime = function () { return self._calculateDelta(self.startTime, self.stopTime); }; -Stopwatch.prototype.read = Stopwatch.prototype.time = function (precision) { +Stopwatch.prototype.read = Stopwatch.prototype.time = function (precision, units) { const self = this; const startTime = self.startTime; let delta = null; @@ -150,6 +150,10 @@ Stopwatch.prototype.read = Stopwatch.prototype.time = function (precision) { delta = delta + self._StartTimeDelta; } + // is in `ms` by default + if (units === 's') + delta = delta / 1000 + if (precision || precision === 0) { delta = delta.toFixed(precision); } diff --git a/test/stopwatch-test.js b/test/stopwatch-test.js index 0f6bad5..0cfcac4 100644 --- a/test/stopwatch-test.js +++ b/test/stopwatch-test.js @@ -332,6 +332,21 @@ describe("stopwatch", function () { }, testtime); }); + it("read with units", function (done) { + const time_ms = 2000; + const time_s = 2; + + const stopwatch = new Stopwatch(); + stopwatch.start(); + setTimeout(function () { + const delta = stopwatch.read('s'); + verifyDelta(time_ms, stopwatch.read(1,'ms'), defaultPrecision); + verifyDelta(time_s, stopwatch.read(1,'s'), defaultPrecision); + done(); + }, time_ms); + }); + + describe("suspend / resume", function () { it("unable to suspend a new stopwatch", function () { const stopwatch = new Stopwatch(); From 745ba713634a2678736a0da4d693643b92f3ffb1 Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:25:58 -0500 Subject: [PATCH 3/7] codacy finding Signed-off-by: Jason Ray --- lib/Stopwatch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Stopwatch.js b/lib/Stopwatch.js index 9d7522e..f63a676 100644 --- a/lib/Stopwatch.js +++ b/lib/Stopwatch.js @@ -151,7 +151,7 @@ Stopwatch.prototype.read = Stopwatch.prototype.time = function (precision, units } // is in `ms` by default - if (units === 's') + if (units === "s") delta = delta / 1000 if (precision || precision === 0) { From 34f4d44e8a4a5918ce40f4ab6b952f441cb3129a Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:26:19 -0500 Subject: [PATCH 4/7] codacy finding Signed-off-by: Jason Ray --- lib/Stopwatch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Stopwatch.js b/lib/Stopwatch.js index f63a676..9807af9 100644 --- a/lib/Stopwatch.js +++ b/lib/Stopwatch.js @@ -49,7 +49,7 @@ Stopwatch.prototype.start = function () { self._verifyState([STATES.STOPPED, STATES.INIT], "Cannot start a stopwatch that is currently running"); - self.reset() + self.reset(); self._state = STATES.RUNNING; self.startTime = now(); }; From 29ff5c269d91f9b0942f8da1ce4e13d28d0041da Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:26:49 -0500 Subject: [PATCH 5/7] codacy finding Signed-off-by: Jason Ray --- lib/Stopwatch.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Stopwatch.js b/lib/Stopwatch.js index 9807af9..de4537f 100644 --- a/lib/Stopwatch.js +++ b/lib/Stopwatch.js @@ -151,8 +151,9 @@ Stopwatch.prototype.read = Stopwatch.prototype.time = function (precision, units } // is in `ms` by default - if (units === "s") - delta = delta / 1000 + if (units === "s") { + delta = delta / 1000; + } if (precision || precision === 0) { delta = delta.toFixed(precision); From d7026f56e2870b7d84a0e675925a58b96541c3eb Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:27:35 -0500 Subject: [PATCH 6/7] codacy finding Signed-off-by: Jason Ray --- test/stopwatch-test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/stopwatch-test.js b/test/stopwatch-test.js index 0cfcac4..9cfe450 100644 --- a/test/stopwatch-test.js +++ b/test/stopwatch-test.js @@ -333,17 +333,17 @@ describe("stopwatch", function () { }); it("read with units", function (done) { - const time_ms = 2000; - const time_s = 2; + const timeMs = 2000; + const timeS = 2; const stopwatch = new Stopwatch(); stopwatch.start(); setTimeout(function () { const delta = stopwatch.read('s'); - verifyDelta(time_ms, stopwatch.read(1,'ms'), defaultPrecision); - verifyDelta(time_s, stopwatch.read(1,'s'), defaultPrecision); + verifyDelta(timeMs, stopwatch.read(1,'ms'), defaultPrecision); + verifyDelta(timeS, stopwatch.read(1,'s'), defaultPrecision); done(); - }, time_ms); + }, timeMs); }); From 3620e969e96861a4df0155d81b63ee0e093914f5 Mon Sep 17 00:00:00 2001 From: Jason Ray Date: Sat, 18 Nov 2023 13:28:02 -0500 Subject: [PATCH 7/7] codacy finding Signed-off-by: Jason Ray --- test/stopwatch-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/stopwatch-test.js b/test/stopwatch-test.js index 9cfe450..2a8f992 100644 --- a/test/stopwatch-test.js +++ b/test/stopwatch-test.js @@ -339,9 +339,9 @@ describe("stopwatch", function () { const stopwatch = new Stopwatch(); stopwatch.start(); setTimeout(function () { - const delta = stopwatch.read('s'); - verifyDelta(timeMs, stopwatch.read(1,'ms'), defaultPrecision); - verifyDelta(timeS, stopwatch.read(1,'s'), defaultPrecision); + const delta = stopwatch.read("s"); + verifyDelta(timeMs, stopwatch.read(1,"ms"), defaultPrecision); + verifyDelta(timeS, stopwatch.read(1,"s"), defaultPrecision); done(); }, timeMs); });