Skip to content

Commit

Permalink
Replace NNBSP with space. (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
tvartom authored and nilssonemma committed Jul 5, 2023
1 parent 8a93e06 commit 7cb59de
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
20 changes: 12 additions & 8 deletions Date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ export namespace Date {
return create(new globalThis.Date())
}
export function localize(value: Date | globalThis.Date, locale?: Locale, timezone?: string): Date {
return (is(value) ? parse(value) : value)
.toLocaleString(locale ? locale : Intl.DateTimeFormat().resolvedOptions().locale, {
year: "numeric",
month: "2-digit",
day: "2-digit",
timeZone: timezone ?? Intl.DateTimeFormat().resolvedOptions().timeZone,
})
.substring(0, 10)
return (
(is(value) ? parse(value) : value)
.toLocaleString(locale ? locale : Intl.DateTimeFormat().resolvedOptions().locale, {
year: "numeric",
month: "2-digit",
day: "2-digit",
timeZone: timezone ?? Intl.DateTimeFormat().resolvedOptions().timeZone,
})
.substring(0, 10)
// See DateTime:localize for note.
.replaceAll(" ", " ")
)
}
export function next(date: Date, days: number | DateSpan = 1): Date {
let result: Date
Expand Down
8 changes: 4 additions & 4 deletions DateTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ describe("DateTime", () => {
expect(isoly.DateTime.localize(new Date(date[0])).split(" ")[1]).toEqual(date[1])
)
it("localize DateTime with locale", () => {
expect(isoly.DateTime.localize("2020-12-31T23:59:59.000Z", "en-US")).toEqual("01/01/2021, 12:59:59AM")
expect(isoly.DateTime.localize("2020-12-31T23:59:59.000Z", "en-US")).toEqual("01/01/2021, 12:59:59 AM")
})

it("localize 2020-12-31T23:59Z to en-US", () => {
expect(isoly.DateTime.localize("2020-12-31T23:59Z", "en-US")).toEqual("01/01/2021, 12:59AM")
expect(isoly.DateTime.localize("2020-12-31T23:59Z", "en-US")).toEqual("01/01/2021, 12:59 AM")
})
it("localize 2020-12-31T23Z to en-US", () => {
expect(isoly.DateTime.localize("2020-12-31T23Z", "en-US")).toEqual("01/01/2021, 12AM")
expect(isoly.DateTime.localize("2020-12-31T23Z", "en-US")).toEqual("01/01/2021, 12 AM")
})
it('localize 2020-12-31T23:12Z to en-US { month: "short", day: "numeric", hour: "numeric", minute: "numeric" }', () => {
expect(
Expand All @@ -122,7 +122,7 @@ describe("DateTime", () => {
{ month: "short", day: "numeric", hour: "numeric", minute: "numeric" },
"en-US"
)
).toEqual("Jan 1, 12:12AM")
).toEqual("Jan 1, 12:12 AM")
})
it('localize 2020-12-31T23:12Z to de-DE { month: "short", day: "numeric", hour: "numeric", minute: "numeric" }', () => {
expect(
Expand Down
9 changes: 8 additions & 1 deletion DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ export namespace DateTime {
let result: string
if (typeof locale == "object") {
const localeString = timeZone ? timeZone : Intl.DateTimeFormat().resolvedOptions().locale
result = (is(value) ? parse(value) : value).toLocaleString(localeString, locale)
result = (is(value) ? parse(value) : value)
.toLocaleString(localeString, locale)
// For consistency, replace NNBSP with space:
// Unicode has decided to use `Narrow No-Break Space (NNBSP)` (U+202F) instead of space in some cases.
// It breaks tests, when running in different environments.
// https://icu.unicode.org/download/72#:~:text=In%20many%20formatting%20patterns%2C%20ASCII%20spaces%20are%20replaced%20with%20Unicode%20spaces%20(e.g.%2C%20a%20%22thin%20space%22)
// This can be removed, with a breaking change and updated tests, when all systems use updated versions of ICU.
.replaceAll(" ", " ")
} else {
const precision = is(value) ? DateTime.precision(value) : "milliseconds"
result = localize(
Expand Down

0 comments on commit 7cb59de

Please sign in to comment.