danholibraryjs 1.10.0 → 2.0.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.
- package/dist/Classes/DanhoLogger.d.ts +23 -0
- package/dist/Classes/DanhoLogger.js +65 -0
- package/dist/Classes/Events/EventEmitter.d.ts +1 -1
- package/dist/Classes/Events/EventEmitter.js +1 -1
- package/dist/Classes/Time/Date.d.ts +1 -0
- package/dist/Classes/Time/Date.js +4 -1
- package/dist/Classes/Time/Time.d.ts +5 -4
- package/dist/Classes/Time/Time.js +7 -4
- package/dist/Classes/index.d.ts +1 -0
- package/dist/Classes/index.js +1 -0
- package/dist/Classes/store.d.ts +5 -9
- package/dist/Extensions/Array/array.extension.d.ts +42 -0
- package/dist/Extensions/Array/array.extension.js +57 -0
- package/dist/Extensions/Array/crud.extension.d.ts +24 -0
- package/dist/Extensions/Array/crud.extension.js +28 -0
- package/dist/Extensions/Array/index.d.ts +20 -0
- package/dist/Extensions/Array/index.js +40 -0
- package/dist/Extensions/Array/loop.extension.d.ts +18 -0
- package/dist/Extensions/Array/loop.extension.js +23 -0
- package/dist/Extensions/Array/random.extension.d.ts +23 -0
- package/dist/Extensions/Array/random.extension.js +35 -0
- package/dist/Extensions/Array/sort.extension.d.ts +27 -0
- package/dist/Extensions/Array/sort.extension.js +31 -0
- package/dist/Extensions/Array/string.extension.d.ts +13 -0
- package/dist/Extensions/Array/string.extension.js +14 -0
- package/dist/Extensions/Array.d.ts +17 -3
- package/dist/Extensions/Array.js +0 -12
- package/dist/Extensions/Function.d.ts +17 -2
- package/dist/Extensions/Function.js +15 -2
- package/dist/Extensions/Number.d.ts +13 -0
- package/dist/Extensions/Number.js +40 -0
- package/dist/Extensions/Object/arrays.extension.d.ts +17 -0
- package/dist/Extensions/Object/arrays.extension.js +13 -0
- package/dist/Extensions/Object/booleans.extension.d.ts +18 -0
- package/dist/Extensions/Object/booleans.extension.js +37 -0
- package/dist/Extensions/Object/extracts.extension.d.ts +38 -0
- package/dist/Extensions/Object/extracts.extension.js +72 -0
- package/dist/Extensions/Object/index.d.ts +8 -47
- package/dist/Extensions/Object/index.js +31 -33
- package/dist/Extensions/Object/properties.extension.d.ts +6 -0
- package/dist/Extensions/Object/properties.extension.js +4 -0
- package/dist/Extensions/Object/properties.js +1 -2
- package/dist/Extensions/String/case.extension.d.ts +12 -0
- package/dist/Extensions/String/case.extension.js +55 -0
- package/dist/Extensions/String/index.d.ts +4 -0
- package/dist/Extensions/String/index.js +30 -0
- package/dist/Extensions/index.d.ts +1 -12
- package/dist/Extensions/index.js +1 -9
- package/dist/Types/Able.d.ts +16 -0
- package/dist/Types/Able.js +2 -0
- package/dist/Types/Array.d.ts +6 -0
- package/dist/Types/Array.js +2 -0
- package/dist/Types/C#.d.ts +8 -0
- package/dist/Types/C#.js +2 -0
- package/dist/Types/Date.d.ts +1 -1
- package/dist/Types/Events.d.ts +2 -2
- package/dist/Types/Function.d.ts +5 -0
- package/dist/Types/Function.js +2 -0
- package/dist/Types/Object.d.ts +4 -0
- package/dist/Types/Object.js +2 -0
- package/dist/Types/PropertiesWith.d.ts +21 -0
- package/dist/Types/String.d.ts +1 -0
- package/dist/Types/String.js +2 -0
- package/dist/Types/TransformTypes.d.ts +9 -0
- package/dist/Types/index.d.ts +6 -28
- package/dist/Types/index.js +6 -0
- package/dist/Utils/ApiUtil/ApiTypes.d.ts +15 -0
- package/dist/Utils/ApiUtil/ApiTypes.js +15 -0
- package/dist/Utils/ApiUtil/RequestUtil.d.ts +19 -0
- package/dist/Utils/ApiUtil/RequestUtil.js +73 -0
- package/dist/Utils/ApiUtil/index.d.ts +20 -0
- package/dist/Utils/ApiUtil/index.js +33 -0
- package/dist/Utils/ApiUtils/ApiTypes.d.ts +15 -0
- package/dist/Utils/ApiUtils/ApiTypes.js +15 -0
- package/dist/Utils/ApiUtils/RequestUtil.d.ts +19 -0
- package/dist/Utils/ApiUtils/RequestUtil.js +73 -0
- package/dist/Utils/ApiUtils/index.d.ts +20 -0
- package/dist/Utils/ApiUtils/index.js +33 -0
- package/dist/Utils/ColorUtils.d.ts +11 -0
- package/dist/Utils/ColorUtils.js +93 -0
- package/dist/Utils/FormUtil.d.ts +6 -0
- package/dist/Utils/FormUtil.js +35 -0
- package/dist/Utils/FormUtils.d.ts +6 -0
- package/dist/Utils/FormUtils.js +35 -0
- package/dist/Utils/NumberUtils.d.ts +1 -0
- package/dist/Utils/NumberUtils.js +7 -0
- package/dist/Utils/PatcherUtils.d.ts +6 -0
- package/dist/Utils/PatcherUtils.js +80 -0
- package/dist/Utils/StringUtils.d.ts +3 -0
- package/dist/Utils/StringUtils.js +47 -0
- package/dist/Utils/TimeUtils/debounce.util.d.ts +22 -0
- package/dist/Utils/TimeUtils/debounce.util.js +78 -0
- package/dist/Utils/TimeUtils/functions.util.d.ts +4 -0
- package/dist/Utils/TimeUtils/functions.util.js +21 -0
- package/dist/Utils/TimeUtils/index.d.ts +15 -0
- package/dist/Utils/TimeUtils/index.js +34 -0
- package/dist/Utils/TimeUtils/throttle.util.d.ts +15 -0
- package/dist/Utils/TimeUtils/throttle.util.js +43 -0
- package/dist/Utils/index.d.ts +7 -0
- package/dist/Utils/index.js +23 -0
- package/package.json +4 -2
- package/src/Classes/DanhoLogger.ts +78 -0
- package/src/Classes/Events/Event.ts +96 -96
- package/src/Classes/Events/EventCollection.ts +90 -90
- package/src/Classes/Events/EventEmitter.ts +68 -68
- package/src/Classes/Time/Date.ts +219 -216
- package/src/Classes/Time/Time.ts +109 -104
- package/src/Classes/Time/TimeSpan.ts +171 -171
- package/src/Classes/index.ts +1 -0
- package/src/Classes/store.ts +22 -22
- package/src/Extensions/Array/array.extension.ts +103 -0
- package/src/Extensions/Array/crud.extension.ts +46 -0
- package/src/Extensions/Array/index.ts +15 -0
- package/src/Extensions/Array/loop.extension.ts +38 -0
- package/src/Extensions/Array/random.extension.ts +56 -0
- package/src/Extensions/Array/sort.extension.ts +52 -0
- package/src/Extensions/Array/string.extension.ts +22 -0
- package/src/Extensions/Document.ts +39 -39
- package/src/Extensions/Function.ts +37 -10
- package/src/Extensions/Map.ts +56 -56
- package/src/Extensions/Number.ts +50 -0
- package/src/Extensions/Object/arrays.extension.ts +27 -0
- package/src/Extensions/Object/booleans.extension.ts +46 -0
- package/src/Extensions/Object/extracts.extension.ts +102 -0
- package/src/Extensions/Object/index.ts +9 -80
- package/src/Extensions/Object/properties.extension.ts +11 -0
- package/src/Extensions/Object/properties.ts +35 -36
- package/src/Extensions/String/case.extension.ts +95 -0
- package/src/Extensions/String/index.ts +5 -0
- package/src/Extensions/index.ts +2 -20
- package/src/Interfaces/ElementOptions.ts +7 -7
- package/src/Interfaces/IReplacement.ts +2 -2
- package/src/Types/Able.ts +22 -0
- package/src/Types/Array.ts +7 -0
- package/src/Types/C#.ts +9 -0
- package/src/Types/Date.ts +1 -1
- package/src/Types/Events.ts +12 -12
- package/src/Types/Function.ts +10 -0
- package/src/Types/Object.ts +4 -0
- package/src/Types/PropertiesWith.ts +35 -4
- package/src/Types/String.ts +1 -0
- package/src/Types/TransformTypes.ts +23 -5
- package/src/Types/index.ts +7 -41
- package/src/Utils/ApiUtils/ApiTypes.ts +43 -0
- package/src/Utils/ApiUtils/RequestUtil.ts +87 -0
- package/src/Utils/ApiUtils/index.ts +39 -0
- package/src/Utils/ColorUtils.ts +102 -0
- package/src/Utils/FormUtils.ts +33 -0
- package/src/Utils/NumberUtils.ts +3 -0
- package/src/Utils/PatcherUtils.ts +111 -0
- package/src/Utils/StringUtils.ts +44 -0
- package/src/Utils/TimeUtils/debounce.util.ts +85 -0
- package/src/Utils/TimeUtils/functions.util.ts +18 -0
- package/src/Utils/TimeUtils/index.ts +9 -0
- package/src/Utils/TimeUtils/throttle.util.ts +44 -0
- package/src/Utils/index.ts +8 -0
- package/src/Extensions/Array.ts +0 -95
- package/src/Extensions/String.ts +0 -54
package/src/Classes/Time/Time.ts
CHANGED
|
@@ -1,38 +1,43 @@
|
|
|
1
|
+
import { Autocomplete } from "../../Types";
|
|
1
2
|
import { LongDay, LongMonth } from "../../Types/Date";
|
|
2
3
|
|
|
3
4
|
/** '2s' or 2000 */
|
|
4
|
-
export type TimeUnit = 'ms' | 's' | 'm' | 'h' | 'd'| 'w' | 'M' | 'y';
|
|
5
|
+
export type TimeUnit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y';
|
|
5
6
|
export type TimeString = `${number}${TimeUnit}`;
|
|
6
7
|
export type TimeDelay = number | TimeString;
|
|
7
8
|
export const ValidTime = /^(\d+(?:\.|,)?\d*)(ms|s|m|h|d|w|M|y)$/;
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
|
-
* Converts input into milliseconds
|
|
11
|
+
* Converts input into milliseconds. Supports multiple time units in one string by space separation. E.g. 1h 30m
|
|
11
12
|
* @param input Input to convert to ms. 1s | 2m | 3h | 1M | 60000
|
|
12
13
|
* @returns Millisecond value of input
|
|
13
14
|
*/
|
|
14
|
-
export function ms(input: TimeDelay) {
|
|
15
|
-
|
|
15
|
+
export function ms(input: Autocomplete<TimeDelay>) {
|
|
16
|
+
return input.toString().split(' ').reduce((result, unit) => result += msSingular(unit as TimeDelay), 0);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function msSingular(input: TimeDelay) {
|
|
20
|
+
if (typeof input === 'number') return input;
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
const match = input.match(ValidTime);
|
|
23
|
+
if (!match) throw Error(`Invalid input string "${input}"`);
|
|
24
|
+
match.shift();
|
|
20
25
|
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
]);
|
|
26
|
+
const [value, unit] = match;
|
|
27
|
+
// console.log({ value, unit, input, match });
|
|
34
28
|
|
|
35
|
-
|
|
29
|
+
const units = new Map<string, number>([
|
|
30
|
+
['ms', Time.millisecond],
|
|
31
|
+
['s', Time.second],
|
|
32
|
+
['m', Time.minute],
|
|
33
|
+
['h', Time.hour],
|
|
34
|
+
['d', Time.day],
|
|
35
|
+
['w', Time.week],
|
|
36
|
+
['M', Time.month],
|
|
37
|
+
['y', Time.year]
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
return parseInt(value) * (units.get(unit) as number);
|
|
36
41
|
}
|
|
37
42
|
|
|
38
43
|
/**
|
|
@@ -41,94 +46,94 @@ export function ms(input: TimeDelay) {
|
|
|
41
46
|
* @borrows ms
|
|
42
47
|
*/
|
|
43
48
|
export class Time {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Array of amount of days in the months. 0 indexed
|
|
51
|
+
*/
|
|
52
|
+
public static get daysInMonth() {
|
|
53
|
+
return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Amount of weeks per year
|
|
58
|
+
*/
|
|
59
|
+
public static get weeksInYear() {
|
|
60
|
+
return Time.daysInYear * Time.day / Time.week;
|
|
61
|
+
}
|
|
50
62
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Amount of days per year
|
|
65
|
+
*/
|
|
66
|
+
public static get daysInYear() {
|
|
67
|
+
return Time.year / Time.day;;
|
|
68
|
+
}
|
|
57
69
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns function that converts value into double digit string
|
|
72
|
+
* @returns (value: number): string
|
|
73
|
+
*/
|
|
74
|
+
public static DoubleDigit(value: number) {
|
|
75
|
+
return value.toString().length < 2 ? `0${value}` : value.toString();
|
|
76
|
+
}
|
|
64
77
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
78
|
+
public static th(value: number, includeValue = false) {
|
|
79
|
+
const th = (() => {
|
|
80
|
+
switch (value) {
|
|
81
|
+
case 1: return 'st';
|
|
82
|
+
case 2: return 'nd';
|
|
83
|
+
case 3: return 'rd';
|
|
84
|
+
default: return 'th';
|
|
85
|
+
}
|
|
86
|
+
})();
|
|
87
|
+
return includeValue ? value + th : th;
|
|
88
|
+
}
|
|
72
89
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Array of names of the months. 0 indexed
|
|
92
|
+
*/
|
|
93
|
+
public static get MonthNames() {
|
|
94
|
+
return new Array<LongMonth>(
|
|
95
|
+
'January', 'February',
|
|
96
|
+
'March', 'April', 'May',
|
|
97
|
+
'June', 'July', 'August',
|
|
98
|
+
'September', 'October', 'November',
|
|
99
|
+
'December'
|
|
100
|
+
);
|
|
101
|
+
}
|
|
84
102
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
'March', 'April', 'May',
|
|
92
|
-
'June', 'July', 'August',
|
|
93
|
-
'September', 'October', 'November',
|
|
94
|
-
'December'
|
|
95
|
-
);
|
|
96
|
-
}
|
|
103
|
+
/**
|
|
104
|
+
* Array of names of the days of the week. 0 indexed
|
|
105
|
+
*/
|
|
106
|
+
public static get DayNames() {
|
|
107
|
+
return new Array<LongDay>('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
|
|
108
|
+
}
|
|
97
109
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
}
|
|
110
|
+
/** Millisecond in milliseconds (I know that sounds weird but the others make sense) */
|
|
111
|
+
public static get millisecond(): number { return 1; }
|
|
112
|
+
/** Second in milliseconds */
|
|
113
|
+
public static get second(): number { return Time.millisecond * 1000; }
|
|
114
|
+
/** Minute in milliseconds */
|
|
115
|
+
public static get minute(): number { return Time.second * 60; }
|
|
116
|
+
/** Hour in milliseconds */
|
|
117
|
+
public static get hour(): number { return Time.minute * 60; }
|
|
118
|
+
/** Day in milliseconds */
|
|
119
|
+
public static get day(): number { return Time.hour * 24; }
|
|
120
|
+
/** Week in milliseconds */
|
|
121
|
+
public static get week(): number { return Time.day * 7; }
|
|
122
|
+
/** Month in milliseconds */
|
|
123
|
+
public static get month(): number {
|
|
124
|
+
return Time.avgMonth * Time.day;
|
|
125
|
+
}
|
|
126
|
+
/** Year in milliseconds */
|
|
127
|
+
public static get year(): number {
|
|
128
|
+
const now = new Date();
|
|
129
|
+
return (365 + (now.getFullYear() % 4 == 0 ? 1 : 0)) * Time.day;
|
|
130
|
+
}
|
|
131
|
+
/** Average month in milliseconds */
|
|
132
|
+
public static get avgMonth(): number {
|
|
133
|
+
return Math.round(this.daysInMonth.reduce((result, num) => result += num, 0) / 12);
|
|
134
|
+
}
|
|
135
|
+
public static ms(input: Autocomplete<TimeDelay>) {
|
|
136
|
+
return ms(input);
|
|
137
|
+
}
|
|
133
138
|
}
|
|
134
139
|
export default Time;
|
|
@@ -6,7 +6,7 @@ import TimeProperties from "./TimeProperties";
|
|
|
6
6
|
/**
|
|
7
7
|
* What properties to include when using TimeSpan.toString(format: TimeSpanFormat): string
|
|
8
8
|
*/
|
|
9
|
-
export type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean
|
|
9
|
+
export type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number, boolean>>;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Timespan between 2 dates.
|
|
@@ -15,181 +15,181 @@ export type TimeSpanFormat = Partial<TransformType<TimeProperties<true>, number,
|
|
|
15
15
|
* @borrows TimeProperties
|
|
16
16
|
*/
|
|
17
17
|
export class TimeSpan implements TimeProperties<true> {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
}
|
|
18
|
+
constructor(from: DanhoDateConstructor, to: DanhoDateConstructor = Date.now()) {
|
|
19
|
+
//General properties
|
|
20
|
+
this.from = new DanhoDate(from);
|
|
21
|
+
this.to = new DanhoDate(to);
|
|
40
22
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
}
|
|
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();
|
|
57
29
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
timeDifference -= ms;
|
|
69
|
-
result++;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return [result, timeDifference];
|
|
73
|
-
}
|
|
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
|
+
}
|
|
74
40
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
}
|
|
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;
|
|
116
49
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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) || '';
|
|
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++;
|
|
192
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
193
|
}
|
|
194
194
|
|
|
195
195
|
export default TimeSpan;
|
package/src/Classes/index.ts
CHANGED