UNPKG

@technobuddha/library

Version:
265 lines (264 loc) 8.7 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeSpan = void 0; /* eslint-disable @typescript-eslint/unified-signatures */ var isString_1 = __importDefault(require("lodash/isString")); var constants_1 = require("../constants"); /** * Store and manipulate a duration of time */ var TimeSpan = /** @class */ (function () { function TimeSpan() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } var sign = 1; var d = 0; var h = 0; var m = 0; var s = 0; var ms = 0; switch (args.length) { case 0: { d = h = m = s = ms = 0; break; } case 1: { if (isString_1.default(args[0])) { var text = args[0]; if (text.startsWith('-')) { sign = -1; text = text.slice(1); } var matches = /^(\d{1,2})(?::(\d\d)(?::(\d\d)(?::(\d\d))?)?)?(?:\.(\d{1,3}))?$/u.exec(text); if (matches) { d = Number(matches[1]); h = Number(matches[2]); m = Number(matches[3]); s = Number(matches[4]); ms = matches[5] ? Math.floor(Number("0." + matches[5]) * 1000) : Number.NaN; while (Number.isNaN(s)) { s = m; m = h; h = d; d = 0; } } else { d = h = m = s = ms = 0; } } else { ms = args[0]; d = h = m = s = 0; } break; } case 3: { d = 0; h = args[0]; m = args[1]; s = args[2]; ms = 0; break; } default: { d = args[0]; h = args[1]; m = args[2]; s = args[3]; ms = args[4]; } } this.clicks = sign * ((d ? d * constants_1.ticksPerDay : 0) + (h ? h * constants_1.ticksPerHour : 0) + (m ? m * constants_1.ticksPerMinute : 0) + (s ? s * constants_1.ticksPerSecond : 0) + (ms ? ms : 0)); } Object.defineProperty(TimeSpan.prototype, "days", { /** * Get the days portion */ get: function () { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / constants_1.ticksPerDay); }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "hours", { /** * Get the hours portion */ get: function () { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / constants_1.ticksPerHour) % constants_1.hoursPerDay; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "minutes", { /** * Get the minutes portion */ get: function () { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / constants_1.ticksPerMinute) % constants_1.minutesPerHour; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "seconds", { /** * Get the seconds portion */ get: function () { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks) / constants_1.ticksPerSecond) % constants_1.secondsPerMinute; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "milliseconds", { /** * Get the milliseconds portion */ get: function () { return Math.sign(this.clicks) * Math.floor(Math.abs(this.clicks)) % constants_1.ticksPerSecond; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "ticks", { /** * Get the total number of ticks (milliseconds) */ get: function () { return this.clicks; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "totalDays", { /** * Get the total number of days */ get: function () { return this.clicks / constants_1.ticksPerDay; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "totalHours", { /** * Get the total number of hours */ get: function () { return this.clicks / constants_1.ticksPerHour; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "totalMinutes", { /** * Get the total number of minutes */ get: function () { return this.clicks / constants_1.ticksPerMinute; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "totalSeconds", { /** * Get the total number of seconds */ get: function () { return this.clicks / constants_1.ticksPerSecond; }, enumerable: false, configurable: true }); Object.defineProperty(TimeSpan.prototype, "totalMilliseconds", { /** * Get the total number of milliseconds */ get: function () { return this.clicks; }, enumerable: false, configurable: true }); /** * Format the timespan using a mask * * @param mask The mask * @returns the formatted TimeSpan */ TimeSpan.prototype.format = function (mask) { if (mask) { var D_1 = this.days; var S_1 = this.seconds; var M_1 = this.minutes; var H_1 = this.hours; var F_1 = this.milliseconds; var flags_1 = { d: D_1.toString(), dd: D_1.toString().padStart(2, '0'), m: M_1.toString(), mm: M_1.toString().padStart(2, '0'), h: H_1.toString(), hh: H_1.toString().padStart(2, '0'), s: S_1.toString(), ss: S_1.toString().padStart(2, '0'), f: F_1.toString().padStart(3, '0'), ff: F_1.toString().padStart(3, '0'), }; return mask.replace(/[dmhsf]{1,2}|"[^"]*"|'[^']*'/ug, function ($0) { return ($0 in flags_1) ? flags_1[$0] : $0.slice(1, -1); }); } var D = this.days; var H = this.hours; var M = this.minutes; var S = this.seconds; var F = this.milliseconds; var str; if (D !== 0) str = D + "d" + H.toString().padStart(2, '0') + ":" + M.toString().padStart(2, '0') + ":" + S.toString().padStart(2, '0'); else if (H !== 0) str = H + ":" + M.toString().padStart(2, '0') + ":" + S.toString().padStart(2, '0'); else str = M + ":" + S.toString().padStart(2, '0'); if (F) str += "." + F.toString().padStart(3, '0'); return str; }; /** * Convert the TimeSpan to a string * * @returns formatted string */ TimeSpan.prototype.toString = function () { return this.format(); }; /** * Add two timespans * * @param other TimeSpan to add to this * @returns a TimeSpan that is the sum of two timespans */ TimeSpan.prototype.add = function (other) { return new TimeSpan(this.ticks + other.ticks); }; /** * Compare two TimeSpans * * @param t1 First TimeSpan * @param t2 Second TimeSpan * @returns -1 if the first time span is less then the second, 0 if they are equal, 1 if the first is greater */ TimeSpan.compare = function (t1, t2) { return t1.ticks === t2.ticks ? 0 : Math.abs(t1.ticks) > Math.abs(t2.ticks) ? 1 : -1; }; return TimeSpan; }()); exports.TimeSpan = TimeSpan; exports.default = TimeSpan;