Skip to content

Commit 5a3410e

Browse files
committed
fix: convertToDate correctly parses time string portions
Added tests for date-time input. The date utilities class uses the new parse ISO implementation which returns `null` instead of `Invalid date` for certain time portions. Adjusted the tests accordingly. This means that the date-time input additional checks around the mask value logic could be simplified but that is for another PR.
1 parent a2695ac commit 5a3410e

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

src/components/calendar/helpers.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ const DaysMap = {
3737

3838
/* Converter functions */
3939

40+
export function isValidDate(date: Date) {
41+
return Number.isNaN(date.valueOf()) ? null : date;
42+
}
43+
44+
export function parseISODate(string: string) {
45+
if (/^\d{4}/.test(string)) {
46+
const time = !string.includes('T') ? 'T00:00:00' : '';
47+
return isValidDate(new Date(`${string}${time}`));
48+
}
49+
50+
if (/^\d{2}/.test(string)) {
51+
const date = first(new Date().toISOString().split('T'));
52+
return isValidDate(new Date(`${date}T${string}`));
53+
}
54+
55+
return null;
56+
}
57+
4058
/**
4159
* Converts the given value to a Date object.
4260
*
@@ -50,8 +68,7 @@ export function convertToDate(value?: Date | string | null): Date | null {
5068
return null;
5169
}
5270

53-
const converted = isString(value) ? new Date(value) : value;
54-
return Number.isNaN(converted.valueOf()) ? null : converted;
71+
return isString(value) ? parseISODate(value) : isValidDate(value);
5572
}
5673

5774
/**

src/components/date-picker/date-picker.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ describe('Date picker', () => {
286286
it('should set the value trough attribute correctly', async () => {
287287
expect(picker.value).to.be.null;
288288
const expectedValue = new CalendarDay({ year: 2024, month: 2, date: 1 });
289-
picker.setAttribute('value', expectedValue.native.toDateString());
289+
picker.setAttribute('value', expectedValue.native.toISOString());
290290
await elementUpdated(picker);
291291

292292
checkDatesEqual(picker.value!, expectedValue);

src/components/date-time-input/date-time-input.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,23 @@ describe('Date Time Input component', () => {
269269
expect(el.value).to.equal(value);
270270
});
271271

272+
it('set value - time portion only', async () => {
273+
const target = new Date();
274+
target.setHours(14, 0, 0, 0);
275+
276+
el.inputFormat = 'HH:mm';
277+
el.value = '14:00';
278+
await elementUpdated(el);
279+
280+
expect(el.value?.valueOf()).to.equal(target.valueOf());
281+
282+
// Invalid time portion
283+
el.value = '23:60';
284+
await elementUpdated(el);
285+
286+
expect(el.value).to.be.null;
287+
});
288+
272289
it('set value - string property binding', async () => {
273290
const value = new Date(2020, 2, 3);
274291
el.value = value.toISOString();

src/components/date-time-input/date-util.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,8 @@ describe('Date Util', () => {
177177
updateDate(new Date(), '11:11').getTime()
178178
);
179179

180+
expect(DateTimeUtil.parseIsoDate('23:60')).to.be.null;
180181
expect(DateTimeUtil.parseIsoDate('')).to.be.null;
181-
expect(
182-
DateTimeUtil.parseIsoDate(new Date().getTime().toString())?.getTime()
183-
).to.be.NaN;
184182
});
185183

186184
it('isValidDate should properly determine if a date is valid or not', () => {

src/components/date-time-input/date-util.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { parseISODate } from '../calendar/helpers.js';
12
import { MaskParser } from '../mask-input/mask-parser.js';
23

34
export enum FormatDesc {
@@ -190,22 +191,7 @@ export abstract class DateTimeUtil {
190191
}
191192

192193
public static parseIsoDate(value: string): Date | null {
193-
let regex = /^\d{4}/g;
194-
const timeLiteral = 'T';
195-
if (regex.test(value)) {
196-
return new Date(
197-
`${value}${value.indexOf(timeLiteral) === -1 ? 'T00:00:00' : ''}`
198-
);
199-
}
200-
201-
regex = /^\d{2}/g;
202-
if (regex.test(value)) {
203-
const dateNow = new Date().toISOString();
204-
const [datePart, _timePart] = dateNow.split(timeLiteral);
205-
return new Date(`${datePart}T${value}`);
206-
}
207-
208-
return null;
194+
return parseISODate(value);
209195
}
210196

211197
public static isValidDate(value: any): value is Date {

0 commit comments

Comments
 (0)