From d23c21a5ca28a627a2d547560d15bdd101f33762 Mon Sep 17 00:00:00 2001 From: "EPIC-LAPTOP-123\\jjaime" Date: Mon, 25 Nov 2024 16:21:17 -0600 Subject: [PATCH 1/2] 2.14.2 Fix parse value when format has not year (MM-dd) --- CHANGELOG.md | 3 +++ src/lib/DateInput.svelte | 4 ++-- src/lib/parse.ts | 9 +++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4884588..8de45f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 2.14.2 - 2024 Nov 25 +- Fix parse value when format has not year (MM-dd) (@jorgejaime) + ## 2.14.1 - 2024 Nov 6 - Fix value bindings not always causing updates (@asdfghjkkl11) diff --git a/src/lib/DateInput.svelte b/src/lib/DateInput.svelte index f91e050..f9c6e2a 100644 --- a/src/lib/DateInput.svelte +++ b/src/lib/DateInput.svelte @@ -77,7 +77,7 @@ function textUpdate(text: string, formatTokens: FormatToken[]) { if (text.length) { - const result = parse(text, formatTokens, $store) + const result = parse(text, formatTokens, $store, min) if (result.date !== null) { valid = true store.set(result.date) @@ -210,7 +210,7 @@ e.currentTarget.value === text + e.data ) { // check for missing punctuation, and add if there is any - let result = parse(text, formatTokens, $store) + let result = parse(text, formatTokens, $store, min) if (result.missingPunctuation !== '' && !result.missingPunctuation.startsWith(e.data)) { text = text + result.missingPunctuation + e.data return diff --git a/src/lib/parse.ts b/src/lib/parse.ts index d9911da..4e35d04 100644 --- a/src/lib/parse.ts +++ b/src/lib/parse.ts @@ -15,11 +15,16 @@ type ParseResult = { } /** Parse a string according to the supplied format tokens. Returns a date if successful, and the missing punctuation if there is any that should be after the string */ -export function parse(str: string, tokens: FormatToken[], baseDate: Date | null): ParseResult { +export function parse( + str: string, + tokens: FormatToken[], + baseDate: Date | null, + min: Date | null, +): ParseResult { let missingPunctuation = '' let valid = true - baseDate = baseDate || new Date(2020, 0, 1, 0, 0, 0, 0) + baseDate = min || baseDate || new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0) let year = baseDate.getFullYear() let month = baseDate.getMonth() let day = baseDate.getDate() From 69721f711666c78b7f9d9a672136159866089d73 Mon Sep 17 00:00:00 2001 From: "EPIC-LAPTOP-123\\jjaime" Date: Mon, 25 Nov 2024 16:31:43 -0600 Subject: [PATCH 2/2] Missing file --- src/lib/utils.test.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/lib/utils.test.ts b/src/lib/utils.test.ts index aa6aa3d..304c23c 100644 --- a/src/lib/utils.test.ts +++ b/src/lib/utils.test.ts @@ -105,7 +105,7 @@ describe('parse()', () => { const format = createFormat('yyyy--MM-dd HH:mm:ss') it('works with a basic date', () => { - const result = parse('1234--12-31 23:59:59', format, baseDate) + const result = parse('1234--12-31 23:59:59', format, baseDate, null) expect(result).toEqual({ date: new Date(1234, 11, 31, 23, 59, 59, 999), missingPunctuation: '', @@ -114,7 +114,7 @@ describe('parse()', () => { it('works with a short month date', () => { const format = createFormat('dd MMM yyyy HH:mm:ss') - const result = parse('31 Dec 2022 23:59:59', format, baseDate) + const result = parse('31 Dec 2022 23:59:59', format, baseDate, null) expect(result).toEqual({ date: new Date(2022, 11, 31, 23, 59, 59, 999), missingPunctuation: '', @@ -123,7 +123,7 @@ describe('parse()', () => { it('works with a short month date in non-En locale', () => { const format = createFormat('dd MMM yyyy HH:mm:ss', localeFromDateFnsLocale(nb)) - const result = parse('31 des. 2022 23:59:59', format, baseDate) + const result = parse('31 des. 2022 23:59:59', format, baseDate, null) expect(result).toEqual({ date: new Date(2022, 11, 31, 23, 59, 59, 999), missingPunctuation: '', @@ -132,7 +132,7 @@ describe('parse()', () => { it('handles badly formed month name', () => { const format = createFormat('dd MMM yyyy HH:mm:ss') - const result = parse('31 Dex 2022 23:59:59', format, baseDate) + const result = parse('31 Dex 2022 23:59:59', format, baseDate, null) expect(result).toEqual({ date: null, missingPunctuation: '', @@ -140,7 +140,7 @@ describe('parse()', () => { }) it('handles missing punctuation', () => { - const result = parse('2345', format, baseDate) + const result = parse('2345', format, baseDate, null) expect(result).toEqual({ date: null, missingPunctuation: '--', @@ -148,7 +148,7 @@ describe('parse()', () => { }) it('fails with too high minute', () => { - const result = parse('1234--12-31 23:99:59', format, baseDate) + const result = parse('1234--12-31 23:99:59', format, baseDate, null) expect(result).toEqual({ date: null, missingPunctuation: '', @@ -157,7 +157,7 @@ describe('parse()', () => { it('fails with too high date-of-month', () => { // separate test because some months have less than 31 days - const dayOfMonthOverflow = parse('1234--02-31 23:59:59', format, baseDate) + const dayOfMonthOverflow = parse('1234--02-31 23:59:59', format, baseDate, null) expect(dayOfMonthOverflow).toEqual({ date: null, missingPunctuation: '', @@ -165,7 +165,7 @@ describe('parse()', () => { }) it('fails when the second has a non-numeric character', () => { - const noNumber = parse('1234--02-31 23:59:5d', format, baseDate) + const noNumber = parse('1234--02-31 23:59:5d', format, baseDate, null) expect(noNumber).toEqual({ date: null, missingPunctuation: '',