@etsoo/shared 1.2.51 → 1.2.54

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 (94) hide show
  1. package/.github/workflows/main.yml +6 -5
  2. package/README.md +1 -1
  3. package/lib/cjs/ActionResult.d.ts +1 -1
  4. package/lib/cjs/ActionResult.js +3 -3
  5. package/lib/cjs/ArrayUtils.d.ts +1 -1
  6. package/lib/cjs/ArrayUtils.js +4 -4
  7. package/lib/cjs/ColorUtils.d.ts +1 -1
  8. package/lib/cjs/ColorUtils.js +2 -2
  9. package/lib/cjs/DataTypes.d.ts +6 -6
  10. package/lib/cjs/DataTypes.js +50 -51
  11. package/lib/cjs/DateUtils.d.ts +1 -1
  12. package/lib/cjs/DateUtils.js +27 -28
  13. package/lib/cjs/DomUtils.d.ts +3 -3
  14. package/lib/cjs/DomUtils.js +64 -73
  15. package/lib/cjs/ExtendUtils.d.ts +1 -1
  16. package/lib/cjs/ExtendUtils.js +6 -6
  17. package/lib/cjs/IActionResult.d.ts +15 -2
  18. package/lib/cjs/NumberUtils.d.ts +1 -1
  19. package/lib/cjs/NumberUtils.js +9 -9
  20. package/lib/cjs/StorageUtils.js +2 -2
  21. package/lib/cjs/Utils.d.ts +4 -4
  22. package/lib/cjs/Utils.js +58 -62
  23. package/lib/cjs/index.d.ts +22 -22
  24. package/lib/cjs/storage/WindowStorage.d.ts +1 -1
  25. package/lib/cjs/types/ContentDisposition.d.ts +2 -2
  26. package/lib/cjs/types/ContentDisposition.js +11 -13
  27. package/lib/cjs/types/EColor.js +5 -7
  28. package/lib/cjs/types/EHistory.d.ts +3 -3
  29. package/lib/cjs/types/EHistory.js +4 -4
  30. package/lib/cjs/types/ErrorData.d.ts +1 -1
  31. package/lib/cjs/types/EventClass.js +1 -1
  32. package/lib/mjs/ActionResult.d.ts +1 -1
  33. package/lib/mjs/ActionResult.js +3 -3
  34. package/lib/mjs/ArrayUtils.d.ts +1 -1
  35. package/lib/mjs/ArrayUtils.js +5 -5
  36. package/lib/mjs/ColorUtils.d.ts +1 -1
  37. package/lib/mjs/ColorUtils.js +3 -3
  38. package/lib/mjs/DataTypes.d.ts +6 -6
  39. package/lib/mjs/DataTypes.js +50 -51
  40. package/lib/mjs/DateUtils.d.ts +1 -1
  41. package/lib/mjs/DateUtils.js +27 -28
  42. package/lib/mjs/DomUtils.d.ts +3 -3
  43. package/lib/mjs/DomUtils.js +67 -76
  44. package/lib/mjs/ExtendUtils.d.ts +1 -1
  45. package/lib/mjs/ExtendUtils.js +6 -6
  46. package/lib/mjs/IActionResult.d.ts +15 -2
  47. package/lib/mjs/NumberUtils.d.ts +1 -1
  48. package/lib/mjs/NumberUtils.js +9 -9
  49. package/lib/mjs/StorageUtils.js +4 -4
  50. package/lib/mjs/Utils.d.ts +4 -4
  51. package/lib/mjs/Utils.js +61 -65
  52. package/lib/mjs/index.d.ts +22 -22
  53. package/lib/mjs/index.js +22 -22
  54. package/lib/mjs/storage/WindowStorage.d.ts +1 -1
  55. package/lib/mjs/storage/WindowStorage.js +2 -2
  56. package/lib/mjs/types/ContentDisposition.d.ts +2 -2
  57. package/lib/mjs/types/ContentDisposition.js +12 -14
  58. package/lib/mjs/types/EColor.js +5 -7
  59. package/lib/mjs/types/EHistory.d.ts +3 -3
  60. package/lib/mjs/types/EHistory.js +5 -5
  61. package/lib/mjs/types/ErrorData.d.ts +1 -1
  62. package/lib/mjs/types/EventClass.js +1 -1
  63. package/package.json +61 -63
  64. package/src/ActionResult.ts +23 -23
  65. package/src/ArrayUtils.ts +164 -172
  66. package/src/ColorUtils.ts +80 -82
  67. package/src/DataTypes.ts +745 -754
  68. package/src/DateUtils.ts +266 -268
  69. package/src/DomUtils.ts +806 -831
  70. package/src/ExtendUtils.ts +191 -191
  71. package/src/IActionResult.ts +55 -40
  72. package/src/Keyboard.ts +258 -258
  73. package/src/NumberUtils.ts +135 -135
  74. package/src/StorageUtils.ts +117 -117
  75. package/src/Utils.ts +908 -930
  76. package/src/index.ts +22 -22
  77. package/src/node/Storage.ts +53 -53
  78. package/src/storage/IStorage.ts +62 -62
  79. package/src/storage/WindowStorage.ts +140 -140
  80. package/src/types/ContentDisposition.ts +59 -63
  81. package/src/types/DataError.ts +15 -15
  82. package/src/types/DelayedExecutorType.ts +15 -15
  83. package/src/types/EColor.ts +241 -248
  84. package/src/types/EHistory.ts +151 -151
  85. package/src/types/ErrorData.ts +11 -11
  86. package/src/types/EventClass.ts +220 -220
  87. package/src/types/FormData.ts +25 -25
  88. package/src/types/ParsedPath.ts +5 -5
  89. package/tsconfig.cjs.json +16 -16
  90. package/tsconfig.json +16 -16
  91. package/.eslintignore +0 -3
  92. package/.eslintrc.json +0 -29
  93. package/.prettierignore +0 -5
  94. package/.prettierrc +0 -6
