{"version":3,"file":"index.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Time class that supports the range 00:00:00 - 23:59:59\n */\nexport class Time {\n\t/**\n\t * regex for format HH:MM:SS or HH:MM\n\t */\n\tstatic readonly regex = /^(\\d\\d):(\\d\\d)(:(\\d\\d))?$/;\n\t\n\t_h: number;\n\t_m: number;\n\t_s: number;\n\n\t/** Gets the hours (0-23) of the current Time instance */\n\tget h() { return this._h; }\n\t/** Gets the minutes (0-59) of the current Time instance */\n\tget m() { return this._m; }\n\t/** Gets the seconds (0-59) of the current Time instance */\n\tget s() { return this._s; }\n\n\t/**\n\t * Sets the hour (0-23) of the current Time instance\n\t * @param v an integer, if it exceeds the valid representation, it will overflow (for example 25 becomes 1)\n\t */\n\tset h(v: number) {\n\t\tif (v < 0) {\n\t\t\tthis._h = 24 + (v % 24);\n\t\t} else {\n\t\t\tthis._h = v % 24;\n\t\t}\n\t}\n\n\t/**\n\t * Sets the minutes (0-59) of the current Time instance\n\t * @param v an integer, if it exceeds the valid representation, it will overflow (for example 60 becomes 0)\n\t */\n\tset m(v: number) {\n\t\tif (v < 0) {\n\t\t\tthis._m = 60 + (v % 60);\n\t\t} else {\n\t\t\tthis._m = v % 60;\n\t\t}\n\t\tthis.h += Math.floor(v / 60);\n\t}\n\n\t/**\n\t * Sets the seconds (0-59) of the current Time instance\n\t * @param v an integer, if it exceeds the valid representation, it will overflow (for example 60 becomes 0)\n\t */\n\tset s(v: number) {\n\t\tif (v < 0) {\n\t\t\tthis._s = 60 + (v % 60);\n\t\t} else {\n\t\t\tthis._s = v % 60;\n\t\t}\n\t\tthis.m += Math.floor(v / 60);\n\t}\n\n\t/** Returns an integer representation of the current instance */\n\tget totalSeconds(): number {\n\t\treturn this._h * 60 * 60 + this._m * 60 + this._s;\n\t}\n\n\t/**\n\t * Constructs a Time object\n\t * @param h hours\n\t * @param m minutes\n\t * @param s seconds\n\t */\n\tconstructor(h?: number, m?: number, s?: number);\n\t/**\n\t * Constructs a Time object\n\t * @param s integer representation of a Time (new Time(s).totalSeconds === s)\n\t */\n\tconstructor(s?: number);\n\t/**\n\t * Constructs a Time object\n\t * @param date the reference date the Time of day will be extracted from\n\t */\n\tconstructor(date: Date);\n\t/**\n\t * Constructs a Time object\n\t * @param str a string representation of a Time HH:MM:SS or HH:MM\n\t */\n\tconstructor(str: string);\n\tconstructor(secondsOrDateOrHours?: number|Date|string, m?: number, s?: number) {\n\t\tif (secondsOrDateOrHours === undefined || secondsOrDateOrHours === null) {\n\t\t\tconst ref = new Date();\n\t\t\tthis._h = ref.getHours();\n\t\t\tthis._m = ref.getMinutes();\n\t\t\tthis._s = ref.getSeconds();\n\t\t} else if (secondsOrDateOrHours instanceof Date) {\n\t\t\tconst ref = secondsOrDateOrHours;\n\t\t\tthis._h = ref.getHours();\n\t\t\tthis._m = ref.getMinutes();\n\t\t\tthis._s = ref.getSeconds();\n\t\t} else if (typeof secondsOrDateOrHours === 'string') {\n\t\t\tconst matches = secondsOrDateOrHours.match(Time.regex);\n\n\t\t\tif (!matches) {\n\t\t\t\tthrow new Error(`invalid time format ${secondsOrDateOrHours}`);\n\t\t\t}\n\n\t\t\tthis._h = 0;\n\t\t\tthis._m = 0;\n\t\t\tthis._s = 0;\n\t\t\tthis.s += Number(matches[4] ?? 0);\n\t\t\tthis.m += Number(matches[2]);\n\t\t\tthis.h += Number(matches[1]);\n\t\t} else if (m === undefined) {\n\t\t\tthis._h = 0;\n\t\t\tthis._m = 0;\n\t\t\tthis._s = 0;\n\t\t\tthis.s += secondsOrDateOrHours;\n\t\t} else {\n\t\t\tthis._h = 0;\n\t\t\tthis._m = 0;\n\t\t\tthis._s = 0;\n\t\t\tthis.s += s || 0;\n\t\t\tthis.m += m || 0;\n\t\t\tthis.h += secondsOrDateOrHours || 0;\n\t\t}\n\t}\n\n\t/**\n\t * Creates a clone of the current instance\n\t */\n\tclone(): Time {\n\t\treturn new Time(this._h, this._m, this._s);\n\t}\n\n\t/**\n\t * Creates a Time corresponding to 00:00:00\n\t */\n\tstatic zero(): Time {\n\t\treturn new Time(0);\n\t}\n\n\t/**\n\t * Returns a Time from the current time of day\n\t */\n\tstatic now(): Time {\n\t\treturn new Time();\n\t}\n\n\t/**\n\t * Returns a Time object from a string\n\t * @param str a string representation of a Time\n\t */\n\tstatic fromString(str: string): Time {\n\t\treturn new Time(str);\n\t}\n\n\t/**\n\t * Returns a Time object\n\t * @param date the reference date the Time of day will be extracted from\n\t */\n\tstatic fromDate(date: Date): Time {\n\t\treturn new Time(date);\n\t}\n\n\t/**\n\t * Returns a Date object with the time of day set according to this instance\n\t * @param year year\n\t * @param month month\n\t * @param date date\n\t */\n\ttoDate(year: number, month: number, date: number): Date;\n\t/**\n\t * Returns a new Date object with the time of day set according to this instance\n\t * @param date the reference Date the year, month and date will be extracted from\n\t */\n\ttoDate(date: Date): Date;\n\t/**\n\t * Returns a new Date object with the time of day set according to this instance\n\t */\n\ttoDate(): Date;\n\n\ttoDate(dateOrYear?: number|Date, month?: number, date?: number): Date {\n\t\tif (!dateOrYear) {\n\t\t\treturn this.toDate(new Date());\n\t\t}\n\t\tconst native = typeof dateOrYear === \"number\"\n\t\t\t? new Date(dateOrYear, month!, date!)\n\t\t\t: new Date(dateOrYear.getFullYear(), dateOrYear.getMonth(), dateOrYear.getDate());\n\n\t\tnative.setSeconds(this._s);\n\t\tnative.setMinutes(this._m);\n\t\tnative.setHours(this._h);\n\t\t\n\t\treturn native;\n\t}\n\n\t/**\n\t * Returns a string representation for the current instance\n\t * @param config configuration\n\t * @param config.seconds boolean indicating whether to include or omit the seconds from the resulting string\n\t * @param config.round boolean indicating whether to round or truncate the string representation without seconds\n\t */\n\ttoString({ seconds, round }: { seconds: boolean, round?: boolean} = { seconds: true }): string {\n\t\tlet result;\n\t\tif (!seconds) {\n\t\t\tif (!round || this._s < 30) {\n\t\t\t\tresult = `${\n\t\t\t\t\tMath.abs(this._h).toString().padStart(2, '0')}:${\n\t\t\t\t\tthis._m.toString().padStart(2, '0')}`;\t\n\t\t\t} else if (this._m < 59) {\n\t\t\t\tresult = `${\n\t\t\t\t\tMath.abs(this._h).toString().padStart(2, '0')}:${\n\t\t\t\t\t(this._m + 1).toString().padStart(2, '0')}`;\n\t\t\t} else if (this._h < 23) {\n\t\t\t\tresult = `${\n\t\t\t\t\t(Math.abs(this._h) + 1).toString().padStart(2, '0')}:00`;\n\t\t\t} else {\n\t\t\t\tresult = '00:00';\n\t\t\t}\n\t\t} else {\n\t\t\tresult = `${\n\t\t\t\tMath.abs(this._h).toString().padStart(2, '0')}:${\n\t\t\t\tthis._m.toString().padStart(2, '0')}:${\n\t\t\t\tthis._s.toString().padStart(2, '0')}`;\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Returns a string representation for the current instance\n\t */\n\ttoJSON(): string {\n\t\treturn this.toString();\n\t}\n\n\t/**\n\t * Returns a string representing the current time using the native toLocaleTimeString of the Date type.\n\t * @param locales an array of locales or a specific locale\n\t * @param options the Intl.DateFormatOptions object\n\t */\n\ttoLocaleString(locales?: string[]|string, options?: Intl.DateTimeFormatOptions): string {\n\t\treturn this.toDate().toLocaleTimeString(locales, options);\n\t}\n\n\t/**\n\t * Checks whether two times are equal\n\t * @param t1 first Time or string\n\t * @param t2 second Time or string\n\t */\n\tstatic equals(t1: Time|string, t2: Time|string) {\n\t\tif (typeof t1 === 'string') {\n\t\t\tt1 = Time.fromString(t1);\n\t\t}\n\t\treturn t1.equals(t2);\n\t}\n\t/**\n\t * Checks whether this instance is equal to another time\n\t * @param that the other instance or string\n\t */\n\tequals(that: Time|string): boolean {\n\t\tif (typeof that === 'string') {\n\t\t\tthat = Time.fromString(that);\n\t\t}\n\t\treturn this.toString() === that.toString();\n\t}\n\n\t/**\n\t * Compares two times\n\t * @param t1 first operand\n\t * @param t2 second operand\n\t */\n\tstatic compare(t1: Time|string, t2: Time|string) {\n\t\tif (typeof t1 === 'string') {\n\t\t\tt1 = Time.fromString(t1);\n\t\t}\n\t\treturn t1.compare(t2);\n\t}\n\t/**\n\t * Compares this instance to another time\n\t * @param that the other time instance or string\n\t */\n\tcompare(that: Time|string): number {\n\t\tif (typeof that === 'string') {\n\t\t\tthat = Time.fromString(that);\n\t\t}\n\n\t\treturn this.totalSeconds - that.totalSeconds;\n\t}\n\n\t/**\n\t * Adds a Time to the current instance, returning a new Time\n\t * @param that the other Time or string\n\t */\n\tadd(that: Time|string): Time {\n\t\tif (typeof that === 'string') {\n\t\t\tthat = Time.fromString(that);\n\t\t}\n\t\treturn new Time(this.totalSeconds + that.totalSeconds);\n\t}\n\n\t/**\n\t * Subtracts a Time to the current instance, returning a new Time. If the difference yields a negative result, the absolute value is considered\n\t * @param that the other Time or string\n\t */\n\tsub(that: Time|string): Time {\n\t\tif (typeof that === 'string') {\n\t\t\tthat = Time.fromString(that);\n\t\t}\n\t\treturn new Time(Math.abs(this.totalSeconds - that.totalSeconds));\n\t}\n}\n"],"mappings":"mEAGA,IAAa,EAAb,MAAa,CAAK,CAIjB,OAAgB,MAAQ,4BAExB,GACA,GACA,GAGA,IAAI,GAAI,CAAE,OAAO,KAAK,GAEtB,IAAI,GAAI,CAAE,OAAO,KAAK,GAEtB,IAAI,GAAI,CAAE,OAAO,KAAK,GAMtB,IAAI,EAAE,EAAW,CACZ,EAAI,EACP,KAAK,GAAK,GAAM,EAAI,GAEpB,KAAK,GAAK,EAAI,GAQhB,IAAI,EAAE,EAAW,CACZ,EAAI,EACP,KAAK,GAAK,GAAM,EAAI,GAEpB,KAAK,GAAK,EAAI,GAEf,KAAK,GAAK,KAAK,MAAM,EAAI,GAAG,CAO7B,IAAI,EAAE,EAAW,CACZ,EAAI,EACP,KAAK,GAAK,GAAM,EAAI,GAEpB,KAAK,GAAK,EAAI,GAEf,KAAK,GAAK,KAAK,MAAM,EAAI,GAAG,CAI7B,IAAI,cAAuB,CAC1B,OAAO,KAAK,GAAK,GAAK,GAAK,KAAK,GAAK,GAAK,KAAK,GAyBhD,YAAY,EAA2C,EAAY,EAAY,CAC9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