UNPKG

@undercroft/timespan

Version:

When dealing with time durations or intervals in JavaScript, it's often useful to have a reliable way to represent and manipulate them. The `Timespan` class provides a convenient and powerful solution for working with timespans in JavaScript.

1 lines 58.4 kB
{"version":3,"sources":["../src/type-utils/create-timeframe.ts","../src/constants.ts","../src/converters/date-unit-converter.ts","../src/converters/day-converter.ts","../src/converters/hour-converter.ts","../src/converters/millisecond-converter.ts","../src/converters/minute-converter.ts","../src/converters/month-converter.ts","../src/converters/second-converter.ts","../src/converters/week-converter.ts","../src/converters/year-converter.ts","../src/type-utils/is-valid-date.ts","../src/assertions/assert-valid-date.ts","../src/type-utils/is-valid-time-unit.ts","../src/assertions/assert-valid-time-unit.ts","../src/type-utils/is-valid-date-range.ts","../src/assertions/assert-valid-date-range.ts","../src/assertions/assert-allowed-characters.ts","../src/assertions/assert-max-input-length.ts","../src/unit-conversion-map.ts","../src/utils/calculate-time-frame.ts","../src/utils/timeframe-to-string.ts","../src/timespan.ts","../src/index.ts"],"names":["createTimeframe","years","months","weeks","days","hours","minutes","seconds","milliseconds","constants_default","AllowedCharactersRegexPattern","CenturyDivisor","DaysPerWeek","DaysPerYear","HoursPerDay","InputRegexPattern","InputRegexUnitIndex","InputRegexValueIndex","LeapYearDivisor","MaxInputStringLength","MillisecondsPerSecond","MinutesPerHour","MonthsPerYear","QuadricentennialDivisor","SecondsPerMinute","DateUnitConverter","_DateUnitConverter","isLeapYear","year","isDivisibleByLeapYearDivisor","isDivisibleByCenturyDivisor","isDivisibleByQuadricentennialDivisor","DayConverter","arguments","default","plural","abbreviation","millisecondsPerUnit","aliases","between","startDate","endDate","startMillis","getTime","endMillis","dayCount","Math","floor","add","Date","HourConverter","MillisecondConverter","MinuteConverter","MonthConverter","startYear","getUTCFullYear","startMonth","getUTCMonth","startDay","getUTCDate","endYear","endMonth","endDay","newDate","originalHours","getUTCHours","originalMinutes","getUTCMinutes","originalSeconds","getUTCSeconds","originalMilliseconds","getUTCMilliseconds","setUTCHours","setUTCMonth","SecondConverter","WeekConverter","weeksInMilliseconds","addedMilliseconds","_YearConverter","targetYear","isStartDateOnLeapYearDay","february","february29","dateWithYearsAdded","setUTCFullYear","setUTCDate","february28","YearConverter","isValidDate","value","isNaN","assertValidDate","Error","isValidTimeUnit","getTimeUnits","includes","assertValidTimeUnit","unit","isValidDateRange","assertValidDateRange","assertAllowedCharacters","test","assertMaxInputLength","length","converters","conversionTable","table","converter","alias","timeUnits","unitSet","Set","forEach","getConverter","calculateTimeFrame","timeFrame","tempDate","totalMonths","remainingTime","millisecondsPerWeek","millisecondsPerDay","millisecondsPerHour","millisecondsPerMinute","millisecondsPerSeconds","timeFrameToString","timespanParts","Object","keys","numericValue","Number","push","join","_Timespan","start","end","toTimeframe","toString","stringValue","toUnit","toMilliseconds","toSeconds","toMinutes","toHours","toDays","toWeeks","toMonths","toYears","fromString","input","timeframe","match","exec","setUTCMinutes","setUTCSeconds","setUTCMilliseconds","fromUnits","amount","fromMilliseconds","millis","fromSeconds","fromMinutes","fromHours","fromDays","fromWeeks","fromMonths","fromYears","Timespan","index_default"],"mappings":"wuEAMO,SAASA,IACd,MAAO,CACLC,MAAO,EACPC,OAAQ,EACRC,MAAO,EACPC,KAAM,EACNC,MAAO,EACPC,QAAS,EACTC,QAAS,EACTC,aAAc,CAChB,CACF,CCjBA,IAAOC,EAAQ,CAoCbC,8BAA+B,mBAC/BC,eAAgB,IAChBC,YAAa,EACbC,YAAa,IACbC,YAAa,GAsCbC,kBAAmB,wBAKnBC,oBAAqB,EAKrBC,qBAAsB,EACtBC,gBAAiB,EAQjBC,qBAAsB,GACtBC,sBAAuB,IACvBC,eAAgB,GAChBC,cAAe,GACfC,wBAAyB,IACzBC,iBAAkB,EACpB,ECjGO,IAAeC,eAAf,iCAAeC,WAAAA,SA+CbC,IAAAA,mBAAAA,SAAAA,EAAWC,CAAAA,EAChB,OACGF,EAAkBG,4BAAA,CAA6BD,IAC9C,CAACF,EAAkBI,2BAAA,CAA4BF,IACjDF,EAAkBK,oCA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,GACtC,OAAWlC,EAAqBkC,EAAO5F,GAChC,IAAI2F,EAASC,EAAO5F,EAC7B,EAzIW2F,EAiJGyB,gBAAA,CAAmB,SAC/BC,EACAtH,UACa4F,EAAKuB,SAAA,CAAUG,EAAQ,eAAgBtH,IApJ3C4F,EA4JG2B,WAAA,CAAc,SAACxJ,EAAiBiC,UAC5C4F,EAAKuB,SAAA,CAAUpJ,EAAS,UAAWiC,IA7J1B4F,EAqKG4B,WAAA,CAAc,SAAC1J,EAAiBkC,UAC5C4F,EAAKuB,SAAA,CAAUrJ,EAAS,UAAWkC,IAtK1B4F,EA8KG6B,SAAA,CAAY,SAAC5J,EAAemC,UACxC4F,EAAKuB,SAAA,CAAUtJ,EAAO,QAASmC,IA/KtB4F,EAuLG8B,QAAA,CAAW,SAAC9J,EAAcoC,UACtC4F,EAAKuB,SAAA,CAAUvJ,EAAM,OAAQoC,IAxLpB4F,EAgMG+B,SAAA,CAAY,SAAChK,EAAeqC,UACxC4F,EAAKuB,SAAA,CAAUxJ,EAAO,QAASqC,IAjMtB4F,EAyMGgC,UAAA,CAAa,SAAClK,EAAgBsC,UAC1C4F,EAAKuB,SAAA,CAAUzJ,EAAQ,SAAUsC,IA1MxB4F,EAkNGiC,SAAA,CAAY,SAACpK,EAAeuC,UACxC4F,EAAKuB,SAAA,CAAU1J,EAAO,QAASuC,IAnN5B,IAAM8H,EAANlC,ECPP,IAAOmC,EAAQD,SAAAA,KAAAA,QAAAA,CAAAA,KAAAA,OAAAA","sourcesContent":["import { TimeFrame } from '../types/timeframe';\n\n/**\n * Creates a new TimeFrame object with all values initialized to zero.\n * @returns A new TimeFrame object.\n */\nexport function createTimeframe(): TimeFrame {\n return {\n years: 0,\n months: 0,\n weeks: 0,\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n milliseconds: 0,\n };\n}\n","export default {\n /**\n * /^[a-zA-Z0-9\\s]+$/\n *\n * This regex pattern ensures that the entire input string consists of\n * one or more uppercase or lowercase letters, digits, or whitespace\n * characters. It does not allow any other characters in the string.\n *\n * This Regex, combined with some other strategies should\n * help reduce the vulnerability. It is still recommended to clean any input\n * you are using, especially if it originates from an uncontrolled source\n * such as a user typing input into an input field on the internet.\n *\n * - ^:\n * This anchor asserts the start of the string. It specifies that the\n * matching pattern should start at the beginning of the string.\n *\n * - [a-zA-Z0-9\\s]+: This character set specifies the allowed characters\n * in the string. It matches one or more occurrences (+) of any uppercase\n * or lowercase letter ([a-zA-Z]), digit (0-9), or whitespace character (\\s).\n *\n * - $: This anchor asserts the end of the string. It specifies that the\n * matching pattern should end at the end of the string.\n *\n * \"Hello123\": This input matches the pattern because it consists of\n * only letters and digits.\n *\n * \"Test String\": This input matches the pattern because it consists of\n * only letters and whitespace characters.\n *\n * \"123-456\": This input does not match the pattern because it contains\n * a hyphen character, which is not allowed.\n *\n * \"@#$\": This input does not match the pattern because it contains special\n * characters that are not part of the allowed character set.\n */\n AllowedCharactersRegexPattern: /^[a-zA-Z0-9\\s]+$/,\n CenturyDivisor: 100,\n DaysPerWeek: 7,\n DaysPerYear: 365,\n HoursPerDay: 24,\n\n /**\n * /(\\d{0,10})([^\\s\\d]+)/g\n *\n * This regex pattern /(\\d{0,10})([^\\s\\d]+)/g aims to match and\n * a sequence of digits (0-9) of up to 10 characters, captured in the first group.\n * A series of one or more non-whitespace, non-digit characters, captured in\n * the second group.\n *\n * No regex is perfect. It is recommended that you clean any input\n * you are using, especially if it originates from an uncontrolled source\n * such as a user typing input into an input field on the internet.\n *\n * - (\\d{0,10}):\n * This part captures a sequence of digits (0-9) that can occur between\n * 0 and 10 times. The parentheses ( ) indicate a capturing group, which\n * means the matched digits will be stored and accessible separately.\n *\n * - ([^\\s\\d]+): This part captures one or more characters that are not\n * whitespace and not digits. The square brackets [ ] define a character\n * class, and the ^ at the beginning of the class negates it. So [^\\s\\d]\n * matches any character that is not a whitespace character or a digit.\n * The + quantifier specifies that one or more characters should be matched.\n *\n * - g:\n * The g flag stands for \"global\" and indicates that the regex should\n * search for all matches in the input string, rather than stopping after\n * the first match.\n *\n * A few examples of acceptable inputs are:\n *\n * 1y 2M 3w 4d 5h 6m 7s 8ms\n *\n * 1yr 2mos 3wks 4dys 5hrs 6mins 7secs 8mss\n *\n * 2years 1month 3weeks 4days\n */\n InputRegexPattern: /(\\d{0,10})([^\\s\\d]+)/g,\n /**\n * This is the index of the unit in the inputString\n * regex match.\n */\n InputRegexUnitIndex: 2,\n /**\n * This is the index of the value in the inputString\n * regex match.\n */\n InputRegexValueIndex: 1,\n LeapYearDivisor: 4,\n /**\n * This sets the max allowable input string length to help reduce Regex\n * Denial of Service attacks. This combined with some other strategies should\n * help reduce the vulnerability. It is still recommended to clean any input\n * you are using, especially if it originates from an uncontrolled source\n * such as a user typing input into an input field on the internet.\n */\n MaxInputStringLength: 75,\n MillisecondsPerSecond: 1000,\n MinutesPerHour: 60,\n MonthsPerYear: 12,\n QuadricentennialDivisor: 400,\n SecondsPerMinute: 60,\n};\n","import Constants from '../constants';\nimport { TimeUnit } from '../types';\n\n/**\n * Represents a converter for a specific date unit.\n */\nexport abstract class DateUnitConverter {\n /**\n * The default name of the date unit.\n */\n public abstract default: string;\n\n /**\n * The plural name of the date unit.\n */\n public abstract plural: string;\n\n /**\n * The abbreviated name of the date unit.\n */\n public abstract abbreviation: string;\n\n /**\n * An array of aliases for the date unit.\n */\n public abstract aliases: Array<TimeUnit>;\n\n /**\n * The number of milliseconds per unit for conversions.\n */\n public abstract millisecondsPerUnit: number;\n\n /**\n * Calculates the number of units between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of units between the two dates.\n */\n public abstract between(startDate: Date, endDate: Date): number;\n\n /**\n * Adds the specified number of units to the given date.\n * @param value - The number of units to add.\n * @param startDate - The start date.\n * @returns The new date after adding the units.\n */\n public abstract add(value: number, startDate: Date): Date;\n\n /**\n * Checks if the given year is a leap year.\n * @param year - The year.\n * @returns True if the year is a leap year, false otherwise.\n */\n public isLeapYear(year: number): boolean {\n return (\n (DateUnitConverter.isDivisibleByLeapYearDivisor(year) &&\n !DateUnitConverter.isDivisibleByCenturyDivisor(year)) ||\n DateUnitConverter.isDivisibleByQuadricentennialDivisor(year)\n );\n }\n\n private static isDivisibleByLeapYearDivisor(year: number): boolean {\n return year % Constants.LeapYearDivisor === 0;\n }\n\n private static isDivisibleByCenturyDivisor(year: number): boolean {\n return year % Constants.CenturyDivisor === 0;\n }\n\n private static isDivisibleByQuadricentennialDivisor(year: number): boolean {\n return year % Constants.QuadricentennialDivisor === 0;\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport Constants from '../constants';\nimport { DayUnit } from '../types';\n\n/**\n * Represents a converter for the day unit.\n */\nexport class DayConverter extends DateUnitConverter {\n /**\n * The default name of the day unit.\n */\n public readonly default: DayUnit = 'day';\n\n /**\n * The plural name of the day unit.\n */\n public readonly plural: DayUnit = 'days';\n\n /**\n * The abbreviated name of the day unit.\n */\n public readonly abbreviation: DayUnit = 'd';\n\n /**\n * The number of milliseconds per day.\n */\n public readonly millisecondsPerUnit =\n Constants.HoursPerDay *\n Constants.MinutesPerHour *\n Constants.SecondsPerMinute *\n Constants.MillisecondsPerSecond;\n\n /**\n * An array of aliases for the day unit.\n */\n public readonly aliases: Array<DayUnit> = ['day', 'days', 'dys', 'dy', 'd'];\n\n /**\n * Calculates the number of days between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of days between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n const startMillis = startDate.getTime();\n const endMillis = endDate.getTime();\n\n const dayCount = Math.floor(\n (endMillis - startMillis) / this.millisecondsPerUnit,\n );\n\n return Math.floor(dayCount);\n }\n\n /**\n * Adds the specified number of days to the given date.\n * @param days - The number of days to add.\n * @param startDate - The start date.\n * @returns The new date after adding the days.\n * @throws Error if the input date is invalid or if the number of days is negative.\n */\n public add(days: number, startDate: Date): Date {\n return new Date(startDate.getTime() + days * this.millisecondsPerUnit);\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport Constants from '../constants';\nimport { HourUnit } from '../types';\n\n/**\n * Represents a converter for the hour unit.\n */\nexport class HourConverter extends DateUnitConverter {\n /**\n * The default name of the hour unit.\n */\n public readonly default: HourUnit = 'hour';\n\n /**\n * The plural name of the hour unit.\n */\n public readonly plural: HourUnit = 'hours';\n\n /**\n * The abbreviated name of the hour unit.\n */\n public readonly abbreviation: HourUnit = 'h';\n\n /**\n * The number of milliseconds per hour.\n */\n public readonly millisecondsPerUnit =\n Constants.MinutesPerHour *\n Constants.SecondsPerMinute *\n Constants.MillisecondsPerSecond;\n\n /**\n * An array of aliases for the hour unit.\n */\n public readonly aliases: Array<HourUnit> = [\n 'hours',\n 'hour',\n 'hrs',\n 'hr',\n 'h',\n ];\n\n /**\n * Calculates the number of hours between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of hours between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n return Math.floor(\n (endDate.getTime() - startDate.getTime()) / this.millisecondsPerUnit,\n );\n }\n\n /**\n * Adds the specified number of hours to the given date.\n * @param hours - The number of hours to add.\n * @param startDate - The start date.\n * @returns The new date after adding the hours.\n * @throws Error if the input date is invalid or if the number of hours is negative.\n */\n public add(hours: number, startDate: Date): Date {\n return new Date(startDate.getTime() + hours * this.millisecondsPerUnit);\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport { MillisecondUnit } from '../types';\n\n/**\n * Represents a converter for the millisecond unit.\n */\nexport class MillisecondConverter extends DateUnitConverter {\n /**\n * The default name of the millisecond unit.\n */\n public readonly default: MillisecondUnit = 'millisecond';\n\n /**\n * The plural name of the millisecond unit.\n */\n public readonly plural: MillisecondUnit = 'milliseconds';\n\n /**\n * The abbreviated name of the millisecond unit.\n */\n public readonly abbreviation: MillisecondUnit = 'ms';\n\n /**\n * The number of milliseconds per millisecond (1).\n */\n public readonly millisecondsPerUnit = 1;\n\n /**\n * An array of aliases for the millisecond unit.\n */\n public readonly aliases: Array<MillisecondUnit> = [\n 'milliseconds',\n 'millisecond',\n 'msec',\n 'mss',\n 'ms',\n ];\n\n /**\n * Calculates the number of milliseconds between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of milliseconds between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n return endDate.getTime() - startDate.getTime();\n }\n\n /**\n * Adds the specified number of milliseconds to the given date.\n * @param milliseconds - The number of milliseconds to add.\n * @param startDate - The start date.\n * @returns The new date after adding the milliseconds.\n * @throws Error if the input date is invalid or if the number of milliseconds is negative.\n */\n public add(milliseconds: number, startDate: Date): Date {\n return new Date(startDate.getTime() + milliseconds);\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport Constants from '../constants';\nimport { MinuteUnit } from '../types';\n\n/**\n * Represents a converter for the minute unit.\n */\nexport class MinuteConverter extends DateUnitConverter {\n /**\n * The default name of the minute unit.\n */\n public readonly default: MinuteUnit = 'minute';\n\n /**\n * The plural name of the minute unit.\n */\n public readonly plural: MinuteUnit = 'minutes';\n\n /**\n * The abbreviated name of the minute unit.\n */\n public readonly abbreviation: MinuteUnit = 'm';\n\n /**\n * The number of milliseconds per minute (60 * 1000).\n */\n public readonly millisecondsPerUnit =\n Constants.SecondsPerMinute * Constants.MillisecondsPerSecond;\n\n /**\n * An array of aliases for the minute unit.\n */\n public readonly aliases: Array<MinuteUnit> = [\n 'minutes',\n 'minute',\n 'mins',\n 'min',\n 'm',\n ];\n\n /**\n * Calculates the number of minutes between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of minutes between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n return Math.floor(\n (endDate.getTime() - startDate.getTime()) / this.millisecondsPerUnit,\n );\n }\n\n /**\n * Adds the specified number of minutes to the given date.\n * @param minutes - The number of minutes to add.\n * @param startDate - The start date.\n * @returns The new date after adding the minutes.\n * @throws Error if the input date is invalid or if the number of minutes is negative.\n */\n public add(minutes: number, startDate: Date): Date {\n return new Date(startDate.getTime() + minutes * this.millisecondsPerUnit);\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport Constants from '../constants';\nimport { MonthUnit } from '../types';\n\n/**\n * Represents a converter for the month unit.\n */\nexport class MonthConverter extends DateUnitConverter {\n /**\n * The default name of the month unit.\n */\n public readonly default: MonthUnit = 'month';\n\n /**\n * The plural name of the month unit.\n */\n public readonly plural: MonthUnit = 'months';\n\n /**\n * The abbreviated name of the month unit.\n */\n public readonly abbreviation: MonthUnit = 'M';\n\n /**\n * The number of milliseconds per month (-1).\n */\n public readonly millisecondsPerUnit = -1;\n\n /**\n * An array of aliases for the month unit.\n */\n public readonly aliases: Array<MonthUnit> = [\n 'months',\n 'month',\n 'mos',\n 'mo',\n 'M',\n ];\n\n /**\n * Calculates the number of months between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of months between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n const startYear = startDate.getUTCFullYear();\n const startMonth = startDate.getUTCMonth();\n const startDay = startDate.getUTCDate();\n const endYear = endDate.getUTCFullYear();\n const endMonth = endDate.getUTCMonth();\n const endDay = endDate.getUTCDate();\n\n let months =\n (endYear - startYear) * Constants.MonthsPerYear + (endMonth - startMonth);\n if (endDay < startDay) {\n months--;\n }\n return Math.floor(months);\n }\n\n /**\n * Adds the specified number of months to the given date.\n * @param months - The number of months to add.\n * @param startDate - The start date.\n * @returns The new date after adding the months.\n * @throws Error if the input date is invalid or if the number of months is negative.\n */\n public add(months: number, startDate: Date): Date {\n const newDate = new Date(startDate);\n const originalHours = newDate.getUTCHours();\n const originalMinutes = newDate.getUTCMinutes();\n const originalSeconds = newDate.getUTCSeconds();\n const originalMilliseconds = newDate.getUTCMilliseconds();\n // Set UTC time components to zero\n newDate.setUTCHours(0, 0, 0, 0);\n newDate.setUTCMonth(newDate.getUTCMonth() + months);\n // Restore original UTC time components\n newDate.setUTCHours(\n originalHours,\n originalMinutes,\n originalSeconds,\n originalMilliseconds,\n );\n return newDate;\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport { SecondUnit } from '../types';\n\n/**\n * Represents a converter for the second unit.\n */\nexport class SecondConverter extends DateUnitConverter {\n /**\n * The default name of the second unit.\n */\n public readonly default: SecondUnit = 'second';\n\n /**\n * The plural name of the second unit.\n */\n public readonly plural: SecondUnit = 'seconds';\n\n /**\n * The abbreviated name of the second unit.\n */\n public readonly abbreviation: SecondUnit = 's';\n\n /**\n * The number of milliseconds per second.\n */\n public readonly millisecondsPerUnit = 1000;\n\n /**\n * An array of aliases for the second unit.\n */\n public readonly aliases: Array<SecondUnit> = [\n 'seconds',\n 'second',\n 'secs',\n 'sec',\n 's',\n ];\n\n /**\n * Calculates the number of seconds between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of seconds between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n return Math.floor(\n (endDate.getTime() - startDate.getTime()) / this.millisecondsPerUnit,\n );\n }\n\n /**\n * Adds the specified number of seconds to the given date.\n * @param seconds - The number of seconds to add.\n * @param startDate - The start date.\n * @returns The new date after adding the seconds.\n * @throws Error if the input date is invalid or if the number of seconds is negative.\n */\n public add(seconds: number, startDate: Date): Date {\n return new Date(startDate.getTime() + seconds * this.millisecondsPerUnit);\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport Constants from '../constants';\nimport { WeekUnit } from '../types';\n\n/**\n * Represents a converter for the week unit.\n */\nexport class WeekConverter extends DateUnitConverter {\n /**\n * The default name of the week unit.\n */\n public readonly default: WeekUnit = 'week';\n\n /**\n * The plural name of the week unit.\n */\n public readonly plural: WeekUnit = 'weeks';\n\n /**\n * The abbreviated name of the week unit.\n */\n public readonly abbreviation: WeekUnit = 'w';\n\n /**\n * The number of milliseconds per week.\n */\n public readonly millisecondsPerUnit =\n Constants.DaysPerWeek *\n Constants.HoursPerDay *\n Constants.MinutesPerHour *\n Constants.SecondsPerMinute *\n Constants.MillisecondsPerSecond;\n\n /**\n * An array of aliases for the week unit.\n */\n public readonly aliases: Array<WeekUnit> = [\n 'w',\n 'wk',\n 'wks',\n 'week',\n 'weeks',\n ];\n\n /**\n * Calculates the number of weeks between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of weeks between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n return Math.floor(\n (endDate.getTime() - startDate.getTime()) / this.millisecondsPerUnit,\n );\n }\n\n /**\n * Adds the specified number of weeks to the given date.\n * @param weeks - The number of weeks to add.\n * @param startDate - The start date.\n * @returns The new date after adding the weeks.\n * @throws Error if the input date is invalid or if the number of weeks is negative.\n */\n public add(weeks: number, startDate: Date): Date {\n const weeksInMilliseconds = weeks * this.millisecondsPerUnit;\n const addedMilliseconds = startDate.getTime() + weeksInMilliseconds;\n return new Date(addedMilliseconds);\n }\n}\n","import { DateUnitConverter } from './date-unit-converter';\nimport Constants from '../constants';\nimport { YearUnit } from '../types';\n\n/**\n * Represents a converter for the year unit.\n */\nexport class YearConverter extends DateUnitConverter {\n private static readonly february28 = 28;\n private static readonly february29 = 29;\n private static readonly february = 1;\n /**\n * The default name of the year unit.\n */\n public readonly default: YearUnit = 'year';\n\n /**\n * The plural name of the year unit.\n */\n public readonly plural: YearUnit = 'years';\n\n /**\n * The abbreviated name of the year unit.\n */\n public readonly abbreviation: YearUnit = 'y';\n\n /**\n * The number of milliseconds per year.\n */\n public readonly millisecondsPerUnit =\n Constants.DaysPerYear *\n Constants.HoursPerDay *\n Constants.MinutesPerHour *\n Constants.SecondsPerMinute *\n Constants.MillisecondsPerSecond;\n\n /**\n * An array of aliases for the year unit.\n */\n public readonly aliases: Array<YearUnit> = [\n 'years',\n 'year',\n 'yrs',\n 'yr',\n 'y',\n ];\n\n /**\n * Calculates the number of years between two dates.\n * @param startDate - The start date.\n * @param endDate - The end date.\n * @returns The number of years between the two dates.\n * @throws Error if the input dates are invalid or if the start date is greater than the end date.\n */\n public between(startDate: Date, endDate: Date): number {\n const startYear = startDate.getUTCFullYear();\n const endYear = endDate.getUTCFullYear();\n return endYear - startYear;\n }\n\n /**\n * Adds the specified number of years to the given date.\n * @param years - The number of years to add.\n * @param startDate - The start date.\n * @returns The new date after adding the years.\n * @throws Error if the input date is invalid or if the number of years is negative.\n */\n public add(years: number, startDate: Date): Date {\n const startYear = startDate.getUTCFullYear();\n const targetYear = startYear + years;\n\n // Check if the starting date is on February 29th\n const isStartDateOnLeapYearDay =\n startDate.getUTCMonth() === YearConverter.february &&\n startDate.getUTCDate() === YearConverter.february29;\n\n const dateWithYearsAdded = new Date(startDate.getTime());\n dateWithYearsAdded.setUTCFullYear(targetYear);\n\n // If the starting date is on February 29th and the target year is not a leap year,\n // set the date to February 28th\n if (isStartDateOnLeapYearDay && !this.isLeapYear(targetYear)) {\n dateWithYearsAdded.setUTCMonth(YearConverter.february);\n dateWithYearsAdded.setUTCDate(YearConverter.february28);\n }\n\n return dateWithYearsAdded;\n }\n}\n","/**\n * Checks if a value is a valid `Date` instance.\n * @param value - The value to check.\n * @returns True if the value is a valid `Date`.\n */\nexport function isValidDate(value: unknown): value is Date {\n return value instanceof Date && !isNaN(value.getTime());\n}\n","import { isValidDate } from '../type-utils/is-valid-date';\n\n/**\n * Asserts that a value is a valid `Date`.\n * @param value - The value to check.\n * @throws Error if the value is not a valid date.\n */\nexport function assertValidDate(value: unknown): asserts value is Date {\n if (!isValidDate(value)) {\n throw new Error('Invalid date input.');\n }\n}\n","import { getTimeUnits } from '../unit-conversion-map';\nimport { TimeUnit } from '../types';\n\n/**\n * Checks if the provided unit is a valid input for date unit calculations.\n * @param value - The time unit to check.\n * @returns True if the unit is valid.\n */\nexport function isValidTimeUnit(value: unknown): value is TimeUnit {\n return getTimeUnits().includes(value as TimeUnit);\n}\n","import { isValidTimeUnit } from '../type-utils/is-valid-time-unit';\nimport { TimeUnit } from '../types';\n\n/**\n * Asserts that the given unit is part of the time unit conversion table.\n * @param unit - The time unit to validate.\n * @throws Error if the unit is not recognized.\n */\nexport function assertValidTimeUnit(unit: unknown): asserts unit is TimeUnit {\n if (!isValidTimeUnit(unit)) {\n throw new Error(`Invalid date unit: ${unit}.`);\n }\n}\n","/**\n * Checks whether the start date is less than or equal to the end date.\n * @param startDate - The proposed start date.\n * @param endDate - The proposed end date.\n * @returns True if the range is valid.\n */\nexport function isValidDateRange(startDate: Date, endDate: Date): boolean {\n return startDate.getTime() <= endDate.getTime();\n}\n","import { isValidDateRange } from '../type-utils/is-valid-date-range';\n\n/**\n * Asserts that the start date is not after the end date.\n * @param startDate - The proposed start date.\n * @param endDate - The proposed end date.\n * @throws Error if the date range is invalid.\n */\nexport function assertValidDateRange(startDate: Date, endDate: Date): void {\n if (!isValidDateRange(startDate, endDate)) {\n throw new Error('Start date cannot be greater than end date.');\n }\n}\n","import Constants from '../constants';\n\n/**\n * Asserts that the input string contains only allowed characters.\n * @param value - The input string to check.\n */\nexport function assertAllowedCharacters(\n value: unknown,\n): asserts value is string {\n if (\n value === null ||\n typeof value !== 'string' ||\n !Constants.AllowedCharactersRegexPattern.test(value)\n ) {\n throw new Error('Invalid input string');\n }\n}\n","import Constants from '../constants';\n\n/**\n * Asserts that the input string does not exceed the maximum allowed length.\n * @param value - The input string to check.\n */\nexport function assertMaxInputLength(value: unknown): asserts value is string {\n if (\n value === null ||\n typeof value !== 'string' ||\n value.length > Constants.MaxInputStringLength\n ) {\n throw new Error(`Invalid input string`);\n }\n}\n","import { DateUnitConverter } from './converters/date-unit-converter';\nimport { DayConverter } from './converters/day-converter';\nimport { HourConverter } from './converters/hour-converter';\nimport { MillisecondConverter } from './converters/millisecond-converter';\nimport { MinuteConverter } from './converters/minute-converter';\nimport { MonthConverter } from './converters/month-converter';\nimport { SecondConverter } from './converters/second-converter';\nimport { WeekConverter } from './converters/week-converter';\nimport { YearConverter } from './converters/year-converter';\nimport { TimeUnit } from './types';\nimport { assertValidTimeUnit } from './assertions';\n\nconst converters: DateUnitConverter[] = [\n new YearConverter(),\n new MonthConverter(),\n new WeekConverter(),\n new DayConverter(),\n new HourConverter(),\n new MinuteConverter(),\n new SecondConverter(),\n new MillisecondConverter(),\n];\n\nconst conversionTable: Record<TimeUnit, DateUnitConverter> = (() => {\n const table: Partial<Record<TimeUnit, DateUnitConverter>> = {};\n for (const converter of converters) {\n for (const alias of converter.aliases) {\n table[alias as TimeUnit] = converter;\n }\n }\n return table as Record<TimeUnit, DateUnitConverter>;\n})();\n\nconst timeUnits: TimeUnit[] = (() => {\n const unitSet = new Set<TimeUnit>();\n for (const converter of converters) {\n converter.aliases.forEach((alias: TimeUnit) =>\n unitSet.add(alias as TimeUnit),\n );\n }\n return [...unitSet];\n})();\n\n/**\n * Get the time unit available in the conversion table.\n * @returns The available time units.\n */\nexport function getTimeUnits(): TimeUnit[] {\n return timeUnits;\n}\n\n/**\n * Retrieves the date unit converter for the specified unit.\n * @param unit - The unit to retrieve the converter for.\n * @returns The date unit converter.\n * @throws {Error} If an invalid date unit is specified.\n */\nexport function getConverter(unit: TimeUnit): DateUnitConverter {\n assertValidTimeUnit(unit);\n return conversionTable[unit];\n}\n","import { TimeFrame } from '../types/timeframe';\nimport { createTimeframe } from '../type-utils/create-timeframe';\nimport Constants from '../constants';\nimport { getConverter } from '../unit-conversion-map';\n\n/**\n * Calculate the TimeFrame representation of the timespan.\n * @param startDate - The start date of the timespan.\n * @param endDate - The end date of the timespan.\n * @returns The TimeFrame object representing the timespan.\n */\nexport function calculateTimeFrame(startDate: Date, endDate: Date): TimeFrame {\n const timeFrame: TimeFrame = createTimeframe();\n const tempDate = new Date(startDate);\n\n // Calculating the years and months is... complicated.\n timeFrame.years = getConverter('years').between(startDate, endDate);\n\n // We get the total months between the two dates\n const totalMonths = getConverter('months').between(startDate, endDate);\n\n // We calculate the months remaining after accounting for the years\n timeFrame.months = Math.floor(\n totalMonths - timeFrame.years * Constants.MonthsPerYear,\n );\n\n tempDate.setUTCFullYear(tempDate.getUTCFullYear() + timeFrame.years);\n tempDate.setUTCMonth(tempDate.getUTCMonth() + timeFrame.months);\n\n const totalTimeDiff = endDate.getTime() - startDate.getTime();\n\n let remainingTime =\n totalTimeDiff - (tempDate.getTime() - startDate.getTime());\n\n const millisecondsPerWeek = getConverter('week').millisecondsPerUnit;\n const millisecondsPerDay = getConverter('days').millisecondsPerUnit;\n const millisecondsPerHour = getConverter('hours').millisecondsPerUnit;\n const millisecondsPerMinute = getConverter('minutes').millisecondsPerUnit;\n const millisecondsPerSeconds = getConverter('seconds').millisecondsPerUnit;\n\n timeFrame.weeks = Math.floor(remainingTime / millisecondsPerWeek);\n remainingTime %= millisecondsPerWeek;\n timeFrame.days = Math.floor(remainingTime / millisecondsPerDay);\n remainingTime %= millisecondsPerDay;\n timeFrame.hours = Math.floor(remainingTime / millisecondsPerHour);\n remainingTime %= millisecondsPerHour;\n timeFrame.minutes = Math.floor(remainingTime / millisecondsPerMinute);\n remainingTime %= millisecondsPerMinute;\n timeFrame.seconds = Math.floor(remainingTime / millisecondsPerSeconds);\n remainingTime %= millisecondsPerSeconds;\n timeFrame.milliseconds = remainingTime;\n\n return timeFrame;\n}\n","import { TimeFrame } from '../types/timeframe';\nimport { isValidTimeUnit } from '../type-utils/is-valid-time-unit';\nimport { getConverter } from '../unit-conversion-map';\n\n/**\n * Calculates a string representation of a TimeFrame.\n * @param timeFrame - The TimeFrame object to convert.\n * @returns A string representation of the TimeFrame.\n */\nexport function timeFrameToString(timeFrame: TimeFrame): string {\n const timespanParts: string[] = [];\n\n // Iterate through the timeFrame and generate the string parts for non-zero values\n for (const unit of Object.keys(timeFrame)) {\n const numericValue = Number(timeFrame[unit]);\n // Check if the value is non-zero and append to the timespanParts\n if (numericValue !== 0 && isValidTimeUnit(unit)) {\n const abbreviation = getConverter(unit).abbreviation;\n timespanParts.push(`${numericValue}${abbreviation}`);\n }\n }\n return timespanParts.join(' ');\n}\n","import { TimeFrame } from './types/timeframe';\nimport { TimeUnit } from './types';\nimport { calculateTimeFrame, timeFrameToString } from './utils';\nimport { getConverter } from './unit-conversion-map';\nimport { createTimeframe } from './type-utils/create-timeframe';\nimport * as Assertions from './assertions';\nimport Constants from './constants';\n\n/**\n * Represents a time span between two dates.\n */\nexport class Timespan {\n /**\n * The start of the time span.\n */\n private readonly startDate: Date;\n\n /**\n * The end of the time span.\n */\n private readonly endDate: Date;\n\n /**\n * We precalculate the string value when the Timespan\n * is created so you don't incur as much memory cost generating\n * it when you want to use it.\n */\n private readonly stringValue: string;\n\n /**\n * We go ahead and generate the TimeFrame when the Timespan\n * is created. This way you can go ahead and get the timeframe\n * at only point without incurring any extra memory cost.\n */\n private readonly timeFrame: TimeFrame;\n\n /**\n * Create a new Timespan instance.\n * @param start - The start date of the timespan.\n * @param end - The end date of the timespan.\n */\n constructor(start: Date, end: Date) {\n Assertions.assertValidDate(start);\n Assertions.assertValidDate(end);\n Assertions.assertValidDateRange(start, end);\n this.startDate = start;\n this.endDate = end;\n this.timeFrame = calculateTimeFrame(start, end);\n this.stringValue = timeFrameToString(this.timeFrame);\n }\n\n /**\n * Get the start date of the timespan.\n * @returns The start date.\n */\n public get start(): Date {\n return this.startDate;\n }\n\n /**\n * Get the end date of the timespan.\n * @returns The end date.\n */\n public get end(): Date {\n return this.endDate;\n }\n\n /**\n * Create a Timespan instance from a string input.\n * @example - \"1y 2M 3w 4d 5h 6m 7s 8ms\"\n * @example - \"1yr 2mos 3wks 4dys 5hrs 6mins 7secs 8mss\"\n * @example - \"2years 1month 3weeks 4days\"\n * @param input - The input string representing the timespan.\n * @param startDate - Optional start date for the timespan. If not provided, the current date is used.\n * @returns A Timespan instance representing the parsed timespan.\n * @throws Error if the input is invalid or contains an invalid unit.\n */\n public static fromString = (input: string, startDate?: Date): Timespan => {\n const start = startDate ? startDate : new Date();\n Assertions.assertValidDate(start);\n Assertions.assertMaxInputLength(input);\n Assertions.assertAllowedCharacters(