package/src/DateUtils.ts CHANGED
@@ -1,66 +1,66 @@
1
1
  declare global {
2
- interface Date {
3
- /**
4
- * Substract date
5
- * @param input Input date
6
- */
7
- substract(input: Date): TimeSpan;
8
- }
2
+ interface Date {
3
+ /**
4
+ * Substract date
5
+ * @param input Input date
6
+ */
7
+ substract(input: Date): TimeSpan;
8
+ }
9
9
  }
10
10
 
11
11
  Date.prototype.substract = function (this: Date, input: Date): TimeSpan {
12
- // Calculate from miniseconds
13
- const totalMiniseconds = this.valueOf() - input.valueOf();
14
- const totalSeconds = totalMiniseconds / 1000.0;
15
- const totalMinutes = totalSeconds / 60.0;
16
- const totalHours = totalMinutes / 60.0;
17
- const totalDays = totalHours / 24.0;
18
-
19
- // Calcuate days
20
- const thisYear = this.getFullYear();
21
- const thisMonth = this.getMonth();
22
- const inputCopy = new Date(input);
23
- inputCopy.setFullYear(thisYear);
24
- inputCopy.setMonth(thisMonth);
25
-
26
- const diffDays =
27
- (new Date(this).valueOf() - inputCopy.valueOf()) / 86400000.0;
28
- const diffMonths =
29
- diffDays > 0
30
- ? diffDays / DateUtils.getDays(thisYear, thisMonth)
31
- : diffDays / DateUtils.getDays(thisYear, thisMonth - 1);
32
-
33
- // Total months
34
- const totalMonths =
35
- 12 * (thisYear - input.getFullYear()) +
36
- (thisMonth - input.getMonth()) +
37
- diffMonths;
38
-
39
- // Total years
40
- const totalYears = totalMonths / 12.0;
41
-
42
- return {
43
- totalMiniseconds,
44
- totalSeconds,
45
- totalMinutes,
46
- totalHours,
47
- totalDays,
48
- totalMonths,
49
- totalYears
50
- };
12
+ // Calculate from miniseconds
13
+ const totalMiniseconds = this.valueOf() - input.valueOf();
14
+ const totalSeconds = totalMiniseconds / 1000.0;
15
+ const totalMinutes = totalSeconds / 60.0;
16
+ const totalHours = totalMinutes / 60.0;
17
+ const totalDays = totalHours / 24.0;
18
+
19
+ // Calcuate days
20
+ const thisYear = this.getFullYear();
21
+ const thisMonth = this.getMonth();
22
+ const inputCopy = new Date(input);
23
+ inputCopy.setFullYear(thisYear);
24
+ inputCopy.setMonth(thisMonth);
25
+
26
+ const diffDays =
27
+ (new Date(this).valueOf() - inputCopy.valueOf()) / 86400000.0;
28
+ const diffMonths =
29
+ diffDays > 0
30
+ ? diffDays / DateUtils.getDays(thisYear, thisMonth)
31
+ : diffDays / DateUtils.getDays(thisYear, thisMonth - 1);
32
+
33
+ // Total months
34
+ const totalMonths =
35
+ 12 * (thisYear - input.getFullYear()) +
36
+ (thisMonth - input.getMonth()) +
37
+ diffMonths;
38
+
39
+ // Total years
40
+ const totalYears = totalMonths / 12.0;
41
+
42
+ return {
43
+ totalMiniseconds,
44
+ totalSeconds,
45
+ totalMinutes,
46
+ totalHours,
47
+ totalDays,
48
+ totalMonths,
49
+ totalYears
50
+ };
51
51
  };
