@etsoo/shared 1.1.52 → 1.1.55

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
@@ -145,6 +145,8 @@ Dates related utilities
145
145
  |formatForInput|Format to 'yyyy-MM-dd' or 'yyyy-MM-ddThh:mm:ss, especially used for date input min/max property|
146
146
  |jsonParser|JSON parser|
147
147
  |parse|Parse string to date|
148
+ |sameDay|Two dates are in the same day|
149
+ |sameMonth|Two dates are in the same month|
148
150
  |substract|Date extended method, substract a date|
149
151
 
150
152
  ## DomUtils
@@ -56,15 +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 d = new Date(2021, 5, 6, 20, 18, 45);
59
+ const d = new Date(2021, 5, 6, 20, 8, 45);
60
60
  const result2 = DateUtils.formatForInput(d);
61
61
  expect(result2).toBe('2021-06-06');
62
62
 
63
63
  const result3 = DateUtils.formatForInput(d, false);
64
- expect(result3).toBe('2021-06-06T20:18');
64
+ expect(result3).toBe('2021-06-06T20:08');
65
65
 
66
66
  const result4 = DateUtils.formatForInput(d, true);
67
- expect(result4).toBe('2021-06-06T20:18:45');
67
+ expect(result4).toBe('2021-06-06T20:08:45');
68
68
  });
69
69
 
70
70
  test('Tests for substract', () => {
@@ -76,3 +76,26 @@ test('Tests for substract', () => {
76
76
  expect(d4.substract(d1).totalYears < 2).toBeTruthy();
77
77
  expect(d2.substract(d1).totalMinutes > 10).toBeTruthy();
78
78
  });
79
+
80
+ test('Tests for parse', () => {
81
+ const d1 = DateUtils.parse('2014-01-01T13:13:34.441Z')?.toJSON();
82
+ expect(d1).toBe('2014-01-01T13:13:34.441Z');
83
+ });
84
+
85
+ test('Tests for sameDay', () => {
86
+ expect(
87
+ DateUtils.sameDay('2022/9/11 22:03', new Date(2022, 8, 11, 10, 3))
88
+ ).toBeTruthy();
89
+ expect(
90
+ DateUtils.sameDay('2022/9/11 22:03', new Date(2022, 9, 11, 10, 3))
91
+ ).toBeFalsy();
92
+ });
93
+
94
+ test('Tests for sameMonth', () => {
95
+ expect(
96
+ DateUtils.sameMonth('2022-09-11 22:03', new Date(2022, 8, 10, 10, 3))
97
+ ).toBeTruthy();
98
+ expect(
99
+ DateUtils.sameMonth('2022/9/11 22:03', '2022/8/31 23:59:59')
100
+ ).toBeFalsy();
101
+ });
@@ -70,4 +70,18 @@ export declare namespace DateUtils {
70
70
  * @returns Date
71
71
  */
72
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;
73
87
  }
@@ -112,9 +112,12 @@ var DateUtils;
112
112
  const d = parts.join('-');
113
113
  if (hasSecond == null)
114
114
  return d;
115
- const hm = [date.getHours(), date.getMinutes()];
115
+ const hm = [
116
+ date.getHours().toString().padStart(2, '0'),
117
+ date.getMinutes().toString().padStart(2, '0')
118
+ ];
116
119
  if (hasSecond)
117
- hm.push(date.getSeconds());
120
+ hm.push(date.getSeconds().toString().padStart(2, '0'));
118
121
  return `${d}T${hm.join(':')}`;
119
122
  }
120
123
  DateUtils.formatForInput = formatForInput;
@@ -164,4 +167,33 @@ var DateUtils;
164
167
  return input;
165
168
  }
166
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;
167
199
  })(DateUtils = exports.DateUtils || (exports.DateUtils = {}));
@@ -70,4 +70,18 @@ export declare namespace DateUtils {
70
70
  * @returns Date
71
71
  */
72
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;
73
87
  }
@@ -109,9 +109,12 @@ export var DateUtils;
109
109
  const d = parts.join('-');
110
110
  if (hasSecond == null)
111
111
  return d;
112
- const hm = [date.getHours(), date.getMinutes()];
112
+ const hm = [
113
+ date.getHours().toString().padStart(2, '0'),
114
+ date.getMinutes().toString().padStart(2, '0')
115
+ ];
113
116
  if (hasSecond)
114
- hm.push(date.getSeconds());
117
+ hm.push(date.getSeconds().toString().padStart(2, '0'));
115
118
  return `${d}T${hm.join(':')}`;
116
119
  }
117
120
  DateUtils.formatForInput = formatForInput;
@@ -161,4 +164,33 @@ export var DateUtils;
161
164
  return input;
162
165
  }
163
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;
164
196
  })(DateUtils || (DateUtils = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.52",
3
+ "version": "1.1.55",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,15 +54,15 @@
54
54
  },
55
55
  "homepage": "https://github.com/ETSOO/Shared#readme",
56
56
  "devDependencies": {
57
- "@types/jest": "^28.1.8",
58
- "@typescript-eslint/eslint-plugin": "^5.36.1",
59
- "@typescript-eslint/parser": "^5.36.1",
57
+ "@types/jest": "^29.0.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.36.2",
59
+ "@typescript-eslint/parser": "^5.36.2",
60
60
  "eslint": "^8.23.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.26.0",
63
- "jest": "^28.1.3",
64
- "jest-environment-jsdom": "^28.1.3",
65
- "ts-jest": "^28.0.8",
66
- "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"
67
67
  }
68
68
  }
package/src/DateUtils.ts CHANGED
@@ -170,8 +170,11 @@ export namespace DateUtils {
170
170
  const d = parts.join('-');
171
171
  if (hasSecond == null) return d;
172
172
 
173
- const hm = [date.getHours(), date.getMinutes()];
174
- if (hasSecond) hm.push(date.getSeconds());
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'));
175
178
  return `${d}T${hm.join(':')}`;
176
179
  }
177
180
 
@@ -222,4 +225,39 @@ export namespace DateUtils {
222
225
  }
223
226
  return input;
224
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
+ }
225
263
  }