Skip to content

Commit

Permalink
Adding a conditional & assertion for a diff() case
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Oct 5, 2023
1 parent bc45b96 commit 147438b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion dist/precise.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.0.0
* @version 4.0.1
*/
'use strict';

Expand All @@ -22,6 +22,10 @@ class Precise {
}

diff (ms = false) {
if (this.started === BIG_INT_NEG_1) {
throw new Error(notStarted);
}

if (this.stopped === BIG_INT_NEG_1) {
throw new Error(notStopped);
}
Expand Down
6 changes: 5 additions & 1 deletion dist/precise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @copyright 2023 Jason Mulligan <jason.mulligan@avoidwork.com>
* @license BSD-3-Clause
* @version 4.0.0
* @version 4.0.1
*/
import {hrtime}from'node:process';const hasStarted = "Timer has been started";
const hasStopped = "Timer has been stopped";
Expand All @@ -16,6 +16,10 @@ const BIG_INT_NEG_1 = BigInt(-1);class Precise {
}

diff (ms = false) {
if (this.started === BIG_INT_NEG_1) {
throw new Error(notStarted);
}

if (this.stopped === BIG_INT_NEG_1) {
throw new Error(notStopped);
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "precise",
"description": "Precision timer for nanosecond differences",
"version": "4.0.0",
"version": "4.0.1",
"homepage": "http://avoidwork.github.io/precise",
"author": "Jason Mulligan <jason.mulligan@avoidwork.com>",
"repository": {
Expand Down
4 changes: 4 additions & 0 deletions src/precise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export class Precise {
}

diff (ms = false) {
if (this.started === BIG_INT_NEG_1) {
throw new Error(notStarted);
}

if (this.stopped === BIG_INT_NEG_1) {
throw new Error(notStopped);
}
Expand Down
4 changes: 3 additions & 1 deletion test/precise_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ describe("Testing stopping state", function () {
const timer = this.timer;
assert.throws(function () { timer.diff(); }, Error, "Should be an 'Error'");
assert.throws(function () { timer.stop(); }, Error, "Should be an 'Error'");
this.timer.start().stop();
this.timer.start();
assert.throws(function () { timer.diff(); }, Error, "Should be an 'Error'");
this.timer.stop();
assert.notEqual(this.timer.started, BIG_INT_NEG_1, "Shouldn't be '-1n'");
assert.notEqual(this.timer.started, BIG_INT_NEG_1, "Shouldn't be '-1n'");
assert.throws(function () { timer.stop(); }, Error, "Should be an 'Error'");
Expand Down

0 comments on commit 147438b

Please sign in to comment.