namirasoft-core 1.4.12 → 1.4.13
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/EncryptionOperation.js +17 -7
- package/dist/EncryptionOperation.js.map +1 -1
- package/dist/HashOperation.js +17 -7
- package/dist/HashOperation.js.map +1 -1
- package/dist/PhoneOperation.js +17 -7
- package/dist/PhoneOperation.js.map +1 -1
- package/dist/SortItem.d.ts +24 -0
- package/dist/SortItem.js +75 -0
- package/dist/SortItem.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/BaseDatabaseRow.ts +6 -6
- package/src/BaseMetaColumn.ts +13 -13
- package/src/BaseMetaTable.ts +24 -24
- package/src/BaseServer.ts +107 -107
- package/src/CacheService.ts +57 -57
- package/src/ConsoleOperation.ts +68 -68
- package/src/ConvertService.ts +100 -100
- package/src/CookieService.ts +33 -33
- package/src/Countries.ts +486 -486
- package/src/Country.ts +21 -21
- package/src/CountryOperation.ts +98 -98
- package/src/EncodingOperation.ts +12 -12
- package/src/EncryptionOperation.ts +40 -40
- package/src/EnvService.ts +22 -22
- package/src/ErrorOperation.ts +13 -13
- package/src/FileOperation.ts +57 -57
- package/src/FilterItem.ts +128 -128
- package/src/FilterItemColumnType.ts +6 -6
- package/src/FilterItemOperator.ts +51 -51
- package/src/GeoOperation.ts +18 -18
- package/src/HTTPError.ts +8 -8
- package/src/HTTPMethod.ts +6 -6
- package/src/HashOperation.ts +24 -24
- package/src/IStorage.ts +5 -5
- package/src/IStorageCookie.ts +52 -52
- package/src/IStorageJsonFile.ts +45 -45
- package/src/IStorageLocal.ts +16 -16
- package/src/IStorageMemory.ts +17 -17
- package/src/NamingConvention.ts +107 -107
- package/src/ObjectService.ts +27 -27
- package/src/PackageService.ts +76 -76
- package/src/PhoneOperation.ts +8 -8
- package/src/PriceOperation.ts +18 -18
- package/src/RegexTemplate.ts +7 -7
- package/src/SearchOperation.ts +29 -29
- package/src/SortItem.ts +89 -0
- package/src/StringOperation.ts +18 -18
- package/src/TimeOperation.ts +262 -262
- package/src/URLOperation.ts +54 -54
- package/src/VersionOperation.ts +46 -46
- package/src/index.ts +40 -39
package/src/TimeOperation.ts
CHANGED
|
@@ -1,263 +1,263 @@
|
|
|
1
|
-
import moment from "moment";
|
|
2
|
-
|
|
3
|
-
export type YMD = { year: number, month: number, day: number };
|
|
4
|
-
export type DateInterval = { start_date: Date, end_date: Date };
|
|
5
|
-
export class TimeOperation
|
|
6
|
-
{
|
|
7
|
-
static toDBFormat(date: Date): string
|
|
8
|
-
{
|
|
9
|
-
return date.toISOString().
|
|
10
|
-
replace(/T/, ' ').
|
|
11
|
-
replace(/\..+/, '');
|
|
12
|
-
}
|
|
13
|
-
static toDate(year: number, month: number, day: number): Date
|
|
14
|
-
{
|
|
15
|
-
return new Date(`${year}/${month}/${day}`);
|
|
16
|
-
}
|
|
17
|
-
static toDateByWeek(year: number, week: number, firstDay: number): Date
|
|
18
|
-
{
|
|
19
|
-
if (!firstDay)
|
|
20
|
-
firstDay = 0;
|
|
21
|
-
let offset = new Date(year, 0, 1).getDay();
|
|
22
|
-
return new Date(year, 0, 1 + firstDay - offset + (week - 1) * 7, 0, 0, 0, 0);
|
|
23
|
-
}
|
|
24
|
-
static fromDate(date: Date): YMD
|
|
25
|
-
{
|
|
26
|
-
let year = this.yearNumber(date);
|
|
27
|
-
let month = this.monthNumber(date);
|
|
28
|
-
let day = this.dayNumber(date);
|
|
29
|
-
return { year, month, day };
|
|
30
|
-
}
|
|
31
|
-
static getLARange(date: Date): DateInterval
|
|
32
|
-
{
|
|
33
|
-
let start_date = this.hoursLater(8, date);
|
|
34
|
-
let end_date = this.daysLater(1, start_date);
|
|
35
|
-
return {
|
|
36
|
-
start_date,
|
|
37
|
-
end_date
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
static diffInSecond(t1: Date, t2: Date, abs: boolean): number
|
|
41
|
-
{
|
|
42
|
-
let ans = (t1.getTime() - t2.getTime()) / 1000;
|
|
43
|
-
if (abs)
|
|
44
|
-
ans = Math.abs(ans);
|
|
45
|
-
return ans;
|
|
46
|
-
}
|
|
47
|
-
static diffInMinute(t1: Date, t2: Date, abs: boolean): number
|
|
48
|
-
{
|
|
49
|
-
return this.diffInSecond(t1, t2, abs) / 60;
|
|
50
|
-
}
|
|
51
|
-
//year
|
|
52
|
-
static yearNumber(date: Date | null = null): number
|
|
53
|
-
{
|
|
54
|
-
if (!date)
|
|
55
|
-
date = new Date();
|
|
56
|
-
return date.getUTCFullYear();
|
|
57
|
-
}
|
|
58
|
-
static beginningOfYear(date: Date | null = null): Date
|
|
59
|
-
{
|
|
60
|
-
date = this.beginningOfMonth(date);
|
|
61
|
-
date.setUTCMonth(0);
|
|
62
|
-
return date;
|
|
63
|
-
}
|
|
64
|
-
//month
|
|
65
|
-
static monthNumber(date: Date | null = null): number
|
|
66
|
-
{
|
|
67
|
-
if (!date)
|
|
68
|
-
date = new Date();
|
|
69
|
-
return date.getUTCMonth() + 1;
|
|
70
|
-
}
|
|
71
|
-
static beginningOfMonth(date: Date | null = null): Date
|
|
72
|
-
{
|
|
73
|
-
date = this.beginningOfDay(date);
|
|
74
|
-
date.setUTCDate(1);
|
|
75
|
-
return date;
|
|
76
|
-
}
|
|
77
|
-
static endOfMonth(date: Date | null = null): Date
|
|
78
|
-
{
|
|
79
|
-
date = this.beginningOfMonth(date);
|
|
80
|
-
date = this.monthsLater(1, date);
|
|
81
|
-
date = this.secondsAgo(1, date);
|
|
82
|
-
return date;
|
|
83
|
-
}
|
|
84
|
-
//week
|
|
85
|
-
static weekNumber(date: Date | null = null): number
|
|
86
|
-
{
|
|
87
|
-
if (!date)
|
|
88
|
-
date = new Date();
|
|
89
|
-
let first = this.beginningOfYear(date);
|
|
90
|
-
let secs = this.diffInSecond(date, first, true);
|
|
91
|
-
return (secs / 60 / 60 / 24 / 7) + 1;
|
|
92
|
-
}
|
|
93
|
-
static beginningOfWeek(date: Date | null = null): Date
|
|
94
|
-
{
|
|
95
|
-
date = this.beginningOfDay(date);
|
|
96
|
-
let day = (date.getDay() - 1) % 7;
|
|
97
|
-
return this.daysAgo(day, date);
|
|
98
|
-
}
|
|
99
|
-
static endOfWeek(date: Date | null = null): Date
|
|
100
|
-
{
|
|
101
|
-
date = this.beginningOfWeek(date);
|
|
102
|
-
return this.secondsAgo(1, this.daysLater(7, date));
|
|
103
|
-
}
|
|
104
|
-
// day
|
|
105
|
-
static dayNumber(date: Date | null = null): number
|
|
106
|
-
{
|
|
107
|
-
if (!date)
|
|
108
|
-
date = new Date();
|
|
109
|
-
return date.getUTCDate();
|
|
110
|
-
}
|
|
111
|
-
static beginningOfDay(date: Date | null = null): Date
|
|
112
|
-
{
|
|
113
|
-
date = this.beginningOfHour(date);
|
|
114
|
-
date.setUTCHours(0);
|
|
115
|
-
return date;
|
|
116
|
-
}
|
|
117
|
-
static endOfDay(date: Date | null = null): Date
|
|
118
|
-
{
|
|
119
|
-
date = this.beginningOfDay(date);
|
|
120
|
-
return this.daysLater(1, date);
|
|
121
|
-
}
|
|
122
|
-
static dayNumberInYear(date: Date | null = null): number
|
|
123
|
-
{
|
|
124
|
-
if (!date)
|
|
125
|
-
date = new Date();
|
|
126
|
-
var start = new Date(date.getFullYear(), 0, 0);
|
|
127
|
-
var diff = date.getTime() - start.getTime();
|
|
128
|
-
var oneDay = 1000 * 60 * 60 * 24;
|
|
129
|
-
return Math.floor(diff / oneDay);
|
|
130
|
-
}
|
|
131
|
-
static addedDaysInMonth(days: number, date: Date): Date
|
|
132
|
-
{
|
|
133
|
-
date = this.beginningOfMonth(date);
|
|
134
|
-
return this.daysLater(days, date);
|
|
135
|
-
}
|
|
136
|
-
//hour
|
|
137
|
-
static hourNumberInWeek(date: Date | null = null): number
|
|
138
|
-
{
|
|
139
|
-
if (!date)
|
|
140
|
-
date = new Date();
|
|
141
|
-
let first = this.beginningOfWeek(date);
|
|
142
|
-
let secs = this.diffInSecond(date, first, true);
|
|
143
|
-
return (secs / 3600);
|
|
144
|
-
}
|
|
145
|
-
static hourNumberInMonth(date: Date | null = null): number
|
|
146
|
-
{
|
|
147
|
-
if (!date)
|
|
148
|
-
date = new Date();
|
|
149
|
-
let first = this.beginningOfMonth(date);
|
|
150
|
-
let secs = this.diffInSecond(date, first, true);
|
|
151
|
-
return (secs / 3600);
|
|
152
|
-
}
|
|
153
|
-
static addedHoursInWeek(hours: number, date: Date): Date
|
|
154
|
-
{
|
|
155
|
-
date = this.beginningOfWeek(date);
|
|
156
|
-
return this.hoursLater(hours, date);
|
|
157
|
-
}
|
|
158
|
-
static beginningOfHour(date: Date | null = null): Date
|
|
159
|
-
{
|
|
160
|
-
if (!date)
|
|
161
|
-
date = new Date();
|
|
162
|
-
else
|
|
163
|
-
date = new Date(date);
|
|
164
|
-
date.setUTCMilliseconds(0);
|
|
165
|
-
date.setUTCSeconds(0);
|
|
166
|
-
date.setUTCMinutes(0);
|
|
167
|
-
return date;
|
|
168
|
-
}
|
|
169
|
-
// ago
|
|
170
|
-
static monthsAgo(months: number, date: Date): Date
|
|
171
|
-
{
|
|
172
|
-
return this.monthsLater(months * -1, date);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
static daysAgo(days: number, date: Date): Date
|
|
176
|
-
{
|
|
177
|
-
return this.daysLater(days * -1, date);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
static hoursAgo(hours: number, date: Date): Date
|
|
181
|
-
{
|
|
182
|
-
return this.hoursLater(hours * -1, date);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
static minutesAgo(minutes: number, date: Date): Date
|
|
186
|
-
{
|
|
187
|
-
return this.minutesLater(minutes * -1, date);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
static secondsAgo(seconds: number, date: Date): Date
|
|
191
|
-
{
|
|
192
|
-
return this.secondsLater(seconds * -1, date);
|
|
193
|
-
}
|
|
194
|
-
// later
|
|
195
|
-
static later(num: number, type: moment.DurationInputArg2, date: Date | null = null): Date
|
|
196
|
-
{
|
|
197
|
-
if (!date)
|
|
198
|
-
date = new Date();
|
|
199
|
-
else
|
|
200
|
-
date = new Date(date);
|
|
201
|
-
return moment(date).add(num, type).toDate();
|
|
202
|
-
}
|
|
203
|
-
static monthsLater(months: number, date: Date): Date
|
|
204
|
-
{
|
|
205
|
-
return this.later(months, 'months', date);
|
|
206
|
-
}
|
|
207
|
-
static daysLater(days: number, date: Date): Date
|
|
208
|
-
{
|
|
209
|
-
return this.later(days, 'days', date);
|
|
210
|
-
}
|
|
211
|
-
static hoursLater(hours: number, date: Date): Date
|
|
212
|
-
{
|
|
213
|
-
return this.later(hours, 'hours', date);
|
|
214
|
-
}
|
|
215
|
-
static minutesLater(minutes: number, date: Date): Date
|
|
216
|
-
{
|
|
217
|
-
return this.later(minutes, 'minutes', date);
|
|
218
|
-
}
|
|
219
|
-
static secondsLater(seconds: number, date: Date): Date
|
|
220
|
-
{
|
|
221
|
-
return this.later(seconds, 'seconds', date);
|
|
222
|
-
}
|
|
223
|
-
static getDuration(startDate: Date, endDate: Date, zeroAllOnPassed: boolean = false)
|
|
224
|
-
{
|
|
225
|
-
if (!endDate)
|
|
226
|
-
endDate = new Date();
|
|
227
|
-
let s = moment(startDate);
|
|
228
|
-
let e = moment(endDate);
|
|
229
|
-
let passed = e < s;
|
|
230
|
-
let year = e.diff(s, 'years', false);
|
|
231
|
-
s.add(year, 'years');
|
|
232
|
-
let month = e.diff(s, 'months', false);
|
|
233
|
-
s.add(month, 'months');
|
|
234
|
-
let day = e.diff(s, 'days', false);
|
|
235
|
-
s.add(day, 'days');
|
|
236
|
-
let hour = e.diff(s, 'hours', false);
|
|
237
|
-
s.add(hour, 'hours');
|
|
238
|
-
let minute = e.diff(s, 'minutes', false);
|
|
239
|
-
s.add(minute, 'minutes');
|
|
240
|
-
let second = e.diff(s, 'seconds', false);
|
|
241
|
-
s.add(second, 'seconds');
|
|
242
|
-
|
|
243
|
-
if (passed)
|
|
244
|
-
if (zeroAllOnPassed)
|
|
245
|
-
year = month = day = hour = minute = second = 0;
|
|
246
|
-
let total = year * 12 + month;
|
|
247
|
-
total = total * 30 + day;
|
|
248
|
-
total = total * 24 + hour;
|
|
249
|
-
total = total * 60 + minute;
|
|
250
|
-
total += second;
|
|
251
|
-
|
|
252
|
-
return {
|
|
253
|
-
passed, year, month, day, hour, minute, second, total
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
static calculateAge(date: Date): number
|
|
257
|
-
{
|
|
258
|
-
let birthday = new Date(date);
|
|
259
|
-
let ageDifMs = Date.now() - birthday.getTime();
|
|
260
|
-
var ageDate = new Date(ageDifMs); // miliseconds from epoch
|
|
261
|
-
return Math.abs(ageDate.getUTCFullYear() - 1970);
|
|
262
|
-
}
|
|
1
|
+
import moment from "moment";
|
|
2
|
+
|
|
3
|
+
export type YMD = { year: number, month: number, day: number };
|
|
4
|
+
export type DateInterval = { start_date: Date, end_date: Date };
|
|
5
|
+
export class TimeOperation
|
|
6
|
+
{
|
|
7
|
+
static toDBFormat(date: Date): string
|
|
8
|
+
{
|
|
9
|
+
return date.toISOString().
|
|
10
|
+
replace(/T/, ' ').
|
|
11
|
+
replace(/\..+/, '');
|
|
12
|
+
}
|
|
13
|
+
static toDate(year: number, month: number, day: number): Date
|
|
14
|
+
{
|
|
15
|
+
return new Date(`${year}/${month}/${day}`);
|
|
16
|
+
}
|
|
17
|
+
static toDateByWeek(year: number, week: number, firstDay: number): Date
|
|
18
|
+
{
|
|
19
|
+
if (!firstDay)
|
|
20
|
+
firstDay = 0;
|
|
21
|
+
let offset = new Date(year, 0, 1).getDay();
|
|
22
|
+
return new Date(year, 0, 1 + firstDay - offset + (week - 1) * 7, 0, 0, 0, 0);
|
|
23
|
+
}
|
|
24
|
+
static fromDate(date: Date): YMD
|
|
25
|
+
{
|
|
26
|
+
let year = this.yearNumber(date);
|
|
27
|
+
let month = this.monthNumber(date);
|
|
28
|
+
let day = this.dayNumber(date);
|
|
29
|
+
return { year, month, day };
|
|
30
|
+
}
|
|
31
|
+
static getLARange(date: Date): DateInterval
|
|
32
|
+
{
|
|
33
|
+
let start_date = this.hoursLater(8, date);
|
|
34
|
+
let end_date = this.daysLater(1, start_date);
|
|
35
|
+
return {
|
|
36
|
+
start_date,
|
|
37
|
+
end_date
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
static diffInSecond(t1: Date, t2: Date, abs: boolean): number
|
|
41
|
+
{
|
|
42
|
+
let ans = (t1.getTime() - t2.getTime()) / 1000;
|
|
43
|
+
if (abs)
|
|
44
|
+
ans = Math.abs(ans);
|
|
45
|
+
return ans;
|
|
46
|
+
}
|
|
47
|
+
static diffInMinute(t1: Date, t2: Date, abs: boolean): number
|
|
48
|
+
{
|
|
49
|
+
return this.diffInSecond(t1, t2, abs) / 60;
|
|
50
|
+
}
|
|
51
|
+
//year
|
|
52
|
+
static yearNumber(date: Date | null = null): number
|
|
53
|
+
{
|
|
54
|
+
if (!date)
|
|
55
|
+
date = new Date();
|
|
56
|
+
return date.getUTCFullYear();
|
|
57
|
+
}
|
|
58
|
+
static beginningOfYear(date: Date | null = null): Date
|
|
59
|
+
{
|
|
60
|
+
date = this.beginningOfMonth(date);
|
|
61
|
+
date.setUTCMonth(0);
|
|
62
|
+
return date;
|
|
63
|
+
}
|
|
64
|
+
//month
|
|
65
|
+
static monthNumber(date: Date | null = null): number
|
|
66
|
+
{
|
|
67
|
+
if (!date)
|
|
68
|
+
date = new Date();
|
|
69
|
+
return date.getUTCMonth() + 1;
|
|
70
|
+
}
|
|
71
|
+
static beginningOfMonth(date: Date | null = null): Date
|
|
72
|
+
{
|
|
73
|
+
date = this.beginningOfDay(date);
|
|
74
|
+
date.setUTCDate(1);
|
|
75
|
+
return date;
|
|
76
|
+
}
|
|
77
|
+
static endOfMonth(date: Date | null = null): Date
|
|
78
|
+
{
|
|
79
|
+
date = this.beginningOfMonth(date);
|
|
80
|
+
date = this.monthsLater(1, date);
|
|
81
|
+
date = this.secondsAgo(1, date);
|
|
82
|
+
return date;
|
|
83
|
+
}
|
|
84
|
+
//week
|
|
85
|
+
static weekNumber(date: Date | null = null): number
|
|
86
|
+
{
|
|
87
|
+
if (!date)
|
|
88
|
+
date = new Date();
|
|
89
|
+
let first = this.beginningOfYear(date);
|
|
90
|
+
let secs = this.diffInSecond(date, first, true);
|
|
91
|
+
return (secs / 60 / 60 / 24 / 7) + 1;
|
|
92
|
+
}
|
|
93
|
+
static beginningOfWeek(date: Date | null = null): Date
|
|
94
|
+
{
|
|
95
|
+
date = this.beginningOfDay(date);
|
|
96
|
+
let day = (date.getDay() - 1) % 7;
|
|
97
|
+
return this.daysAgo(day, date);
|
|
98
|
+
}
|
|
99
|
+
static endOfWeek(date: Date | null = null): Date
|
|
100
|
+
{
|
|
101
|
+
date = this.beginningOfWeek(date);
|
|
102
|
+
return this.secondsAgo(1, this.daysLater(7, date));
|
|
103
|
+
}
|
|
104
|
+
// day
|
|
105
|
+
static dayNumber(date: Date | null = null): number
|
|
106
|
+
{
|
|
107
|
+
if (!date)
|
|
108
|
+
date = new Date();
|
|
109
|
+
return date.getUTCDate();
|
|
110
|
+
}
|
|
111
|
+
static beginningOfDay(date: Date | null = null): Date
|
|
112
|
+
{
|
|
113
|
+
date = this.beginningOfHour(date);
|
|
114
|
+
date.setUTCHours(0);
|
|
115
|
+
return date;
|
|
116
|
+
}
|
|
117
|
+
static endOfDay(date: Date | null = null): Date
|
|
118
|
+
{
|
|
119
|
+
date = this.beginningOfDay(date);
|
|
120
|
+
return this.daysLater(1, date);
|
|
121
|
+
}
|
|
122
|
+
static dayNumberInYear(date: Date | null = null): number
|
|
123
|
+
{
|
|
124
|
+
if (!date)
|
|
125
|
+
date = new Date();
|
|
126
|
+
var start = new Date(date.getFullYear(), 0, 0);
|
|
127
|
+
var diff = date.getTime() - start.getTime();
|
|
128
|
+
var oneDay = 1000 * 60 * 60 * 24;
|
|
129
|
+
return Math.floor(diff / oneDay);
|
|
130
|
+
}
|
|
131
|
+
static addedDaysInMonth(days: number, date: Date): Date
|
|
132
|
+
{
|
|
133
|
+
date = this.beginningOfMonth(date);
|
|
134
|
+
return this.daysLater(days, date);
|
|
135
|
+
}
|
|
136
|
+
//hour
|
|
137
|
+
static hourNumberInWeek(date: Date | null = null): number
|
|
138
|
+
{
|
|
139
|
+
if (!date)
|
|
140
|
+
date = new Date();
|
|
141
|
+
let first = this.beginningOfWeek(date);
|
|
142
|
+
let secs = this.diffInSecond(date, first, true);
|
|
143
|
+
return (secs / 3600);
|
|
144
|
+
}
|
|
145
|
+
static hourNumberInMonth(date: Date | null = null): number
|
|
146
|
+
{
|
|
147
|
+
if (!date)
|
|
148
|
+
date = new Date();
|
|
149
|
+
let first = this.beginningOfMonth(date);
|
|
150
|
+
let secs = this.diffInSecond(date, first, true);
|
|
151
|
+
return (secs / 3600);
|
|
152
|
+
}
|
|
153
|
+
static addedHoursInWeek(hours: number, date: Date): Date
|
|
154
|
+
{
|
|
155
|
+
date = this.beginningOfWeek(date);
|
|
156
|
+
return this.hoursLater(hours, date);
|
|
157
|
+
}
|
|
158
|
+
static beginningOfHour(date: Date | null = null): Date
|
|
159
|
+
{
|
|
160
|
+
if (!date)
|
|
161
|
+
date = new Date();
|
|
162
|
+
else
|
|
163
|
+
date = new Date(date);
|
|
164
|
+
date.setUTCMilliseconds(0);
|
|
165
|
+
date.setUTCSeconds(0);
|
|
166
|
+
date.setUTCMinutes(0);
|
|
167
|
+
return date;
|
|
168
|
+
}
|
|
169
|
+
// ago
|
|
170
|
+
static monthsAgo(months: number, date: Date): Date
|
|
171
|
+
{
|
|
172
|
+
return this.monthsLater(months * -1, date);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static daysAgo(days: number, date: Date): Date
|
|
176
|
+
{
|
|
177
|
+
return this.daysLater(days * -1, date);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static hoursAgo(hours: number, date: Date): Date
|
|
181
|
+
{
|
|
182
|
+
return this.hoursLater(hours * -1, date);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
static minutesAgo(minutes: number, date: Date): Date
|
|
186
|
+
{
|
|
187
|
+
return this.minutesLater(minutes * -1, date);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
static secondsAgo(seconds: number, date: Date): Date
|
|
191
|
+
{
|
|
192
|
+
return this.secondsLater(seconds * -1, date);
|
|
193
|
+
}
|
|
194
|
+
// later
|
|
195
|
+
static later(num: number, type: moment.DurationInputArg2, date: Date | null = null): Date
|
|
196
|
+
{
|
|
197
|
+
if (!date)
|
|
198
|
+
date = new Date();
|
|
199
|
+
else
|
|
200
|
+
date = new Date(date);
|
|
201
|
+
return moment(date).add(num, type).toDate();
|
|
202
|
+
}
|
|
203
|
+
static monthsLater(months: number, date: Date): Date
|
|
204
|
+
{
|
|
205
|
+
return this.later(months, 'months', date);
|
|
206
|
+
}
|
|
207
|
+
static daysLater(days: number, date: Date): Date
|
|
208
|
+
{
|
|
209
|
+
return this.later(days, 'days', date);
|
|
210
|
+
}
|
|
211
|
+
static hoursLater(hours: number, date: Date): Date
|
|
212
|
+
{
|
|
213
|
+
return this.later(hours, 'hours', date);
|
|
214
|
+
}
|
|
215
|
+
static minutesLater(minutes: number, date: Date): Date
|
|
216
|
+
{
|
|
217
|
+
return this.later(minutes, 'minutes', date);
|
|
218
|
+
}
|
|
219
|
+
static secondsLater(seconds: number, date: Date): Date
|
|
220
|
+
{
|
|
221
|
+
return this.later(seconds, 'seconds', date);
|
|
222
|
+
}
|
|
223
|
+
static getDuration(startDate: Date, endDate: Date, zeroAllOnPassed: boolean = false)
|
|
224
|
+
{
|
|
225
|
+
if (!endDate)
|
|
226
|
+
endDate = new Date();
|
|
227
|
+
let s = moment(startDate);
|
|
228
|
+
let e = moment(endDate);
|
|
229
|
+
let passed = e < s;
|
|
230
|
+
let year = e.diff(s, 'years', false);
|
|
231
|
+
s.add(year, 'years');
|
|
232
|
+
let month = e.diff(s, 'months', false);
|
|
233
|
+
s.add(month, 'months');
|
|
234
|
+
let day = e.diff(s, 'days', false);
|
|
235
|
+
s.add(day, 'days');
|
|
236
|
+
let hour = e.diff(s, 'hours', false);
|
|
237
|
+
s.add(hour, 'hours');
|
|
238
|
+
let minute = e.diff(s, 'minutes', false);
|
|
239
|
+
s.add(minute, 'minutes');
|
|
240
|
+
let second = e.diff(s, 'seconds', false);
|
|
241
|
+
s.add(second, 'seconds');
|
|
242
|
+
|
|
243
|
+
if (passed)
|
|
244
|
+
if (zeroAllOnPassed)
|
|
245
|
+
year = month = day = hour = minute = second = 0;
|
|
246
|
+
let total = year * 12 + month;
|
|
247
|
+
total = total * 30 + day;
|
|
248
|
+
total = total * 24 + hour;
|
|
249
|
+
total = total * 60 + minute;
|
|
250
|
+
total += second;
|
|
251
|
+
|
|
252
|
+
return {
|
|
253
|
+
passed, year, month, day, hour, minute, second, total
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
static calculateAge(date: Date): number
|
|
257
|
+
{
|
|
258
|
+
let birthday = new Date(date);
|
|
259
|
+
let ageDifMs = Date.now() - birthday.getTime();
|
|
260
|
+
var ageDate = new Date(ageDifMs); // miliseconds from epoch
|
|
261
|
+
return Math.abs(ageDate.getUTCFullYear() - 1970);
|
|
262
|
+
}
|
|
263
263
|
}
|
package/src/URLOperation.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { ParsedNameValue } from "./ParsedNameValue";
|
|
2
|
-
|
|
3
|
-
export abstract class URLOperation
|
|
4
|
-
{
|
|
5
|
-
static getQuery(query?: { [name: string]: ParsedNameValue }): string
|
|
6
|
-
{
|
|
7
|
-
let ans = "";
|
|
8
|
-
let first = true;
|
|
9
|
-
if (query)
|
|
10
|
-
for (const key of Object.keys(query))
|
|
11
|
-
{
|
|
12
|
-
let value = query[key];
|
|
13
|
-
if (value != null)
|
|
14
|
-
{
|
|
15
|
-
if (first)
|
|
16
|
-
ans += "?";
|
|
17
|
-
else
|
|
18
|
-
ans += "&";
|
|
19
|
-
if (Array.isArray(value))
|
|
20
|
-
value = value.join(",");
|
|
21
|
-
ans += `${key}=${value}`;
|
|
22
|
-
first = false;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
return ans;
|
|
26
|
-
}
|
|
27
|
-
static merge(...urlParts: string[]): string
|
|
28
|
-
{
|
|
29
|
-
let ans = "";
|
|
30
|
-
for (let part of urlParts)
|
|
31
|
-
{
|
|
32
|
-
if (part)
|
|
33
|
-
{
|
|
34
|
-
if (ans)
|
|
35
|
-
{
|
|
36
|
-
if (part.startsWith('/'))
|
|
37
|
-
part = part.substring(1);
|
|
38
|
-
ans += "/";
|
|
39
|
-
}
|
|
40
|
-
ans += part;
|
|
41
|
-
if (ans.endsWith('/'))
|
|
42
|
-
ans = ans.substring(0, ans.length - 1);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return ans;
|
|
46
|
-
}
|
|
47
|
-
static getSub(sub: string, query?: { [name: string]: ParsedNameValue }): string
|
|
48
|
-
{
|
|
49
|
-
return this.merge(sub, this.getQuery(query));
|
|
50
|
-
}
|
|
51
|
-
static getLink(domain: string, sub: string, query?: { [name: string]: ParsedNameValue }): string
|
|
52
|
-
{
|
|
53
|
-
return this.merge(domain, this.getSub(sub, query));
|
|
54
|
-
}
|
|
1
|
+
import { ParsedNameValue } from "./ParsedNameValue";
|
|
2
|
+
|
|
3
|
+
export abstract class URLOperation
|
|
4
|
+
{
|
|
5
|
+
static getQuery(query?: { [name: string]: ParsedNameValue }): string
|
|
6
|
+
{
|
|
7
|
+
let ans = "";
|
|
8
|
+
let first = true;
|
|
9
|
+
if (query)
|
|
10
|
+
for (const key of Object.keys(query))
|
|
11
|
+
{
|
|
12
|
+
let value = query[key];
|
|
13
|
+
if (value != null)
|
|
14
|
+
{
|
|
15
|
+
if (first)
|
|
16
|
+
ans += "?";
|
|
17
|
+
else
|
|
18
|
+
ans += "&";
|
|
19
|
+
if (Array.isArray(value))
|
|
20
|
+
value = value.join(",");
|
|
21
|
+
ans += `${key}=${value}`;
|
|
22
|
+
first = false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return ans;
|
|
26
|
+
}
|
|
27
|
+
static merge(...urlParts: string[]): string
|
|
28
|
+
{
|
|
29
|
+
let ans = "";
|
|
30
|
+
for (let part of urlParts)
|
|
31
|
+
{
|
|
32
|
+
if (part)
|
|
33
|
+
{
|
|
34
|
+
if (ans)
|
|
35
|
+
{
|
|
36
|
+
if (part.startsWith('/'))
|
|
37
|
+
part = part.substring(1);
|
|
38
|
+
ans += "/";
|
|
39
|
+
}
|
|
40
|
+
ans += part;
|
|
41
|
+
if (ans.endsWith('/'))
|
|
42
|
+
ans = ans.substring(0, ans.length - 1);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return ans;
|
|
46
|
+
}
|
|
47
|
+
static getSub(sub: string, query?: { [name: string]: ParsedNameValue }): string
|
|
48
|
+
{
|
|
49
|
+
return this.merge(sub, this.getQuery(query));
|
|
50
|
+
}
|
|
51
|
+
static getLink(domain: string, sub: string, query?: { [name: string]: ParsedNameValue }): string
|
|
52
|
+
{
|
|
53
|
+
return this.merge(domain, this.getSub(sub, query));
|
|
54
|
+
}
|
|
55
55
|
}
|