Skip to content

Commit 9557b41

Browse files
refactor(date): rename formatDate to dateFormat, subject first
1 parent b22a1a2 commit 9557b41

File tree

7 files changed

+119
-129
lines changed

7 files changed

+119
-129
lines changed

src/date/dateFormat.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { expect, test } from "bun:test"
2+
import { dateFormat } from "~utils/date/dateFormat"
3+
import { dateFormatStyle } from "~utils/date/dateFormatStyle"
4+
5+
const string = "2024-08-31T12:00:00.000Z"
6+
const date = new Date(string)
7+
8+
const en = "en"
9+
const de = "de"
10+
11+
test("dateFormat.full.en", () => {
12+
const f = dateFormat(date, dateFormatStyle.full, en)
13+
expect(f).toEqual("August 31, 2024")
14+
})
15+
16+
test("dateFormat.full.de", () => {
17+
const f = dateFormat(date, dateFormatStyle.full, de)
18+
expect(f).toEqual("31. August 2024")
19+
})
20+
21+
test("dateFormat.numeric.en", () => {
22+
const f = dateFormat(date, dateFormatStyle.numeric, en)
23+
expect(f).toEqual("8/31/2024")
24+
})
25+
26+
test("dateFormat.numeric.de", () => {
27+
const f = dateFormat(date, dateFormatStyle.numeric, de)
28+
expect(f).toEqual("31.8.2024")
29+
})
30+
31+
test("dateFormat.short.en", () => {
32+
const f = dateFormat(date, dateFormatStyle.short, en)
33+
expect(f).toEqual("8/31/24")
34+
})
35+
36+
test("dateFormat.short.de", () => {
37+
const f = dateFormat(date, dateFormatStyle.short, de)
38+
expect(f).toEqual("31.8.24")
39+
})
40+
41+
test("dateFormat.iso8601", () => {
42+
const f = dateFormat(date, dateFormatStyle.iso)
43+
expect(f).toEqual("2024-08-31")
44+
})

src/date/dateFormat.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { dateFormatStyle, type DateFormatStyle } from "~utils/date/dateFormatStyle"
2+
import { dateFormatterGetCached } from "./dateFormatterGetCached"
3+
4+
export function dateFormat(date: Date, format: DateFormatStyle, l?: string) {
5+
const formatter = dateFormatterGetCached(format, l)
6+
return formatter.format(date)
7+
}
8+
9+
export function dateFormatFull(date: Date | string, l?: string) {
10+
return dateFormat(typeof date === "string" ? new Date(date) : date, dateFormatStyle.full, l)
11+
}
12+
13+
export function dateFormatNumeric(date: Date | string, l?: string) {
14+
return dateFormat(typeof date === "string" ? new Date(date) : date, dateFormatStyle.numeric, l)
15+
}
16+
17+
export function dateFormatShort(date: Date | string, l?: string) {
18+
return dateFormat(typeof date === "string" ? new Date(date) : date, dateFormatStyle.short, l)
19+
}
20+
21+
export function dateFormatIso(date: Date | string, l?: string) {
22+
return dateFormat(typeof date === "string" ? new Date(date) : date, dateFormatStyle.iso, l)
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as a from "valibot"
22

3-
export type DateOnlyFormat = keyof typeof dateOnlyFormat
3+
export type DateFormatStyle = keyof typeof dateFormatStyle
44

5-
export const dateOnlyFormat = {
5+
export const dateFormatStyle = {
66
full: "full",
77
numeric: "numeric",
88
short: "short",
@@ -12,4 +12,4 @@ export const dateOnlyFormat = {
1212
iso: "iso",
1313
} as const
1414

15-
export const dateOnlyFormatSchema = a.enum(dateOnlyFormat)
15+
export const dateFormatStyleSchema = a.enum(dateFormatStyle)

src/date/dateFormatterCreate.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { type DateFormatStyle, dateFormatStyle } from "~utils/date/dateFormatStyle"
2+
3+
export function dateFormatterCreate(format: DateFormatStyle, l?: string): Intl.DateTimeFormat {
4+
switch (format) {
5+
case dateFormatStyle.full: {
6+
return new Intl.DateTimeFormat(l ?? undefined, {
7+
year: "numeric",
8+
month: "long",
9+
day: "numeric",
10+
})
11+
}
12+
case dateFormatStyle.numeric: {
13+
return new Intl.DateTimeFormat(l ?? undefined, {
14+
month: "numeric",
15+
day: "numeric",
16+
year: "numeric",
17+
})
18+
}
19+
case dateFormatStyle.short: {
20+
return new Intl.DateTimeFormat(l ?? undefined, {
21+
year: "2-digit",
22+
month: "numeric",
23+
day: "numeric",
24+
})
25+
}
26+
case dateFormatStyle.iso: {
27+
return new Intl.DateTimeFormat("sv-SE", {
28+
year: "numeric",
29+
month: "numeric",
30+
day: "numeric",
31+
})
32+
}
33+
}
34+
}

src/date/dateFormatterGetCached.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { dateFormatterCreate } from "~utils/date/dateFormatterCreate"
2+
import { type DateFormatStyle } from "./dateFormatStyle"
3+
4+
let cachedLocale: string | undefined = undefined
5+
let cachedDateFormat: DateFormatStyle | undefined = undefined
6+
let cachedFormatter: Intl.DateTimeFormat | undefined = undefined
7+
8+
export function dateFormatterGetCached(format: DateFormatStyle, l?: string): Intl.DateTimeFormat {
9+
if (!cachedFormatter || cachedLocale !== l || cachedDateFormat !== format) {
10+
cachedLocale = l
11+
cachedDateFormat = format
12+
cachedFormatter = dateFormatterCreate(format, l)
13+
}
14+
return cachedFormatter
15+
}

src/date/formatDate.test.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/date/formatDate.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)