@etsoo/shared 1.1.50 → 1.1.53

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 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
@@ -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 result2 = DateUtils.formatForInput(new Date(2021, 5, 6));
59
+ const d = new Date(2021, 5, 6, 20, 18, 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:18');
65
+
66
+ const result4 = DateUtils.formatForInput(d, true);
67
+ expect(result4).toBe('2021-06-06T20:18: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
+ });
@@ -344,4 +344,4 @@ export declare type IdDefaultType<T extends object> = T extends {
344
344
  */
345
345
  export declare type LabelDefaultType<T extends object> = T extends {
346
346
  label: string;
347
- } ? DataTypes.Keys<T> & 'label' : DataTypes.Keys<T>;
347
+ } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
@@ -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
  }
@@ -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,14 @@ var DateUtils;
107
108
  (date.getMonth() + 1).toString().padStart(2, '0'),
108
109
  date.getDate().toString().padStart(2, '0')
109
110
  ];
110
- // Return
111
- return parts.join('-');
111
+ // Date
112
+ const d = parts.join('-');
113
+ if (hasSecond == null)
114
+ return d;
115
+ const hm = [date.getHours(), date.getMinutes()];
116
+ if (hasSecond)
117
+ hm.push(date.getSeconds());
118
+ return `${d}T${hm.join(':')}`;
112
119
  }
113
120
  DateUtils.formatForInput = formatForInput;
114
121
  /**
@@ -157,4 +164,33 @@ var DateUtils;
157
164
  return input;
158
165
  }
159
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;
160
196
  })(DateUtils = exports.DateUtils || (exports.DateUtils = {}));
@@ -344,4 +344,4 @@ export declare type IdDefaultType<T extends object> = T extends {
344
344
  */
345
345
  export declare type LabelDefaultType<T extends object> = T extends {
346
346
  label: string;
347
- } ? DataTypes.Keys<T> & 'label' : DataTypes.Keys<T>;
347
+ } ? DataTypes.Keys<T, string> & 'label' : DataTypes.Keys<T, string>;
@@ -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
  }
@@ -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,14 @@ export var DateUtils;
104
105
  (date.getMonth() + 1).toString().padStart(2, '0'),
105
106
  date.getDate().toString().padStart(2, '0')
106
107
  ];
107
- // Return
108
- return parts.join('-');
108
+ // Date
109
+ const d = parts.join('-');
110
+ if (hasSecond == null)
111
+ return d;
112
+ const hm = [date.getHours(), date.getMinutes()];
113
+ if (hasSecond)
114
+ hm.push(date.getSeconds());
115
+ return `${d}T${hm.join(':')}`;
109
116
  }
110
117
  DateUtils.formatForInput = formatForInput;
111
118
  /**
@@ -154,4 +161,33 @@ export var DateUtils;
154
161
  return input;
155
162
  }
156
163
  DateUtils.parse = parse;
164
+ /**
165
+ * Two dates are in the same day
166
+ * @param d1 First date
167
+ * @param d2 Second date
168
+ * @returns Result
169
+ */
170
+ function sameDay(d1, d2) {
171
+ d1 = parse(d1);
172
+ d2 = parse(d2);
173
+ if (d1 == null || d2 == null)
174
+ return false;
175
+ return d1.toDateString() === d2.toDateString();
176
+ }
177
+ DateUtils.sameDay = sameDay;
178
+ /**
179
+ * Two dates are in the same month
180
+ * @param d1 First date
181
+ * @param d2 Second date
182
+ * @returns Result
183
+ */
184
+ function sameMonth(d1, d2) {
185
+ d1 = parse(d1);
186
+ d2 = parse(d2);
187
+ if (d1 == null || d2 == null)
188
+ return false;
189
+ return (d1.getFullYear() === d2.getFullYear() &&
190
+ d1.getMonth() === d2.getMonth());
191
+ }
192
+ DateUtils.sameMonth = sameMonth;
157
193
  })(DateUtils || (DateUtils = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.50",
3
+ "version": "1.1.53",
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": "^28.1.8",
59
- "@typescript-eslint/eslint-plugin": "^5.35.1",
60
- "@typescript-eslint/parser": "^5.35.1",
61
- "eslint": "^8.22.0",
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": "^28.1.3",
65
- "jest-environment-jsdom": "^28.1.3",
66
- "ts-jest": "^28.0.8",
67
- "typescript": "^4.8.2"
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/DataTypes.ts CHANGED
@@ -668,5 +668,5 @@ export type IdDefaultType<T extends object> = T extends { id: number | string }
668
668
  * Label default type
669
669
  */
670
670
  export type LabelDefaultType<T extends object> = T extends { label: string }
671
- ? DataTypes.Keys<T> & 'label'
672
- : DataTypes.Keys<T>;
671
+ ? DataTypes.Keys<T, string> & 'label'
672
+ : DataTypes.Keys<T, string>;
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(date?: Date | string | null) {
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,13 @@ export namespace DateUtils {
162
166
  date.getDate().toString().padStart(2, '0')
163
167
  ];
164
168
 
165
- // Return
166
- return parts.join('-');
169
+ // Date
170
+ const d = parts.join('-');
171
+ if (hasSecond == null) return d;
172
+
173
+ const hm = [date.getHours(), date.getMinutes()];
174
+ if (hasSecond) hm.push(date.getSeconds());
175
+ return `${d}T${hm.join(':')}`;
167
176
  }
168
177
 
169
178
  /**
@@ -213,4 +222,39 @@ export namespace DateUtils {
213
222
  }
214
223
  return input;
215
224
  }
225
+
226
+ /**
227
+ * Two dates are in the same day
228
+ * @param d1 First date
229
+ * @param d2 Second date
230
+ * @returns Result
231
+ */
232
+ export function sameDay(
233
+ d1?: Date | string | null,
234
+ d2?: Date | string | null
235
+ ) {
236
+ d1 = parse(d1);
237
+ d2 = parse(d2);
238
+ if (d1 == null || d2 == null) return false;
239
+ return d1.toDateString() === d2.toDateString();
240
+ }
241
+
242
+ /**
243
+ * Two dates are in the same month
244
+ * @param d1 First date
245
+ * @param d2 Second date
246
+ * @returns Result
247
+ */
248
+ export function sameMonth(
249
+ d1?: Date | string | null,
250
+ d2?: Date | string | null
251
+ ) {
252
+ d1 = parse(d1);
253
+ d2 = parse(d2);
254
+ if (d1 == null || d2 == null) return false;
255
+ return (
256
+ d1.getFullYear() === d2.getFullYear() &&
257
+ d1.getMonth() === d2.getMonth()
258
+ );
259
+ }
216
260
  }