@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
268 lines • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Duration = void 0;
var weeksToDays = 7;
var daysToHours = 24;
var hoursToMinutes = 60;
var minutesToSeconds = 60;
var secondsToMilliseconds = 1000;
var weeksToHours = weeksToDays * daysToHours;
var weeksToMinutes = weeksToHours * hoursToMinutes;
var weeksToSeconds = weeksToMinutes * minutesToSeconds;
var weeksToMilliseconds = weeksToSeconds * secondsToMilliseconds;
var daysToWeeks = 1 / weeksToDays;
var daysToMinutes = daysToHours * hoursToMinutes;
var daysToSeconds = daysToMinutes * minutesToSeconds;
var daysToMilliseconds = daysToSeconds * secondsToMilliseconds;
var hoursToWeeks = 1 / weeksToHours;
var hoursToDays = 1 / daysToHours;
var hoursToSeconds = hoursToMinutes * minutesToSeconds;
var hoursToMilliseconds = hoursToSeconds * secondsToMilliseconds;
var minutesToWeeks = 1 / weeksToMinutes;
var minutesToDays = 1 / daysToMinutes;
var minutesToHours = 1 / hoursToMinutes;
var minutesToMilliseconds = minutesToSeconds * secondsToMilliseconds;
var secondsToWeeks = 1 / weeksToSeconds;
var secondsToDays = 1 / daysToSeconds;
var secondsToHours = 1 / hoursToSeconds;
var secondsToMinutes = 1 / minutesToSeconds;
var millisecondsToWeeks = 1 / weeksToMilliseconds;
var millisecondsToDays = 1 / daysToMilliseconds;
var millisecondsToHours = 1 / hoursToMilliseconds;
var millisecondsToMinutes = 1 / minutesToMilliseconds;
var millisecondsToSeconds = 1 / secondsToMilliseconds;
/**
* A class that represents a duration of time.
*/
var Duration = /** @class */ (function () {
/**
* Create a new Duration for the provided amount of milliseconds.
* @param value The numeric value of the Duration.
* @param units The units of the value for the Duration.
*/
function Duration(value, units) {
this.value = value;
this.units = units;
}
/**
* Create a new Duration object with the provided number of milliseconds.
* @param value The number of milliseconds in the Duration.
*/
Duration.milliseconds = function (value) {
return new Duration(value, "Milliseconds");
};
/**
* Create a new Duration object with the provided number of seconds.
* @param value The number of seconds in the Duration.
*/
Duration.seconds = function (value) {
return new Duration(value, "Seconds");
};
/**
* Create a new Duration object with the provided number of minutes.
* @param value The number of minutes in the Duration.
*/
Duration.minutes = function (value) {
return new Duration(value, "Minutes");
};
/**
* Create a new Duration object with the provided number of hours.
* @param value The number of hours in the Duration.
*/
Duration.hours = function (value) {
return new Duration(value, "Hours");
};
/**
* Create a new Duration object with the provided number of days.
* @param value The number of days in the Duration.
*/
Duration.days = function (value) {
return new Duration(value, "Days");
};
/**
* Create a new Duration object with the provided number of weeks.
* @param value The number of weeks in the Duration.
*/
Duration.weeks = function (value) {
return new Duration(value, "Weeks");
};
/**
* Convert this Duration to the provided DurationUnits.
*/
Duration.prototype.convertTo = function (targetUnits) {
var result = this;
switch (this.units) {
case "Milliseconds":
switch (targetUnits) {
case "Seconds":
result = new Duration(this.value * millisecondsToSeconds, targetUnits);
break;
case "Minutes":
result = new Duration(this.value * millisecondsToMinutes, targetUnits);
break;
case "Hours":
result = new Duration(this.value * millisecondsToHours, targetUnits);
break;
case "Days":
result = new Duration(this.value * millisecondsToDays, targetUnits);
break;
case "Weeks":
result = new Duration(this.value * millisecondsToWeeks, targetUnits);
break;
}
break;
case "Seconds":
switch (targetUnits) {
case "Milliseconds":
result = new Duration(this.value * secondsToMilliseconds, targetUnits);
break;
case "Minutes":
result = new Duration(this.value * secondsToMinutes, targetUnits);
break;
case "Hours":
result = new Duration(this.value * secondsToHours, targetUnits);
break;
case "Days":
result = new Duration(this.value * secondsToDays, targetUnits);
break;
case "Weeks":
result = new Duration(this.value * secondsToWeeks, targetUnits);
break;
}
break;
case "Minutes":
switch (targetUnits) {
case "Milliseconds":
result = new Duration(this.value * minutesToMilliseconds, targetUnits);
break;
case "Seconds":
result = new Duration(this.value * minutesToSeconds, targetUnits);
break;
case "Hours":
result = new Duration(this.value * minutesToHours, targetUnits);
break;
case "Days":
result = new Duration(this.value * minutesToDays, targetUnits);
break;
case "Weeks":
result = new Duration(this.value * minutesToWeeks, targetUnits);
break;
}
break;
case "Hours":
switch (targetUnits) {
case "Milliseconds":
result = new Duration(this.value * hoursToMilliseconds, targetUnits);
break;
case "Seconds":
result = new Duration(this.value * hoursToSeconds, targetUnits);
break;
case "Minutes":
result = new Duration(this.value * hoursToMinutes, targetUnits);
break;
case "Days":
result = new Duration(this.value * hoursToDays, targetUnits);
break;
case "Weeks":
result = new Duration(this.value * hoursToWeeks, targetUnits);
break;
}
break;
case "Days":
switch (targetUnits) {
case "Milliseconds":
result = new Duration(this.value * daysToMilliseconds, targetUnits);
break;
case "Seconds":
result = new Duration(this.value * daysToSeconds, targetUnits);
break;
case "Minutes":
result = new Duration(this.value * daysToMinutes, targetUnits);
break;
case "Hours":
result = new Duration(this.value * daysToHours, targetUnits);
break;
case "Weeks":
result = new Duration(this.value * daysToWeeks, targetUnits);
break;
}
break;
case "Weeks":
switch (targetUnits) {
case "Milliseconds":
result = new Duration(this.value * weeksToMilliseconds, targetUnits);
break;
case "Seconds":
result = new Duration(this.value * weeksToSeconds, targetUnits);
break;
case "Minutes":
result = new Duration(this.value * weeksToMinutes, targetUnits);
break;
case "Hours":
result = new Duration(this.value * weeksToHours, targetUnits);
break;
case "Days":
result = new Duration(this.value * weeksToDays, targetUnits);
break;
}
break;
}
return result;
};
/**
* Convert this Duration to milliseconds.
*/
Duration.prototype.toMilliseconds = function () {
return this.convertTo("Milliseconds");
};
/**
* Convert this Duration to seconds.
*/
Duration.prototype.toSeconds = function () {
return this.convertTo("Seconds");
};
/**
* Convert this Duration to minutes.
*/
Duration.prototype.toMinutes = function () {
return this.convertTo("Minutes");
};
/**
* Convert this Duration to hours.
*/
Duration.prototype.toHours = function () {
return this.convertTo("Hours");
};
/**
* Convert this Duration to days.
*/
Duration.prototype.toDays = function () {
return this.convertTo("Days");
};
/**
* Convert this Duration to weeks.
*/
Duration.prototype.toWeeks = function () {
return this.convertTo("Weeks");
};
/**
* Get the string representation of this Duration.
*/
Duration.prototype.toString = function () {
return this.value + " " + this.units;
};
Duration.prototype.plus = function (rhs) {
return rhs instanceof Duration
? new Duration(this.value + rhs.convertTo(this.units).value, this.units)
: new Date(rhs.valueOf() + this.toMilliseconds().value);
};
/**
* Get the current Date plus this Duration.
*/
Duration.prototype.fromNow = function () {
return this.plus(new Date());
};
return Duration;
}());
exports.Duration = Duration;
//# sourceMappingURL=duration.js.map