Skip to content

Commit

Permalink
Add date-fns 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
probablykasper committed May 22, 2024
1 parent 9b49671 commit e3324d3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next
- Add MMM format to allow short month names (@peterbell215)
- Add date-fns 3 support

## 2.12.0 - 2024 Apr 9
- Disable autocomplete for date input (@gianarb)
Expand Down
38 changes: 6 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@typescript-eslint/eslint-plugin": "^7.10.0",
"@typescript-eslint/parser": "^7.10.0",
"@vitest/coverage-v8": "^1.6.0",
"date-fns": "^2.30.0",
"date-fns": "^3.6.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.39.0",
Expand Down
13 changes: 8 additions & 5 deletions src/lib/locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ export function getInnerLocale(locale: Locale): InnerLocale {
return innerLocale
}

type Month = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
type Day = 0 | 1 | 2 | 3 | 4 | 5 | 6
type LocaleWidth = 'short' | 'wide' | 'abbreviated' | 'narrow' | 'any'
type DateFnsLocale = {
options?: {
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6
}
localize?: {
month: (n: number, options?: { width?: string }) => string
day: (i: number, options?: { width?: string }) => string
month: (n: Month, options?: { width?: LocaleWidth }) => string
day: (i: Day, options?: { width?: LocaleWidth }) => string
}
}
/** Create a Locale from a date-fns locale */
Expand All @@ -73,12 +76,12 @@ export function localeFromDateFnsLocale(dateFnsLocale: DateFnsLocale): InnerLoca
if (dateFnsLocale.localize) {
for (let i = 0; i < 7; i++) {
// widths: narrow, short, abbreviated, wide, any
locale.weekdays[i] = dateFnsLocale.localize.day(i, { width: 'short' })
locale.weekdays[i] = dateFnsLocale.localize.day(i as Day, { width: 'short' })
}

for (let i = 0; i < 12; i++) {
locale.months[i] = dateFnsLocale.localize.month(i, { width: 'wide' })
locale.shortMonths[i] = dateFnsLocale.localize.month(i, { width: 'abbreviated' })
locale.months[i] = dateFnsLocale.localize.month(i as Month, { width: 'wide' })
locale.shortMonths[i] = dateFnsLocale.localize.month(i as Month, { width: 'abbreviated' })
}
}
return locale
Expand Down
7 changes: 1 addition & 6 deletions src/routes/DateInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import Prop from './prop.svelte'
import Split from './split.svelte'
import { localeFromDateFnsLocale } from '$lib'
// had to import it this way to avoid errors
// in `npm run build:site` or `npm run check`:
import hy from 'date-fns/locale/hy/index'
import de from 'date-fns/locale/de/index'
import nb from 'date-fns/locale/nb/index'
import { hy, de, nb } from 'date-fns/locale'
let id: string
let placeholder: string
Expand Down
7 changes: 1 addition & 6 deletions src/routes/DatePicker.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import { localeFromDateFnsLocale } from '$lib/locale.js'
import Prop from './prop.svelte'
import Split from './split.svelte'
// had to import it this way to avoid errors
// in `npm run build:site` or `npm run check`:
import hy from 'date-fns/locale/hy/index'
import de from 'date-fns/locale/de/index'
import nb from 'date-fns/locale/nb/index'
import { hy, de, nb } from 'date-fns/locale'
let value: Date
let min: Date
Expand Down

0 comments on commit e3324d3

Please sign in to comment.