52
52
 
53
53
  /**
54
54
  * TimeSpan
55
55
  */
56
56
  export interface TimeSpan {
57
- totalMiniseconds: number;
58
- totalSeconds: number;
59
- totalMinutes: number;
60
- totalHours: number;
61
- totalDays: number;
62
- totalMonths: number;
63
- totalYears: number;
57
+ totalMiniseconds: number;
58
+ totalSeconds: number;
59
+ totalMinutes: number;
60
+ totalHours: number;
61
+ totalDays: number;
62
+ totalMonths: number;
63
+ totalYears: number;
64
64
  }
65
65
 
66
66
  type DateType = Date | string | null | undefined;
@@ -68,223 +68,221 @@ type DateType = Date | string | null | undefined;
68
68
  type DateReturn<T extends DateType, R> = T extends Date ? R : R | undefined;
69
69
 
70
70
  export namespace DateUtils {
71
- /**
72
- * Day format, YYYY-MM-DD
73
- */
74
- export const DayFormat: Intl.DateTimeFormatOptions = {
75
- year: 'numeric',
76
- month: '2-digit',
77
- day: '2-digit'
78
- };
79
-
80
- /**
81
- * Minute format, YYYY-MM-DD hh:mm
82
- */
83
- export const MinuteFormat: Intl.DateTimeFormatOptions = {
84
- ...DayFormat,
85
- hour: '2-digit',
86
- hourCycle: 'h23',
87
- minute: '2-digit'
88
- };
89
-
90
- /**
91
- * Second format, YYYY-MM-DD hh:mm:ss
92
- */
93
- export const SecondFormat: Intl.DateTimeFormatOptions = {
94
- ...MinuteFormat,
95
- second: '2-digit'
96
- };
97
-
98
- /**
99
- * Date format options
100
- */
101
- export type FormatOptions = Intl.DateTimeFormatOptions | 'd' | 'dm' | 'ds';
102
-
103
- /**
104
- * Format
105
- * @param input Input date time
106
- * @param locale Locale
107
- * @param options Options
108
- * @param timeZone Time zone
109
- */
110
- export function format<T extends DateType>(
111
- input: T,
112
- locale?: string | string[],
113
- options?: FormatOptions,
114
- timeZone?: string
115
- ): DateReturn<T, string> {
116
- // Parse
117
- const parsed = parse(input);
118
-
119
- // Null case
120
- if (parsed == null) return <DateReturn<T, string>>undefined;
121
-
122
- // Default options
123
- options ??= DayFormat;
124
-
125
- // Default options
126
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
127
- let opt: Intl.DateTimeFormatOptions;
128
- switch (options) {
129
- case 'd':
130
- opt = DayFormat;
131
- break;
132
- case 'dm':
133
- opt = MinuteFormat;
134
- break;
135
- case 'ds':
136
- opt = SecondFormat;
137
- break;
138
- default:
139
- opt = options;
140
- }
141
-
142
- // Clone as new options with time zone
143
- const newOpt = { ...opt, ...(timeZone != null && { timeZone }) };
144
-
145
- // Return format result
146
- return new Intl.DateTimeFormat(locale, newOpt)
147
- .format(parsed)
148
- .replace(/,\s*/g, ' ');
71
+ /**
72
+ * Day format, YYYY-MM-DD
73
+ */
74
+ export const DayFormat: Intl.DateTimeFormatOptions = {
75
+ year: "numeric",
76
+ month: "2-digit",
77
+ day: "2-digit"
78
+ };
79
+
80
+ /**
81
+ * Minute format, YYYY-MM-DD hh:mm
82
+ */
83
+ export const MinuteFormat: Intl.DateTimeFormatOptions = {
84
+ ...DayFormat,
85
+ hour: "2-digit",
86
+ hourCycle: "h23",
87
+ minute: "2-digit"
88
+ };
89
+
90
+ /**
91
+ * Second format, YYYY-MM-DD hh:mm:ss
92
+ */
93
+ export const SecondFormat: Intl.DateTimeFormatOptions = {
94
+ ...MinuteFormat,
95
+ second: "2-digit"
96
+ };
97
+
98
+ /**
99
+ * Date format options
100
+ */
101
+ export type FormatOptions = Intl.DateTimeFormatOptions | "d" | "dm" | "ds";
102
+
103
+ /**
104
+ * Format
105
+ * @param input Input date time
106
+ * @param locale Locale
107
+ * @param options Options
108
+ * @param timeZone Time zone
109
+ */
110
+ export function format<T extends DateType>(
111
+ input: T,
112
+ locale?: string | string[],
113
+ options?: FormatOptions,
114
+ timeZone?: string
115
+ ): DateReturn<T, string> {
116
+ // Parse
117
+ const parsed = parse(input);
118
+
119
+ // Null case
120
+ if (parsed == null) return <DateReturn<T, string>>undefined;
121
+
122
+ // Default options
123
+ options ??= DayFormat;
124
+
125
+ // Default options
126
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
127
+ let opt: Intl.DateTimeFormatOptions;
128
+ switch (options) {
129
+ case "d":
130
+ opt = DayFormat;
131
+ break;
132
+ case "dm":
133
+ opt = MinuteFormat;
134
+ break;
135
+ case "ds":
136
+ opt = SecondFormat;
137
+ break;
138
+ default:
139
+ opt = options;
149
140
  }
150
141
 
151
- /**
152
- * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
153
- * @param date Input date
154
- * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
155
- */
156
- export function formatForInput<T extends DateType>(
157
- date: T,
158
- hasSecondOrType?: boolean | string
159
- ): DateReturn<T, string> {
160
- // Return when null
161
- if (date == null || date === '')
162
- return <DateReturn<T, string>>undefined;
163
-
164
- // Parse string as date
165
- const dt: Date = typeof date === 'string' ? new Date(date) : date;
166
-
167
- const hasSecond =
168
- typeof hasSecondOrType === 'string'
169
- ? hasSecondOrType === 'date'
170
- ? undefined
171
- : true
172
- : hasSecondOrType;
173
-
174
- // Parts
175
- const parts = [
176
- dt.getFullYear(),
177
- (dt.getMonth() + 1).toString().padStart(2, '0'),
178
- dt.getDate().toString().padStart(2, '0')
179
- ];
180
-
181
- // Date
182
- const d = parts.join('-');
183
- if (hasSecond == null) return d;
184
-
185
- const hm = [
186
- dt.getHours().toString().padStart(2, '0'),
187
- dt.getMinutes().toString().padStart(2, '0')
188
- ];
189
- if (hasSecond) hm.push(dt.getSeconds().toString().padStart(2, '0'));
190
- return `${d}T${hm.join(':')}`;
191
- }
192
-
193
- /**
194
- * Get month's days
195
- * @param year Year
196
- * @param month Month, starts from 0
197
- * @returns Days
198
- */
199
- export const getDays = (year: number, month: number) =>
200
- new Date(year, month + 1, 0).getDate();
201
-
202
- /**
203
- * Is the test date expired to now
204
- * @param testDate Test date
205
- * @returns Result
206
- */
207
- export function isExpired(testDate?: Date | string | null) {
208
- if (testDate == null) return false;
209
- const d = parse(testDate);
210
-
211
- // Format error
212
- if (d == null) return false;
213
-
214
- return d.valueOf() < new Date().valueOf();
215
- }
216
-
217
- /**
218
- * Build JSON parser
219
- * @param keys Date field names
220
- * @returns JSON parser
221
- */
222
- export const jsonParser = (keys: string[]) => {
223
- return function (key: string, value: unknown) {
224
- if (
225
- typeof value === 'string' &&
226
- value != null &&
227
- value !== '' &&
228
- keys.includes(key)
229
- ) {
230
- const parsedDate = parse(value);
231
- if (parsedDate != null) return parsedDate;
232
- }
233
- return value;
234
- };
142
+ // Clone as new options with time zone
143
+ const newOpt = { ...opt, ...(timeZone != null && { timeZone }) };
144
+
145
+ // Return format result
146
+ return new Intl.DateTimeFormat(locale, newOpt)
147
+ .format(parsed)
148
+ .replace(/,\s*/g, " ");
149
+ }
150
+
151
+ /**
152
+ * Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
153
+ * @param date Input date
154
+ * @param hasSecondOrType 'undefined' for date only, 'false' for hour:minute only, 'true' for all, or input field type
155
+ */
156
+ export function formatForInput<T extends DateType>(
157
+ date: T,
158
+ hasSecondOrType?: boolean | string
159
+ ): DateReturn<T, string> {
160
+ // Return when null
161
+ if (date == null || date === "") return <DateReturn<T, string>>undefined;
162
+
163
+ // Parse string as date
164
+ const dt: Date = typeof date === "string" ? new Date(date) : date;
165
+
166
+ const hasSecond =
167
+ typeof hasSecondOrType === "string"
168
+ ? hasSecondOrType === "date"
169
+ ? undefined
170
+ : true
171
+ : hasSecondOrType;
172
+
173
+ // Parts
174
+ const parts = [
175
+ dt.getFullYear(),
176
+ (dt.getMonth() + 1).toString().padStart(2, "0"),
177
+ dt.getDate().toString().padStart(2, "0")
178
+ ];
179
+
180
+ // Date
181
+ const d = parts.join("-");
182
+ if (hasSecond == null) return d;
183
+
184
+ const hm = [
185
+ dt.getHours().toString().padStart(2, "0"),
186
+ dt.getMinutes().toString().padStart(2, "0")
187
+ ];
188
+ if (hasSecond) hm.push(dt.getSeconds().toString().padStart(2, "0"));
189
+ return `${d}T${hm.join(":")}`;
190
+ }
191
+
192
+ /**
193
+ * Get month's days
194
+ * @param year Year
195
+ * @param month Month, starts from 0
196
+ * @returns Days
197
+ */
198
+ export const getDays = (year: number, month: number) =>
199
+ new Date(year, month + 1, 0).getDate();
200
+
201
+ /**
202
+ * Is the test date expired to now
203
+ * @param testDate Test date
204
+ * @returns Result
205
+ */
206
+ export function isExpired(testDate?: Date | string | null) {
207
+ if (testDate == null) return false;
208
+ const d = parse(testDate);
209
+
210
+ // Format error
211
+ if (d == null) return false;
212
+
213
+ return d.valueOf() < new Date().valueOf();
214
+ }
215
+
216
+ /**
217
+ * Build JSON parser
218
+ * @param keys Date field names
219
+ * @returns JSON parser
220
+ */
221
+ export const jsonParser = (keys: string[]) => {
222
+ return function (key: string, value: unknown) {
223
+ if (
224
+ typeof value === "string" &&
225
+ value != null &&
226
+ value !== "" &&
227
+ keys.includes(key)
228
+ ) {
229
+ const parsedDate = parse(value);
230
+ if (parsedDate != null) return parsedDate;
231
+ }
232
+ return value;
235
233
  };
236
-
237
- /**
238
- * Parse string to date
239
- * 2021/10/31 or 2021-10-31
240
- * @param input Input string
241
- * @returns Date
242
- */
243
- export function parse<T extends DateType>(input: T): DateReturn<T, Date> {
244
- if (input == null) return <DateReturn<T, Date>>undefined;
245
- if (typeof input === 'string') {
246
- const f = input[0];
247
- if (f >= '0' && f <= '9' && /[-\/\s]/g.test(input)) {
248
- const n = Date.parse(input);
249
- if (!isNaN(n)) return new Date(n);
250
- }
251
- return <DateReturn<T, Date>>undefined;
252
- }
253
- return <Date>input;
254
- }
255
-
256
- /**
257
- * Two dates are in the same day
258
- * @param d1 First date
259
- * @param d2 Second date
260
- * @returns Result
261
- */
262
- export function sameDay(
263
- d1?: Date | string | null,
264
- d2?: Date | string | null
265
- ) {
266
- d1 = parse(d1);
267
- d2 = parse(d2);
268
- if (d1 == null || d2 == null) return false;
269
- return d1.toDateString() === d2.toDateString();
270
- }
271
-
272
- /**
273
- * Two dates are in the same month
274
- * @param d1 First date
275
- * @param d2 Second date
276
- * @returns Result
277
- */
278
- export function sameMonth(
279
- d1?: Date | string | null,
280
- d2?: Date | string | null
281
- ) {
282
- d1 = parse(d1);
283
- d2 = parse(d2);
284
- if (d1 == null || d2 == null) return false;
285
- return (
286
- d1.getFullYear() === d2.getFullYear() &&
287
- d1.getMonth() === d2.getMonth()
288
- );
234
+ };
235
+
236
+ /**
237
+ * Parse string to date
238
+ * 2021/10/31 or 2021-10-31
239
+ * @param input Input string
240
+ * @returns Date
241
+ */
242
+ export function parse<T extends DateType>(input: T): DateReturn<T, Date> {
243
+ if (input == null) return <DateReturn<T, Date>>undefined;
244
+ if (typeof input === "string") {
245
+ const f = input[0];
246
+ if (f >= "0" && f <= "9" && /[-\/\s]/g.test(input)) {
247
+ const n = Date.parse(input);
248
+ if (!isNaN(n)) return new Date(n);
249
+ }
250
+ return <DateReturn<T, Date>>undefined;
289
251
  }
252
+ return <Date>input;
253
+ }
254
+
255
+ /**
256
+ * Two dates are in the same day
257
+ * @param d1 First date
258
+ * @param d2 Second date
259
+ * @returns Result
260
+ */
261
+ export function sameDay(
262
+ d1?: Date | string | null,
263
+ d2?: Date | string | null
264
+ ) {
265
+ d1 = parse(d1);
266
+ d2 = parse(d2);
267
+ if (d1 == null || d2 == null) return false;
268
+ return d1.toDateString() === d2.toDateString();
269
+ }
270
+
271
+ /**
272
+ * Two dates are in the same month
273
+ * @param d1 First date
274
+ * @param d2 Second date
275
+ * @returns Result
276
+ */
277
+ export function sameMonth(
278
+ d1?: Date | string | null,
279
+ d2?: Date | string | null
280
+ ) {
281
+ d1 = parse(d1);
282
+ d2 = parse(d2);
283
+ if (d1 == null || d2 == null) return false;
284
+ return (
285
+ d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth()
286
+ );
287
+ }
290
288
  }