Skip to content

Commit

Permalink
fix lang on relative-time day ordering
Browse files Browse the repository at this point in the history
Co-authored-by: Kristján Oddsson <koddsson@gmail.com>
  • Loading branch information
keithamus and koddsson committed Oct 17, 2022
1 parent d35f48f commit de27411
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/relative-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class RelativeTime {
}

formatDate(): string {
let format = isDayFirst() ? '%e %b' : '%b %e'
let format = isDayFirst(this.locale) ? '%e %b' : '%b %e'
if (!isThisYear(this.date)) {
format += isYearSeparator() ? ', %Y' : ' %Y'
}
Expand Down
25 changes: 12 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ export function strftime(time: Date, formatString: string): string {
})
}

export function makeFormatter(options: Intl.DateTimeFormatOptions): () => Intl.DateTimeFormat | undefined {
let format: Intl.DateTimeFormat | null
return function (): Intl.DateTimeFormat | undefined {
if (format) return format
export function makeFormatter(
options: Intl.DateTimeFormatOptions
): (locale?: string) => Intl.DateTimeFormat | undefined {
return function (locale?: string): Intl.DateTimeFormat | undefined {
if ('Intl' in window) {
try {
format = new Intl.DateTimeFormat(undefined, options)
return format
return new Intl.DateTimeFormat(locale, options)
} catch (e) {
if (!(e instanceof RangeError)) {
throw e
Expand All @@ -108,24 +107,24 @@ export function makeFormatter(options: Intl.DateTimeFormatOptions): () => Intl.D
}
}

let dayFirst: boolean | null = null
let dayFirst: Record<string, boolean> = {}
const dayFirstFormatter = makeFormatter({day: 'numeric', month: 'short'})

// Private: Determine if the day should be formatted before the month name in
// the user's current locale. For example, `9 Jun` for en-GB and `Jun 9`
// for en-US.
//
// Returns true if the day appears before the month.
export function isDayFirst(): boolean {
if (dayFirst !== null) {
return dayFirst
export function isDayFirst(locale: string = 'default'): boolean {
if (locale in dayFirst) {
return dayFirst[locale]
}

const formatter = dayFirstFormatter()
const formatter = dayFirstFormatter(locale)
if (formatter) {
const output = formatter.format(new Date(0))
dayFirst = !!output.match(/^\d/)
return dayFirst
dayFirst[locale] = !!output.match(/^\d/)
return dayFirst[locale]
} else {
return false
}
Expand Down
2 changes: 2 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ suite('relative-time', function () {
test('switches to dates after 30 past days', function () {
const now = new Date(Date.now() - 30 * 60 * 60 * 24 * 1000).toISOString()
const time = document.createElement('relative-time')
time.setAttribute('lang', 'en-US')
time.setAttribute('datetime', now)
assert.match(time.textContent, /on [A-Z][a-z]{2} \d{1,2}/)
})

test('switches to dates after 30 future days', function () {
const now = new Date(Date.now() + 30 * 60 * 60 * 24 * 1000).toISOString()
const time = document.createElement('relative-time')
time.setAttribute('lang', 'en-US')
time.setAttribute('datetime', now)
assert.match(time.textContent, /on [A-Z][a-z]{2} \d{1,2}/)
})
Expand Down

0 comments on commit de27411

Please sign in to comment.