@etsoo/shared 1.1.51 → 1.1.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.
- package/README.md +3 -0
- package/__tests__/DateUtils.ts +26 -1
- package/lib/cjs/DateUtils.d.ts +17 -2
- package/lib/cjs/DateUtils.js +43 -4
- package/lib/mjs/DateUtils.d.ts +17 -2
- package/lib/mjs/DateUtils.js +43 -4
- package/package.json +9 -10
- package/src/DateUtils.ts +51 -4
package/README.md
CHANGED
|
@@ -142,8 +142,11 @@ Dates related utilities
|
|
|
142
142
|
|Methods||
|
|
143
143
|
|getDays|Get month's days|
|
|
144
144
|
|forma|Format dates|
|
|
145
|
+
|formatForInput|Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property|
|
|
145
146
|
|jsonParser|JSON parser|
|
|
146
147
|
|parse|Parse string to date|
|
|
148
|
+
|sameDay|Two dates are in the same day|
|
|
149
|
+
|sameMonth|Two dates are in the same month|
|
|
147
150
|
|substract|Date extended method, substract a date|
|
|
148
151
|
|
|
149
152
|
## DomUtils
|
package/__tests__/DateUtils.ts
CHANGED
|
@@ -56,8 +56,15 @@ test('Tests for formatForInput', () => {
|
|
|
56
56
|
const result1 = DateUtils.formatForInput('2021/7/17');
|
|
57
57
|
expect(result1).toBe('2021-07-17');
|
|
58
58
|
|
|
59
|
-
const
|
|
59
|
+
const d = new Date(2021, 5, 6, 20, 8, 45);
|
|
60
|
+
const result2 = DateUtils.formatForInput(d);
|
|
60
61
|
expect(result2).toBe('2021-06-06');
|
|
62
|
+
|
|
63
|
+
const result3 = DateUtils.formatForInput(d, false);
|
|
64
|
+
expect(result3).toBe('2021-06-06T20:08');
|
|
65
|
+
|
|
66
|
+
const result4 = DateUtils.formatForInput(d, true);
|
|
67
|
+
expect(result4).toBe('2021-06-06T20:08:45');
|
|
61
68
|
});
|
|
62
69
|
|
|
63
70
|
test('Tests for substract', () => {
|
|
@@ -69,3 +76,21 @@ test('Tests for substract', () => {
|
|
|
69
76
|
expect(d4.substract(d1).totalYears < 2).toBeTruthy();
|
|
70
77
|
expect(d2.substract(d1).totalMinutes > 10).toBeTruthy();
|
|
71
78
|
});
|
|
79
|
+
|
|
80
|
+
test('Tests for sameDay', () => {
|
|
81
|
+
expect(
|
|
82
|
+
DateUtils.sameDay('2022/9/11 22:03', new Date(2022, 8, 11, 10, 3))
|
|
83
|
+
).toBeTruthy();
|
|
84
|
+
expect(
|
|
85
|
+
DateUtils.sameDay('2022/9/11 22:03', new Date(2022, 9, 11, 10, 3))
|
|
86
|
+
).toBeFalsy();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('Tests for sameMonth', () => {
|
|
90
|
+
expect(
|
|
91
|
+
DateUtils.sameMonth('2022-09-11 22:03', new Date(2022, 8, 10, 10, 3))
|
|
92
|
+
).toBeTruthy();
|
|
93
|
+
expect(
|
|
94
|
+
DateUtils.sameMonth('2022/9/11 22:03', '2022/8/31 23:59:59')
|
|
95
|
+
).toBeFalsy();
|
|
96
|
+
});
|
package/lib/cjs/DateUtils.d.ts
CHANGED
|
@@ -45,10 +45,11 @@ export declare namespace DateUtils {
|
|
|
45
45
|
*/
|
|
46
46
|
function format(input?: Date | string, locale?: string | string[], options?: FormatOptions, timeZone?: string): string | undefined;
|
|
47
47
|
/**
|
|
48
|
-
* Format to 'yyyy-MM-dd', especially used for date input min/max property
|
|
48
|
+
* Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
|
|
49
49
|
* @param date Input date
|
|
50
|
+
* @param hasSecond 'undefined' for date only, 'false' for hour:minute only
|
|
50
51
|
*/
|
|
51
|
-
function formatForInput(date?: Date | string | null): string;
|
|
52
|
+
function formatForInput(date?: Date | string | null, hasSecond?: boolean): string;
|
|
52
53
|
/**
|
|
53
54
|
* Get month's days
|
|
54
55
|
* @param year Year
|
|
@@ -69,4 +70,18 @@ export declare namespace DateUtils {
|
|
|
69
70
|
* @returns Date
|
|
70
71
|
*/
|
|
71
72
|
function parse(input?: Date | string | null): Date | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Two dates are in the same day
|
|
75
|
+
* @param d1 First date
|
|
76
|
+
* @param d2 Second date
|
|
77
|
+
* @returns Result
|
|
78
|
+
*/
|
|
79
|
+
function sameDay(d1?: Date | string | null, d2?: Date | string | null): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Two dates are in the same month
|
|
82
|
+
* @param d1 First date
|
|
83
|
+
* @param d2 Second date
|
|
84
|
+
* @returns Result
|
|
85
|
+
*/
|
|
86
|
+
function sameMonth(d1?: Date | string | null, d2?: Date | string | null): boolean;
|
|
72
87
|
}
|
package/lib/cjs/DateUtils.js
CHANGED
|
@@ -92,10 +92,11 @@ var DateUtils;
|
|
|
92
92
|
}
|
|
93
93
|
DateUtils.format = format;
|
|
94
94
|
/**
|
|
95
|
-
* Format to 'yyyy-MM-dd', especially used for date input min/max property
|
|
95
|
+
* Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
|
|
96
96
|
* @param date Input date
|
|
97
|
+
* @param hasSecond 'undefined' for date only, 'false' for hour:minute only
|
|
97
98
|
*/
|
|
98
|
-
function formatForInput(date) {
|
|
99
|
+
function formatForInput(date, hasSecond) {
|
|
99
100
|
// Parse string as date
|
|
100
101
|
if (typeof date === 'string')
|
|
101
102
|
date = new Date(date);
|
|
@@ -107,8 +108,17 @@ var DateUtils;
|
|
|
107
108
|
(date.getMonth() + 1).toString().padStart(2, '0'),
|
|
108
109
|
date.getDate().toString().padStart(2, '0')
|
|
109
110
|
];
|
|
110
|
-
//
|
|
111
|
-
|
|
111
|
+
// Date
|
|
112
|
+
const d = parts.join('-');
|
|
113
|
+
if (hasSecond == null)
|
|
114
|
+
return d;
|
|
115
|
+
const hm = [
|
|
116
|
+
date.getHours().toString().padStart(2, '0'),
|
|
117
|
+
date.getMinutes().toString().padStart(2, '0')
|
|
118
|
+
];
|
|
119
|
+
if (hasSecond)
|
|
120
|
+
hm.push(date.getSeconds().toString().padStart(2, '0'));
|
|
121
|
+
return `${d}T${hm.join(':')}`;
|
|
112
122
|
}
|
|
113
123
|
DateUtils.formatForInput = formatForInput;
|
|
114
124
|
/**
|
|
@@ -157,4 +167,33 @@ var DateUtils;
|
|
|
157
167
|
return input;
|
|
158
168
|
}
|
|
159
169
|
DateUtils.parse = parse;
|
|
170
|
+
/**
|
|
171
|
+
* Two dates are in the same day
|
|
172
|
+
* @param d1 First date
|
|
173
|
+
* @param d2 Second date
|
|
174
|
+
* @returns Result
|
|
175
|
+
*/
|
|
176
|
+
function sameDay(d1, d2) {
|
|
177
|
+
d1 = parse(d1);
|
|
178
|
+
d2 = parse(d2);
|
|
179
|
+
if (d1 == null || d2 == null)
|
|
180
|
+
return false;
|
|
181
|
+
return d1.toDateString() === d2.toDateString();
|
|
182
|
+
}
|
|
183
|
+
DateUtils.sameDay = sameDay;
|
|
184
|
+
/**
|
|
185
|
+
* Two dates are in the same month
|
|
186
|
+
* @param d1 First date
|
|
187
|
+
* @param d2 Second date
|
|
188
|
+
* @returns Result
|
|
189
|
+
*/
|
|
190
|
+
function sameMonth(d1, d2) {
|
|
191
|
+
d1 = parse(d1);
|
|
192
|
+
d2 = parse(d2);
|
|
193
|
+
if (d1 == null || d2 == null)
|
|
194
|
+
return false;
|
|
195
|
+
return (d1.getFullYear() === d2.getFullYear() &&
|
|
196
|
+
d1.getMonth() === d2.getMonth());
|
|
197
|
+
}
|
|
198
|
+
DateUtils.sameMonth = sameMonth;
|
|
160
199
|
})(DateUtils = exports.DateUtils || (exports.DateUtils = {}));
|
package/lib/mjs/DateUtils.d.ts
CHANGED
|
@@ -45,10 +45,11 @@ export declare namespace DateUtils {
|
|
|
45
45
|
*/
|
|
46
46
|
function format(input?: Date | string, locale?: string | string[], options?: FormatOptions, timeZone?: string): string | undefined;
|
|
47
47
|
/**
|
|
48
|
-
* Format to 'yyyy-MM-dd', especially used for date input min/max property
|
|
48
|
+
* Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
|
|
49
49
|
* @param date Input date
|
|
50
|
+
* @param hasSecond 'undefined' for date only, 'false' for hour:minute only
|
|
50
51
|
*/
|
|
51
|
-
function formatForInput(date?: Date | string | null): string;
|
|
52
|
+
function formatForInput(date?: Date | string | null, hasSecond?: boolean): string;
|
|
52
53
|
/**
|
|
53
54
|
* Get month's days
|
|
54
55
|
* @param year Year
|
|
@@ -69,4 +70,18 @@ export declare namespace DateUtils {
|
|
|
69
70
|
* @returns Date
|
|
70
71
|
*/
|
|
71
72
|
function parse(input?: Date | string | null): Date | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Two dates are in the same day
|
|
75
|
+
* @param d1 First date
|
|
76
|
+
* @param d2 Second date
|
|
77
|
+
* @returns Result
|
|
78
|
+
*/
|
|
79
|
+
function sameDay(d1?: Date | string | null, d2?: Date | string | null): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Two dates are in the same month
|
|
82
|
+
* @param d1 First date
|
|
83
|
+
* @param d2 Second date
|
|
84
|
+
* @returns Result
|
|
85
|
+
*/
|
|
86
|
+
function sameMonth(d1?: Date | string | null, d2?: Date | string | null): boolean;
|
|
72
87
|
}
|
package/lib/mjs/DateUtils.js
CHANGED
|
@@ -89,10 +89,11 @@ export var DateUtils;
|
|
|
89
89
|
}
|
|
90
90
|
DateUtils.format = format;
|
|
91
91
|
/**
|
|
92
|
-
* Format to 'yyyy-MM-dd', especially used for date input min/max property
|
|
92
|
+
* Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
|
|
93
93
|
* @param date Input date
|
|
94
|
+
* @param hasSecond 'undefined' for date only, 'false' for hour:minute only
|
|
94
95
|
*/
|
|
95
|
-
function formatForInput(date) {
|
|
96
|
+
function formatForInput(date, hasSecond) {
|
|
96
97
|
// Parse string as date
|
|
97
98
|
if (typeof date === 'string')
|
|
98
99
|
date = new Date(date);
|
|
@@ -104,8 +105,17 @@ export var DateUtils;
|
|
|
104
105
|
(date.getMonth() + 1).toString().padStart(2, '0'),
|
|
105
106
|
date.getDate().toString().padStart(2, '0')
|
|
106
107
|
];
|
|
107
|
-
//
|
|
108
|
-
|
|
108
|
+
// Date
|
|
109
|
+
const d = parts.join('-');
|
|
110
|
+
if (hasSecond == null)
|
|
111
|
+
return d;
|
|
112
|
+
const hm = [
|
|
113
|
+
date.getHours().toString().padStart(2, '0'),
|
|
114
|
+
date.getMinutes().toString().padStart(2, '0')
|
|
115
|
+
];
|
|
116
|
+
if (hasSecond)
|
|
117
|
+
hm.push(date.getSeconds().toString().padStart(2, '0'));
|
|
118
|
+
return `${d}T${hm.join(':')}`;
|
|
109
119
|
}
|
|
110
120
|
DateUtils.formatForInput = formatForInput;
|
|
111
121
|
/**
|
|
@@ -154,4 +164,33 @@ export var DateUtils;
|
|
|
154
164
|
return input;
|
|
155
165
|
}
|
|
156
166
|
DateUtils.parse = parse;
|
|
167
|
+
/**
|
|
168
|
+
* Two dates are in the same day
|
|
169
|
+
* @param d1 First date
|
|
170
|
+
* @param d2 Second date
|
|
171
|
+
* @returns Result
|
|
172
|
+
*/
|
|
173
|
+
function sameDay(d1, d2) {
|
|
174
|
+
d1 = parse(d1);
|
|
175
|
+
d2 = parse(d2);
|
|
176
|
+
if (d1 == null || d2 == null)
|
|
177
|
+
return false;
|
|
178
|
+
return d1.toDateString() === d2.toDateString();
|
|
179
|
+
}
|
|
180
|
+
DateUtils.sameDay = sameDay;
|
|
181
|
+
/**
|
|
182
|
+
* Two dates are in the same month
|
|
183
|
+
* @param d1 First date
|
|
184
|
+
* @param d2 Second date
|
|
185
|
+
* @returns Result
|
|
186
|
+
*/
|
|
187
|
+
function sameMonth(d1, d2) {
|
|
188
|
+
d1 = parse(d1);
|
|
189
|
+
d2 = parse(d2);
|
|
190
|
+
if (d1 == null || d2 == null)
|
|
191
|
+
return false;
|
|
192
|
+
return (d1.getFullYear() === d2.getFullYear() &&
|
|
193
|
+
d1.getMonth() === d2.getMonth());
|
|
194
|
+
}
|
|
195
|
+
DateUtils.sameMonth = sameMonth;
|
|
157
196
|
})(DateUtils || (DateUtils = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.54",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -53,17 +53,16 @@
|
|
|
53
53
|
"url": "https://github.com/ETSOO/Shared/issues"
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
56
|
-
"dependencies": {},
|
|
57
56
|
"devDependencies": {
|
|
58
|
-
"@types/jest": "^
|
|
59
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
60
|
-
"@typescript-eslint/parser": "^5.
|
|
61
|
-
"eslint": "^8.
|
|
57
|
+
"@types/jest": "^29.0.0",
|
|
58
|
+
"@typescript-eslint/eslint-plugin": "^5.36.2",
|
|
59
|
+
"@typescript-eslint/parser": "^5.36.2",
|
|
60
|
+
"eslint": "^8.23.0",
|
|
62
61
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
63
62
|
"eslint-plugin-import": "^2.26.0",
|
|
64
|
-
"jest": "^
|
|
65
|
-
"jest-environment-jsdom": "^
|
|
66
|
-
"ts-jest": "^
|
|
67
|
-
"typescript": "^4.8.
|
|
63
|
+
"jest": "^29.0.3",
|
|
64
|
+
"jest-environment-jsdom": "^29.0.3",
|
|
65
|
+
"ts-jest": "^29.0.0",
|
|
66
|
+
"typescript": "^4.8.3"
|
|
68
67
|
}
|
|
69
68
|
}
|
package/src/DateUtils.ts
CHANGED
|
@@ -145,10 +145,14 @@ export namespace DateUtils {
|
|
|
145
145
|
}
|
|
146
146
|
|
|
147
147
|
/**
|
|
148
|
-
* Format to 'yyyy-MM-dd', especially used for date input min/max property
|
|
148
|
+
* Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property
|
|
149
149
|
* @param date Input date
|
|
150
|
+
* @param hasSecond 'undefined' for date only, 'false' for hour:minute only
|
|
150
151
|
*/
|
|
151
|
-
export function formatForInput(
|
|
152
|
+
export function formatForInput(
|
|
153
|
+
date?: Date | string | null,
|
|
154
|
+
hasSecond?: boolean
|
|
155
|
+
) {
|
|
152
156
|
// Parse string as date
|
|
153
157
|
if (typeof date === 'string') date = new Date(date);
|
|
154
158
|
|
|
@@ -162,8 +166,16 @@ export namespace DateUtils {
|
|
|
162
166
|
date.getDate().toString().padStart(2, '0')
|
|
163
167
|
];
|
|
164
168
|
|
|
165
|
-
//
|
|
166
|
-
|
|
169
|
+
// Date
|
|
170
|
+
const d = parts.join('-');
|
|
171
|
+
if (hasSecond == null) return d;
|
|
172
|
+
|
|
173
|
+
const hm = [
|
|
174
|
+
date.getHours().toString().padStart(2, '0'),
|
|
175
|
+
date.getMinutes().toString().padStart(2, '0')
|
|
176
|
+
];
|
|
177
|
+
if (hasSecond) hm.push(date.getSeconds().toString().padStart(2, '0'));
|
|
178
|
+
return `${d}T${hm.join(':')}`;
|
|
167
179
|
}
|
|
168
180
|
|
|
169
181
|
/**
|
|
@@ -213,4 +225,39 @@ export namespace DateUtils {
|
|
|
213
225
|
}
|
|
214
226
|
return input;
|
|
215
227
|
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Two dates are in the same day
|
|
231
|
+
* @param d1 First date
|
|
232
|
+
* @param d2 Second date
|
|
233
|
+
* @returns Result
|
|
234
|
+
*/
|
|
235
|
+
export function sameDay(
|
|
236
|
+
d1?: Date | string | null,
|
|
237
|
+
d2?: Date | string | null
|
|
238
|
+
) {
|
|
239
|
+
d1 = parse(d1);
|
|
240
|
+
d2 = parse(d2);
|
|
241
|
+
if (d1 == null || d2 == null) return false;
|
|
242
|
+
return d1.toDateString() === d2.toDateString();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Two dates are in the same month
|
|
247
|
+
* @param d1 First date
|
|
248
|
+
* @param d2 Second date
|
|
249
|
+
* @returns Result
|
|
250
|
+
*/
|
|
251
|
+
export function sameMonth(
|
|
252
|
+
d1?: Date | string | null,
|
|
253
|
+
d2?: Date | string | null
|
|
254
|
+
) {
|
|
255
|
+
d1 = parse(d1);
|
|
256
|
+
d2 = parse(d2);
|
|
257
|
+
if (d1 == null || d2 == null) return false;
|
|
258
|
+
return (
|
|
259
|
+
d1.getFullYear() === d2.getFullYear() &&
|
|
260
|
+
d1.getMonth() === d2.getMonth()
|
|
261
|
+
);
|
|
262
|
+
}
|
|
216
263
|
}
|