Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.14.2 Fix parse value when format has not year (MM-dd) #106

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 2 additions & 2 deletions src/lib/DateInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/lib/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading