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

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

Closed
wants to merge 3 commits 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
16 changes: 8 additions & 8 deletions src/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -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: '',
Expand All @@ -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: '',
Expand All @@ -132,23 +132,23 @@ 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: '',
})
})

it('handles missing punctuation', () => {
const result = parse('2345', format, baseDate)
const result = parse('2345', format, baseDate, null)
expect(result).toEqual({
date: null,
missingPunctuation: '--',
})
})

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: '',
Expand All @@ -157,15 +157,15 @@ 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: '',
})
})

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: '',
Expand Down
Loading