danholibraryjs 1.5.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/.gitattributes +2 -2
  2. package/README.md +12 -304
  3. package/Time.xlsx +0 -0
  4. package/dist/Classes/{Event.d.ts → Events/Event.d.ts} +9 -8
  5. package/dist/Classes/{Event.js → Events/Event.js} +17 -9
  6. package/dist/Classes/{EventCollection.d.ts → Events/EventCollection.d.ts} +10 -10
  7. package/dist/Classes/{EventCollection.js → Events/EventCollection.js} +28 -29
  8. package/dist/Classes/{EventEmitter.d.ts → Events/EventEmitter.d.ts} +33 -7
  9. package/dist/Classes/{EventEmitter.js → Events/EventEmitter.js} +33 -3
  10. package/dist/Classes/Events/index.d.ts +3 -0
  11. package/dist/Classes/Events/index.js +19 -0
  12. package/dist/Classes/Time/Date.d.ts +147 -0
  13. package/dist/Classes/Time/Date.js +238 -0
  14. package/dist/Classes/Time/Time.d.ts +65 -0
  15. package/dist/Classes/Time/Time.js +117 -0
  16. package/dist/Classes/Time/TimeProperties.d.ts +3 -0
  17. package/dist/{Interfaces/BaseEventInterface.js → Classes/Time/TimeProperties.js} +0 -0
  18. package/dist/Classes/Time/TimeSpan.d.ts +123 -0
  19. package/dist/Classes/Time/TimeSpan.js +179 -0
  20. package/dist/Classes/Time/index.d.ts +4 -0
  21. package/dist/Classes/Time/index.js +20 -0
  22. package/dist/Classes/index.d.ts +2 -3
  23. package/dist/Classes/index.js +7 -4
  24. package/dist/Classes/store.d.ts +79 -0
  25. package/dist/Classes/store.js +85 -0
  26. package/dist/Extensions/Array.d.ts +31 -0
  27. package/dist/Extensions/Array.js +26 -0
  28. package/dist/Extensions/Map.d.ts +38 -0
  29. package/dist/Extensions/Map.js +31 -0
  30. package/dist/Extensions/Object.d.ts +16 -0
  31. package/dist/Extensions/Object.js +8 -0
  32. package/dist/Extensions/String.d.ts +26 -0
  33. package/dist/Extensions/String.js +17 -0
  34. package/dist/Extensions/index.d.ts +29 -0
  35. package/dist/Extensions/index.js +71 -0
  36. package/dist/Functions/GetCSSProperty.d.ts +15 -0
  37. package/dist/Functions/GetCSSProperty.js +26 -0
  38. package/dist/Functions/HTMLEvent.d.ts +6 -0
  39. package/dist/Functions/HTMLEvent.js +6 -0
  40. package/dist/Functions/SetNavigationSelected.js +2 -0
  41. package/dist/Functions/index.d.ts +1 -0
  42. package/dist/Functions/index.js +6 -1
  43. package/dist/Interfaces/ElementOptions.d.ts +12 -15
  44. package/dist/Interfaces/IReplacement.d.ts +1 -1
  45. package/dist/Interfaces/index.d.ts +0 -1
  46. package/dist/Interfaces/index.js +5 -2
  47. package/dist/Types/BetterTypes.d.ts +9 -0
  48. package/dist/{Interfaces/IHTMLEvent.js → Types/BetterTypes.js} +0 -0
  49. package/dist/Types/Date.d.ts +6 -0
  50. package/dist/Types/{EventHandler.js → Date.js} +0 -0
  51. package/dist/Types/Events.d.ts +10 -0
  52. package/dist/Types/{IElement.js → Events.js} +0 -0
  53. package/dist/Types/PropertiesWith.d.ts +13 -0
  54. package/dist/Types/{StringRegex.js → PropertiesWith.js} +0 -0
  55. package/dist/Types/TransformTypes.d.ts +13 -0
  56. package/dist/Types/TransformTypes.js +2 -0
  57. package/dist/Types/index.d.ts +31 -3
  58. package/dist/Types/index.js +10 -4
  59. package/dist/index.d.ts +5 -5
  60. package/dist/index.js +19 -10
  61. package/docs/Classes.md +488 -0
  62. package/docs/Extensions.md +146 -0
  63. package/docs/Functions.md +53 -0
  64. package/docs/Interfaces.md +34 -0
  65. package/docs/Types.md +137 -0
  66. package/docs/index.md +29 -0
  67. package/package.json +22 -21
  68. package/src/Classes/{Event.ts → Events/Event.ts} +19 -14
  69. package/src/Classes/Events/EventCollection.ts +109 -0
  70. package/src/Classes/{EventEmitter.ts → Events/EventEmitter.ts} +37 -11
  71. package/src/Classes/Events/index.ts +3 -0
  72. package/src/Classes/Time/Date.ts +264 -0
  73. package/src/Classes/Time/Time.ts +134 -0
  74. package/src/Classes/Time/TimeProperties.ts +3 -0
  75. package/src/Classes/Time/TimeSpan.ts +195 -0
  76. package/src/Classes/Time/index.ts +4 -0
  77. package/src/Classes/index.ts +2 -3
  78. package/src/Classes/store.ts +95 -0
  79. package/src/Extensions/Array.ts +57 -0
  80. package/src/Extensions/Map.ts +73 -0
  81. package/src/Extensions/Object.ts +25 -0
  82. package/src/Extensions/String.ts +43 -0
  83. package/src/Extensions/index.ts +83 -0
  84. package/src/Functions/GetCSSProperty.ts +27 -0
  85. package/src/Functions/HTMLEvent.ts +6 -0
  86. package/src/Functions/SetNavigationSelected.ts +1 -0
  87. package/src/Functions/index.ts +2 -1
  88. package/src/Interfaces/ElementOptions.ts +18 -14
  89. package/src/Interfaces/IReplacement.ts +1 -1
  90. package/src/Interfaces/index.ts +1 -2
  91. package/src/Types/BetterTypes.ts +10 -0
  92. package/src/Types/Date.ts +7 -0
  93. package/src/Types/Events.ts +15 -0
  94. package/src/Types/PropertiesWith.ts +14 -0
  95. package/src/Types/TransformTypes.ts +18 -0
  96. package/src/Types/index.ts +36 -3
  97. package/src/index.ts +5 -6
  98. package/tsconfig.json +99 -7
  99. package/dist/Extensions.d.ts +0 -85
  100. package/dist/Extensions.js +0 -84
  101. package/dist/Interfaces/BaseEventInterface.d.ts +0 -4
  102. package/dist/Interfaces/IHTMLEvent.d.ts +0 -4
  103. package/dist/Types/EventHandler.d.ts +0 -7
  104. package/dist/Types/IElement.d.ts +0 -9
  105. package/dist/Types/StringRegex.d.ts +0 -2
  106. package/src/Classes/EventCollection.ts +0 -116
  107. package/src/Extensions.ts +0 -185
  108. package/src/Interfaces/BaseEventInterface.ts +0 -4
  109. package/src/Types/EventHandler.ts +0 -12
  110. package/src/Types/IElement.ts +0 -9
  111. package/src/Types/StringRegex.ts +0 -2
  112. package/test.js +0 -20
@@ -0,0 +1,264 @@
1
+ import { BetterOmit, PartialExcept, LongMonth, LongDay, ShortDay, ShortMonth } from "../../Types";
2
+ import Time from "./Time";
3
+ import TimeProperties from "./TimeProperties";
4
+ import TimeSpan, { TimeSpanFormat } from "./TimeSpan";
5
+
6
+ type Data = PartialExcept<BetterOmit<TimeProperties<true>, 'weeks'>, 'years' | 'months'>;
7
+ type Double = `${number}${number}`;
8
+ type Quadruple = `${Double}${Double}`;
9
+ type DateFormat = `${Double}/${Double}/${Quadruple}`;
10
+ /**
11
+ * Type used to construct DanhoDate.
12
+ * @Data Partial TimeProperties except years & months
13
+ * @DateFormat string as dd/MM/yyyy
14
+ */
15
+ export type DanhoDateConstructor = Data | DateFormat | number | Date;
16
+
17
+ class DanhoDate {
18
+ /**
19
+ * Returns the value of the current irl time
20
+ */
21
+ public static get now() {
22
+ return Date.now();
23
+ }
24
+
25
+ constructor(data?: DanhoDateConstructor) {
26
+ // data not provided
27
+ if (!data) this.date = new Date();
28
+ // data is typeof Date
29
+ else if (typeof data === 'object' && data instanceof Date) {
30
+ this.date = new Date(data);
31
+ }
32
+ // data is typeof Data
33
+ else if (typeof data === 'object') {
34
+ this.date = new Date(data.years, data.months - 1);
35
+ if (data.days) this.date.setDate(data.days);
36
+ if (data.hours) this.date.setHours(data.hours);
37
+ if (data.minutes) this.date.setMinutes(data.minutes);
38
+ if (data.seconds) this.date.setSeconds(data.seconds);
39
+ if (data.milliseconds) this.date.setMilliseconds(data.milliseconds);
40
+ }
41
+ // data is string or number
42
+ else this.date = new Date(data);
43
+
44
+ this._formats = new Map<string, string | number>([
45
+ ['year', this.year],
46
+ ['daysInMonth', this.daysInMonth],
47
+ ['monthShort', this.monthNameShort],
48
+ ['month', this.monthName],
49
+ ['MM', Time.DoubleDigit(this.month)],
50
+ ['M', this.month],
51
+
52
+ ['weekMonth', this.weekOfMonth],
53
+ ['weekDay', this.weekDay],
54
+ ['weekDayShort', this.weekDayShort],
55
+ ['week', this.week],
56
+
57
+ ['dd', Time.DoubleDigit(this.day)],
58
+ ['ddth', `${Time.DoubleDigit(this.day)}${Time.th(this.day)}`],
59
+ ['d', this.day],
60
+ ['dth', Time.th(this.day, true)],
61
+
62
+ ['hh12', `${Time.DoubleDigit(this.hours)}${this.hours < 12 ? 'AM' : 'PM'}`],
63
+ ['h12', `${this.hours > 12 ? 12 - this.hours : this.hours}${this.hours < 12 ? 'AM' : 'PM'}`],
64
+ ['hh24', Time.DoubleDigit(this.hours)],
65
+ ['h24', this.hours],
66
+ ['mm', Time.DoubleDigit(this.minutes)],
67
+ ['m', this.minutes],
68
+ ['ss', Time.DoubleDigit(this.seconds)],
69
+ ['s', this.seconds],
70
+ ['msms', Time.DoubleDigit(this.milliseconds)],
71
+ ['ms', this.milliseconds]
72
+ ]).map((v, k) => [k, v.toString()])
73
+ }
74
+
75
+ private _formats: Map<string, string>;
76
+ private _format(format: string) {
77
+ return format
78
+ .split(/\$/)
79
+ .map(v => v.split(/\W/)[0])
80
+ .filter(v => v)
81
+ .reduce((result, key) => result.replaceAll(key, this._formats.get(key)!), format.replaceAll('$', ''));
82
+ }
83
+
84
+ public date: Date
85
+
86
+ /**
87
+ * Year of the date
88
+ */
89
+ public get year(): number { return this.date.getFullYear() }
90
+ public set year(value: number) { this.date.setFullYear(value); }
91
+
92
+ /**
93
+ * Month of the date
94
+ */
95
+ public get month(): number { return this.date.getMonth() + 1 }
96
+ public set month(value: number) { this.date.setMonth(value) }
97
+
98
+ /**
99
+ * Days in the month of the date
100
+ */
101
+ public get daysInMonth() { return Time.daysInMonth[this.month - 1]; }
102
+
103
+ /**
104
+ * Week of the year the day is in
105
+ */
106
+ public get week(): number {
107
+ const firstMonday = new Date(this.year, 1, 1);
108
+
109
+ if (firstMonday.getDay() !== 1) {
110
+ const daysToMon = Time.DayNames.reverse().reduce((result, day, i) =>
111
+ result.set(day, i + 1)
112
+ , new Map<LongDay, number>()).get(Time.DayNames[firstMonday.getDay() - 1])!;
113
+ firstMonday.setDate(firstMonday.getDate() + daysToMon);
114
+ }
115
+
116
+ const timeDiff = Math.round(firstMonday.getTime() - this.time);
117
+ const timeSince = Time.millisecond % timeDiff;
118
+ const result = Math.ceil(timeSince / Time.week);
119
+ return result;
120
+ }
121
+ public set week(value: number) { this.date.setDate(value * Time.week / Time.day); }
122
+
123
+ /**
124
+ * Week of the month the day is in
125
+ */
126
+ public get weekOfMonth(): number { return Math.round(this.daysInMonth / (Time.week / Time.day)); }
127
+
128
+ /**
129
+ * Day of the date
130
+ */
131
+ public get day(): number { return this.date.getDate(); }
132
+ public set day(value: number) { this.date.setDate(value); }
133
+
134
+ public get dayOfWeek(): number { return this.date.getDay() }
135
+ public set dayOfWeek(value: number) {
136
+ const current = this.dayOfWeek;
137
+
138
+ if (value > current) var diff = value - current;
139
+ else diff = current - value;
140
+
141
+ this.day -= diff;
142
+ }
143
+
144
+ /**
145
+ * Hours of the date
146
+ */
147
+ public get hours(): number { return this.date.getHours(); }
148
+ public set hours(value: number) { this.date.setHours(value); }
149
+
150
+ public get isPM(): boolean {
151
+ return this.hours > 11;
152
+ }
153
+
154
+ /**
155
+ * Minutes of the date
156
+ */
157
+ public get minutes(): number { return this.date.getMinutes(); }
158
+ public set minutes(value: number) { this.date.setMinutes(value); }
159
+
160
+ /**
161
+ * Seconds of the date
162
+ */
163
+ public get seconds(): number { return this.date.getSeconds(); }
164
+ public set seconds(value: number) { this.date.setSeconds(value); }
165
+
166
+ /**
167
+ * Milliseconds of the date
168
+ */
169
+ public get milliseconds(): number { return this.date.getMilliseconds(); }
170
+ public set milliseconds(value: number) { this.date.setMilliseconds(value); }
171
+
172
+ /**
173
+ * Millisecond value of internal time
174
+ */
175
+ public get time(): number { return this.date.getTime(); }
176
+ public set time(value: number) { this.date.setTime(value) }
177
+
178
+ /**
179
+ * Week day i.e. Monday
180
+ */
181
+ public get weekDay(): LongDay { return Time.DayNames[this.dayOfWeek - 1] || Time.DayNames.at(-1); }
182
+ /**
183
+ * Short week day i.e. Mon
184
+ */
185
+ public get weekDayShort(): ShortDay { return this.weekDay.substring(0, 3) as ShortDay; }
186
+ /**
187
+ * Month name i.e. February
188
+ */
189
+ public get monthName(): LongMonth { return Time.MonthNames[this.month - 1] || Time.MonthNames.at(-1); }
190
+ /**
191
+ * Short month name i.e. Feb
192
+ */
193
+ public get monthNameShort(): ShortMonth { return this.monthName.substring(0, 3) as ShortMonth; }
194
+
195
+ /**
196
+ * Sets internal date property
197
+ * @param data Time properties to set - replacement of i.e. Date.setHours(value: number): number
198
+ * @returns This, with updated properties
199
+ */
200
+ public set(data: Partial<Data>) {
201
+ const { years, months, days, hours, minutes, seconds, milliseconds } = data;
202
+ const ymd = this.date.setFullYear(years || this.year, months, days);
203
+ const hmsms = new Date(ymd).setHours(hours || this.hours, minutes, seconds, milliseconds);
204
+ this.date = new Date(hmsms);
205
+ return this;
206
+ }
207
+
208
+ /**
209
+ * Calculates the time between this and provided date
210
+ * @param date Date information
211
+ * @returns TimeSpan between this and provided date
212
+ */
213
+ public between(date?: DanhoDate | DanhoDateConstructor): TimeSpan {
214
+ if (!date) return new TimeSpan(this.date, new Date());
215
+ else if (date instanceof DanhoDate) return new TimeSpan(this.date, date.date);
216
+ else if (date instanceof Date) return new TimeSpan(this.date, date);
217
+ else if (typeof date === 'object') return new DanhoDate(date).between(this.date);
218
+ return new TimeSpan(this.date, new Date(date));
219
+ }
220
+
221
+ /**
222
+ * String representation of this
223
+ * @param format String format of date
224
+ * @returns String representation of this
225
+ *
226
+ * @$year Replaced with year of the date i.e. 2022
227
+ *
228
+ * @$month Replaced with month name i.e. March
229
+ * @$daysInMonth Replaced with the amount of days in the month i.e. 31
230
+ * @$MM replaced with double digit month i.e. 01
231
+ * @$M replaced with single digit month i.e. 1
232
+ *
233
+ * @$week Replaced with week of the year i.e. 32
234
+ * @$weekOfMonth Replaced with the week of the month i.e. 3
235
+ *
236
+ * @$weekday Replaced with day of the week i.e. Wednesday
237
+ * @$dd Replaced with double digit day i.e. 02
238
+ * @$d Replaced with single digit day i.e. 2
239
+ * @$ddth Replaced with double digit day + "th" i.e. 05th
240
+ * @$dth Replaced with double digit day + "th" i.e. 5th
241
+ *
242
+ * @$hh12 Replaced with double digit hour in 12-hour format i.e. 09
243
+ * @$hh24 Replaced with double digit hour in 24-hour format i.e. 21
244
+ * @$h12 Replaced with single digit hour in 12-hour format i.e. 9
245
+ * @$h24 Replaced with single digit hour in 24-hour format i.e. 9
246
+ *
247
+ * @$mm Replaced with double digit minute i.e. 05
248
+ * @$m Replaced with single digit minute i.e. 5
249
+ *
250
+ * @$ss Replaced with double digit second i.e. 03
251
+ * @$s Replaced with single digit second i.e. 3
252
+ *
253
+ * @msms Replaced with double digit millisecond i.e. 02
254
+ * @ms Replaced with single digit millisecond i.e. 2
255
+ *
256
+ * @$relative Replaced with relative timeformat as TimeSpan
257
+ */
258
+ public toString(format = "$dd/$MM/$year", relativeFormat?: TimeSpanFormat) {
259
+ return this._format(format.replaceAll('$relative', this.between(new Date()).toString(relativeFormat)))
260
+ }
261
+
262
+ }
263
+ export { DanhoDate as Date }
264
+ export default DanhoDate;
@@ -0,0 +1,134 @@
1
+ import { LongDay, LongMonth } from "../../Types/Date";
2
+
3
+ /** '2s' or 2000 */
4
+ export type TimeUnit = 'ms' | 's' | 'm' | 'h' | 'd'| 'w' | 'M' | 'y';
5
+ export type TimeString = `${number}${TimeUnit}`;
6
+ export type TimeDelay = number | TimeString;
7
+ export const ValidTime = /^(\d+(?:\.|,)?\d*)(ms|s|m|h|d|w|M|y)$/;
8
+
9
+ /**
10
+ * Converts input into milliseconds
11
+ * @param input Input to convert to ms. 1s | 2m | 3h | 1M | 60000
12
+ * @returns Millisecond value of input
13
+ */
14
+ export function ms(input: TimeDelay) {
15
+ if (typeof input === 'number') return input;
16
+
17
+ const match = input.match(ValidTime);
18
+ if (!match) throw Error(`Invalid input string "${input}"`)
19
+ match.shift();
20
+
21
+ const [value, unit] = match;
22
+ // console.log({ value, unit, input, match });
23
+
24
+ const units = new Map<string, number>([
25
+ ['ms', Time.millisecond],
26
+ ['s', Time.second],
27
+ ['m', Time.minute],
28
+ ['h', Time.hour],
29
+ ['d', Time.day],
30
+ ['w', Time.week],
31
+ ['M', Time.month],
32
+ ['y', Time.year]
33
+ ]);
34
+
35
+ return parseInt(value) * (units.get(unit) as number);
36
+ }
37
+
38
+ /**
39
+ * Time utility class
40
+ * @borrows TimeDelay
41
+ * @borrows ms
42
+ */
43
+ export class Time {
44
+ /**
45
+ * Array of amount of days in the months. 0 indexed
46
+ */
47
+ public static get daysInMonth() {
48
+ return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
49
+ }
50
+
51
+ /**
52
+ * Amount of weeks per year
53
+ */
54
+ public static get weeksInYear() {
55
+ return Time.daysInYear * Time.day / Time.week
56
+ }
57
+
58
+ /**
59
+ * Amount of days per year
60
+ */
61
+ public static get daysInYear() {
62
+ return Time.year / Time.day;;
63
+ }
64
+
65
+ /**
66
+ * Returns function that converts value into double digit string
67
+ * @returns (value: number): string
68
+ */
69
+ public static DoubleDigit(value: number) {
70
+ return value.toString().length < 2 ? `0${value}` : value.toString();
71
+ }
72
+
73
+ public static th(value: number, includeValue = false) {
74
+ const th = (() => {
75
+ switch (value) {
76
+ case 1: return 'st';
77
+ case 2: return 'nd';
78
+ case 3: return 'rd';
79
+ default: return 'th';
80
+ }
81
+ })();
82
+ return includeValue ? value + th : th;
83
+ }
84
+
85
+ /**
86
+ * Array of names of the months. 0 idnexed
87
+ */
88
+ public static get MonthNames() {
89
+ return new Array<LongMonth>(
90
+ 'Janurary', 'February',
91
+ 'March', 'April', 'May',
92
+ 'June', 'July', 'August',
93
+ 'September', 'October', 'November',
94
+ 'December'
95
+ );
96
+ }
97
+
98
+ /**
99
+ * Array of names of the days of the week. 0 indexed
100
+ */
101
+ public static get DayNames() {
102
+ return new Array<LongDay>('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
103
+ }
104
+
105
+ /** Millisecond in milliseconds (I know that sounds weird but the others make sense) */
106
+ public static get millisecond(): number { return 1; }
107
+ /** Second in milliseconds */
108
+ public static get second(): number { return Time.millisecond * 1000; }
109
+ /** Minute in milliseconds */
110
+ public static get minute(): number { return Time.second * 60; }
111
+ /** Hour in milliseconds */
112
+ public static get hour(): number { return Time.minute * 60; }
113
+ /** Day in milliseconds */
114
+ public static get day(): number { return Time.hour * 24 }
115
+ /** Week in milliseconds */
116
+ public static get week(): number { return Time.day * 7; }
117
+ /** Month in milliseconds */
118
+ public static get month(): number {
119
+ return Time.avgMonth * Time.day;
120
+ }
121
+ /** Year in milliseconds */
122
+ public static get year(): number {
123
+ const now = new Date()
124
+ return (365 + (now.getFullYear() % 4 == 0 ? 1 : 0)) * Time.day
125
+ }
126
+ /** Average month in milliseconds */
127
+ public static get avgMonth(): number {
128
+ return Math.round(this.daysInMonth.reduce((result, num) => result += num, 0) / 12)
129
+ }
130
+ public static ms(input: TimeDelay) {
131
+ return ms(input);
132
+ }
133
+ }
134
+ export default Time;
@@ -0,0 +1,3 @@
1
+ type TimeKeys = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond';
2
+ export type TimeProperties<Plural extends boolean = false> = Record<Plural extends true ? `${TimeKeys}s` : TimeKeys, number>
3
+ export default TimeProperties;
@@ -0,0 +1,195 @@
1
+ import { TransformType } from "../../Types";
2
+ import DanhoDate, { DanhoDateConstructor } from "./Date";
3
+ import Time from "./Time";
4
+ import TimeProperties from "./TimeProperties";
5
+
6
+ /**
7
+ * What properties to include when using TimeSpan.toString(format: TimeSpanFormat): string
8
+ */
9
+ export type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean>>
10
+
11
+ /**
12
+ * Timespan between 2 dates.
13
+ * @borrows TimeSpanValue
14
+ * @borrows Time
15
+ * @borrows TimeProperties
16
+ */
17
+ export class TimeSpan implements TimeProperties<true> {
18
+ constructor(from: DanhoDateConstructor, to: DanhoDateConstructor = Date.now()) {
19
+ //General properties
20
+ this.from = new DanhoDate(from);
21
+ this.to = new DanhoDate(to);
22
+
23
+ //Ensure from < to
24
+ this._highest = this.from.time > this.to.time ? this.from : this.to;
25
+ this._lowest = this.from == this._highest ? this.to : this.from;
26
+ this.pastTense = this._highest !== this.to;
27
+ this._highest = new DanhoDate(this._highest.time + Time.millisecond);
28
+ let timeDifference = this._getTimeDifference();
29
+
30
+ //Calculate time difference between from & to and set to object properties
31
+ [this.years, timeDifference] = this._reduceTime(Time.year, timeDifference);
32
+ [this.months, timeDifference] = this._reduceTime(Time.month, timeDifference);
33
+ [this.weeks, timeDifference] = this._reduceTime(Time.week, timeDifference);
34
+ [this.days, timeDifference] = this._reduceTime(Time.day, timeDifference);
35
+ [this.hours, timeDifference] = this._reduceTime(Time.hour, timeDifference);
36
+ [this.minutes, timeDifference] = this._reduceTime(Time.minute, timeDifference);
37
+ [this.seconds, timeDifference] = this._reduceTime(Time.second, timeDifference);
38
+ [this.milliseconds, timeDifference] = this._reduceTime(Time.millisecond, timeDifference);
39
+ }
40
+
41
+ /**
42
+ * Which of the dates in constructor had the highest getTime()
43
+ */
44
+ private _highest: DanhoDate;
45
+ /**
46
+ * Which of the dates in constructor had the lowest getTime()
47
+ */
48
+ private _lowest: DanhoDate;
49
+
50
+ /**
51
+ * The time difference between highest and lowest
52
+ * @returns Time difference in ms between this._highest and this._lowest
53
+ */
54
+ private _getTimeDifference(): number {
55
+ return Math.round(this._highest.time - this._lowest.time)
56
+ }
57
+
58
+ /**
59
+ * Reduces ms into provided timeDifference
60
+ * @param ms Millisecond value to reduce
61
+ * @param timeDifference Time difference in ms that for each timeDifference in ms, add 1 to result
62
+ * @returns How many times timeDifference fits in ms
63
+ */
64
+ private _reduceTime(ms: number, timeDifference: number) {
65
+ let result = 0;
66
+
67
+ while (timeDifference > ms) {
68
+ timeDifference -= ms;
69
+ result++;
70
+ }
71
+
72
+ return [result, timeDifference];
73
+ }
74
+
75
+ /**
76
+ * Total years between dates
77
+ */
78
+ public years: number;
79
+ /**
80
+ * Total months between dates
81
+ */
82
+ public months: number;
83
+ /**
84
+ * Total weeks between dates
85
+ */
86
+ public weeks: number;
87
+ /**
88
+ * Total days between dates
89
+ */
90
+ public days: number;
91
+ /**
92
+ * Total hours between dates
93
+ */
94
+ public hours: number;
95
+ /**
96
+ * Total minutes between dates
97
+ */
98
+ public minutes: number;
99
+ /**
100
+ * Total seconds between dates
101
+ */
102
+ public seconds: number;
103
+ /**
104
+ * Total milliseconds between dates
105
+ */
106
+ public milliseconds: number;
107
+
108
+ /**
109
+ * Get the total unit time between the two dates
110
+ * @param unit Time unit in milliseconds
111
+ * @returns How many times that unit fits in total ms between the two dates
112
+ */
113
+ private _getTotalUnit(unit: number): number {
114
+ return this._reduceTime(unit, this._getTimeDifference())[0]
115
+ }
116
+
117
+ /**
118
+ * Get the maximum amount of months between the two dates
119
+ * @returns Number of max amount of months that are between the two dates
120
+ */
121
+ public getTotalMonths(): number {
122
+ return this._getTotalUnit(Time.month);
123
+ }
124
+ /**
125
+ * Get the maximum amount of weeks between the two dates
126
+ * @returns Number of max amount of weeks that are between the two dates
127
+ */
128
+ public getTotalWeeks(): number {
129
+ return this._getTotalUnit(Time.week);
130
+ }
131
+ /**
132
+ * Get the maximum amount of days between the two dates
133
+ * @returns Number of max amount of days that are between the two dates
134
+ */
135
+ public getTotalDays(): number {
136
+ return this._getTotalUnit(Time.day);
137
+ }
138
+ /**
139
+ * Get the maximum amount of hours between the two dates
140
+ * @returns Number of max amount of hours that are between the two dates
141
+ */
142
+ public getTotalHours(): number {
143
+ return this._getTotalUnit(Time.hour);
144
+ }
145
+ /**
146
+ * Get the maximum amount of minutes between the two dates
147
+ * @returns Number of max amount of minutes that are between the two dates
148
+ */
149
+ public getTotalMinutes(): number {
150
+ return this._getTotalUnit(Time.minute);
151
+ }
152
+ /**
153
+ * Get the maximum amount of seconds between the two dates
154
+ * @returns Number of max amount of seconds that are between the two dates
155
+ */
156
+ public getTotalSeconds(): number {
157
+ return this._getTotalUnit(Time.second)
158
+ }
159
+ /**
160
+ * Get the maximum amount of milliseconds between the two dates
161
+ * @returns Number of max amount of milliseconds that are between the two dates
162
+ */
163
+ public getTotalMilliseconds(): number {
164
+ return this._getTotalUnit(Time.millisecond);
165
+ }
166
+
167
+ /**
168
+ * Start date of timespan
169
+ */
170
+ public from: DanhoDate;
171
+ /**
172
+ * End date of timespan
173
+ */
174
+ public to: DanhoDate;
175
+ /**
176
+ * Timespan is in the past
177
+ */
178
+ public pastTense: boolean;
179
+
180
+ public toString(format?: TimeSpanFormat) {
181
+ //console.log(`${this.years}Y ${this.months}M ${this.weeks}w ${this.days}d ${this.hours}h ${this.minutes}m ${this.seconds}s ${this.milliseconds}ms`);
182
+ const times = [this.years, this.months, this.weeks, this.days, this.hours, this.minutes, this.seconds, this.milliseconds];
183
+ const timeMsg = ["year", "month", "week", "day", "hour", "minute", "second", "millisecond"];
184
+ const result = times.reduce((result, time, i) => (
185
+ // If time is above 0, format is provided, a property matches current property and said property is true, append addition else return result
186
+ time > 0 && (format ? timeMsg[i] in format && (format as any)[timeMsg[i]] === true : true) ?
187
+ `${result}${times[i]} ${timeMsg[i]}${times[i] != 1 ? 's' : ''}, ` :
188
+ result
189
+ ), '');
190
+
191
+ return result.length > 2 && result.substring(0, result.length - 2) || '';
192
+ }
193
+ }
194
+
195
+ export default TimeSpan;
@@ -0,0 +1,4 @@
1
+ export * from './Date';
2
+ export * from './Time';
3
+ export * from './TimeProperties';
4
+ export * from './TimeSpan';
@@ -1,3 +1,2 @@
1
- export * from './EventEmitter';
2
- export * from './EventCollection';
3
- export * from './Event';
1
+ export * from './Events';
2
+ export * from './Time';