@hypst/time-beat-format
Version:
Formatting time, notes and beats.
146 lines (145 loc) • 5.51 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const definitions_1 = require("./definitions");
class HMSTime {
constructor(...args) {
this._hour = 0;
this._minute = 0;
this._second = 0;
this._millisecond = 0;
this._option = {};
if (args.length == 0) {
}
else if (typeof (args[0]) == 'string') {
this.parseString(args[0]);
this._option = args[1];
}
else if (typeof (args[0]) == 'number' && typeof (args[1]) == 'number') {
this.parseNumber(...args);
this._option = args[4];
}
else if (typeof (args[0]) == 'number') {
this.parseNumber(0, 0, 0, args[0]);
this._option = args[1];
}
else if (args[0] instanceof HMSTime) {
this._hour = args[0]._hour;
this._minute = args[0]._minute;
this._second = args[0]._second;
this._millisecond = args[0]._millisecond;
}
}
get hour() {
return this._hour;
}
get minute() {
return this._minute;
}
get second() {
return this._second;
}
get millisecond() {
return this._millisecond;
}
parseNumber(hour, minute, second, millisecond) {
if ((hour && hour < 0) || (minute && minute < 0) || (second && second < 0) || (millisecond && millisecond < 0)) {
throw new definitions_1.NegativeValueError();
}
if (hour !== undefined) {
this._hour = hour;
}
if (minute !== undefined) {
this._minute = minute;
}
if (second !== undefined) {
this._second = second;
}
if (millisecond !== undefined) {
this._millisecond = millisecond;
}
this._second += Math.floor(this.millisecond / 1000);
this._millisecond %= 1000;
this._minute += Math.floor(this.second / 60);
this._second %= 60;
this._hour += Math.floor(this.minute / 60);
this._minute %= 60;
return this;
}
parseString(s) {
s.replace(HMSTime.HMS_REGEXP, (match, d1, p2, d2, p4, d3, p6, d4) => {
let hourStr = "";
let minuteStr = "";
let secondStr = "";
let millisecondStr = "";
if (d3) {
secondStr = d3;
minuteStr = d2;
hourStr = d1;
}
else if (d2) {
secondStr = d2;
minuteStr = d1;
}
else {
secondStr = d1;
}
if (d4) {
millisecondStr = d4;
}
while (millisecondStr.length < 3) {
millisecondStr += '0';
}
this.parseNumber(Number(hourStr), Number(minuteStr), Number(secondStr), Number(millisecondStr));
return match;
});
return this;
}
toString() {
var _a, _b, _c, _d, _e, _f, _g, _h;
let hour = utils_1.fillNumberWidth(this.hour, (_b = (_a = this._option) === null || _a === void 0 ? void 0 : _a.numberWidthOption) === null || _b === void 0 ? void 0 : _b.hour);
let minute = utils_1.fillNumberWidth(this.minute, (_d = (_c = this._option) === null || _c === void 0 ? void 0 : _c.numberWidthOption) === null || _d === void 0 ? void 0 : _d.minute);
let second = utils_1.fillNumberWidth(this.second, (_f = (_e = this._option) === null || _e === void 0 ? void 0 : _e.numberWidthOption) === null || _f === void 0 ? void 0 : _f.second);
let millisecond = utils_1.fixNumberWidth(this.millisecond, (_h = (_g = this._option) === null || _g === void 0 ? void 0 : _g.numberWidthOption) === null || _h === void 0 ? void 0 : _h.millisecond);
if (hour)
hour += ':';
if (minute)
minute += ':';
if (millisecond)
millisecond = '.' + millisecond;
return hour + minute + second + millisecond;
}
toMillisecond() {
return this.hour * 60 * 60 * 1000 + this.minute * 60 * 1000 + this.second * 1000 + this.millisecond;
}
increase(timeObj) {
return this.parseNumber(this.hour + timeObj.hour, this.minute + timeObj.minute, this.second + timeObj.second, this.millisecond + timeObj.millisecond);
}
reduce(timeObj) {
this._hour -= timeObj.hour;
this._minute -= timeObj.minute;
this._second -= timeObj.second;
this._millisecond -= timeObj.millisecond;
if (this.millisecond < 0) {
this._millisecond += 1000;
this._second -= 1;
}
if (this.second < 0) {
this._second += 60;
this._minute -= 1;
}
if (this.minute < 0) {
this._minute += 60;
this._hour -= 1;
}
if (this.hour < 0) {
return this.parseNumber(0, 0, 0, 0);
}
return this;
}
offset(timeObj) {
return (this.millisecond - timeObj.millisecond) + (this.second - timeObj.second) * 1000 + (this.minute - timeObj.minute) * 60 * 1000 + (this.hour - timeObj.hour) * 60 * 60 * 1000;
}
}
exports.HMSTime = HMSTime;
HMSTime.HMS_REGEXP = /^([0-9]*)(:|:)?([0-9]*)(:|:)?([0-9]*)(\.|。|.)?([0-9]*)$/;