boss-timer
Version:
Timer for node
126 lines • 4.21 kB
JavaScript
function calculateDifference(t1, t2) {
return [t1[0] - t2[0], t1[1] - t2[1]];
}
function addTimes(t1, t2) {
return [t1[0] + t2[0], t1[1] + t2[1]];
}
var Timer = (function () {
/**
* Constructor for the class
* @param {boolean} start? - Optional: starts the time on creation of the class
* @returns {Timer} this returns itself to allow command chaining
*/
function Timer(start) {
this.version = "0.1.0";
//
this.start_time = null;
this.intervals = [];
this.total = [0, 0];
this.end_time = null;
this.split_times = [];
this.debug = false;
this.running = false;
if (start !== false) {
this.start_time = process.hrtime();
this.running = true;
}
}
/**
* Starts the timer
* @returns {Timer} this returns itself to allow command chaining
*/
Timer.prototype.start = function () {
if (!this.running) {
this.start_time = process.hrtime();
this.running = true;
}
else {
if (this.debug)
console.log('[boss-timer] Timer already running');
}
return this;
};
/**
* Stops the timer
* @returns {Timer} this returns itself to allow command chaining
*/
Timer.prototype.stop = function () {
if (!this.running) {
if (this.debug) {
console.log('[boss-timer] Timer is not running');
}
}
else {
this.end_time = process.hrtime();
}
this.total = calculateDifference(this.end_time, this.start_time);
this.end_time = null;
this.start_time = null;
this.running = false;
return this;
};
/**
* Creates a split time in the timer
* @returns {Timer} this returns itself to allow command chaining
*/
Timer.prototype.split = function () {
this.split_times.push(process.hrtime());
return this;
};
/**
* Returns the elapsed time between when the timer was started and the end time, or the time elapsed so far
* @param {number} split? array index of the the split time from which to calculate the elapsed time from the start time
* @returns {number} time in miliseconds that have elapsed
*/
Timer.prototype.elapsed = function (split) {
var time = [];
if (split && this.split_times.length > 0) {
if (split === -1) {
time = calculateDifference(this.split_times[this.split_times.length - 1], this.start_time);
}
else {
if (typeof this.split_times[split] !== 'undefined') {
time = calculateDifference(this.split_times[split], this.start_time);
}
else {
if (this.debug) {
console.log('[boss-timer] Split time of index:"' + split + '" not found!');
}
}
}
}
else if (split && this.split_times.length) {
if (this.debug) {
console.log('[boss-timer] No split times');
}
}
else if (!this.running) {
time = this.total;
}
else {
time = addTimes(this.total, process.hrtime(this.start_time));
}
return time;
};
/**
* Renders the time in a human redable format
* @param {Array<number>} time? time to render
* @returns {string} returns the time as a string with the correct time denominator
*/
Timer.prototype.render = function (time) {
var timeToRender = time || this.elapsed();
var ns = timeToRender[0] * 1e9 + timeToRender[1];
var ms = ns / 1e6;
var s = ns / 1e9;
if (ns < 1000) {
return ns + 'ns';
}
if (ms < 1000) {
return ms.toFixed(2) + 'ms';
}
return s.toFixed(2) + 's';
};
return Timer;
})();
exports.Timer = Timer;
//# sourceMappingURL=index.js.map