Skip to content

Commit 1a98356

Browse files
committed
extract toString from relative-time into element
1 parent 47880c2 commit 1a98356

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

src/relative-time-element.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,47 @@ import ExtendedTimeElement from './extended-time-element.js'
44
import {localeFromElement} from './utils.js'
55

66
export default class RelativeTimeElement extends ExtendedTimeElement {
7+
static get observedAttributes() {
8+
return [...ExtendedTimeElement.observedAttributes, 'prefix']
9+
}
10+
711
getFormattedDate(): string | undefined {
812
const date = this.date
913
if (!date) return
10-
return new RelativeTime(date, localeFromElement(this)).toString({
11-
tense: this.tense,
12-
format: this.format
13-
})
14+
const relativeTime = new RelativeTime(date, localeFromElement(this))
15+
const format = this.format
16+
const tense = this.tense
17+
const micro = format === 'micro'
18+
if (tense === 'past') {
19+
return micro ? relativeTime.microTimeAgo() : relativeTime.timeAgo()
20+
}
21+
if (tense === 'future') {
22+
return micro ? relativeTime.microTimeUntil() : relativeTime.timeUntil()
23+
}
24+
if (format === 'auto') {
25+
const ago = micro ? relativeTime.microTimeAgo() : relativeTime.timeElapsed()
26+
if (ago) {
27+
return ago
28+
}
29+
const ahead = micro ? relativeTime.microTimeUntil() : relativeTime.timeAhead()
30+
if (ahead) {
31+
return ahead
32+
}
33+
}
34+
if (format !== 'auto' && format !== 'micro') {
35+
return relativeTime.formatDate(format)
36+
}
37+
return `${this.prefix ? this.prefix + ' ' : ''}${relativeTime.formatDate()}`
38+
}
39+
40+
/** @deprecated */
41+
get prefix(): string {
42+
return this.getAttribute('prefix') ?? 'on'
43+
}
44+
45+
/** @deprecated */
46+
set prefix(value: string) {
47+
this.setAttribute('prefix', value)
1448
}
1549

1650
get tense(): Tense {

src/relative-time.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,12 @@ export type Tense = 'auto' | 'past' | 'future'
77
export default class RelativeTime {
88
date: Date
99
locale: string
10-
10+
1111
constructor(date: Date, locale: string) {
1212
this.date = date
1313
this.locale = locale
1414
}
1515

16-
toString({format = 'auto', tense = 'auto'}: {format?: Format; tense?: Tense} = {}): string | undefined {
17-
const micro = format === 'micro'
18-
if (tense === 'past') {
19-
return micro ? this.microTimeAgo() : this.timeAgo()
20-
}
21-
if (tense === 'future') {
22-
return micro ? this.microTimeUntil() : this.timeUntil()
23-
}
24-
if (format === 'auto') {
25-
const ago = micro ? this.microTimeAgo() : this.timeElapsed()
26-
if (ago) {
27-
return ago
28-
}
29-
const ahead = micro ? this.microTimeUntil() : this.timeAhead()
30-
if (ahead) {
31-
return ahead
32-
}
33-
}
34-
if (format !== 'auto' && format !== 'micro') {
35-
return this.formatDate(format)
36-
}
37-
return `on ${this.formatDate()}`
38-
}
39-
4016
timeElapsed(): string | undefined | null {
4117
const ms = new Date().getTime() - this.date.getTime()
4218
const sec = Math.round(ms / 1000)

test/relative-time.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ suite('relative-time', function () {
130130
assert.match(time.textContent, /on [A-Z][a-z]{2} \d{1,2}/)
131131
})
132132

133+
test('uses `prefix` attribute to customise prefix', function () {
134+
const now = new Date(Date.now() + 30 * 60 * 60 * 24 * 1000).toISOString()
135+
const time = document.createElement('relative-time')
136+
time.setAttribute('prefix', 'will happen by')
137+
time.setAttribute('lang', 'en-US')
138+
time.setAttribute('datetime', now)
139+
assert.match(time.textContent, /will happen by [A-Z][a-z]{2} \d{1,2}/)
140+
})
141+
142+
test('uses `prefix` attribute to customise prefix as empty string', function () {
143+
const now = new Date(Date.now() + 30 * 60 * 60 * 24 * 1000).toISOString()
144+
const time = document.createElement('relative-time')
145+
time.setAttribute('prefix', '')
146+
time.setAttribute('lang', 'en-US')
147+
time.setAttribute('datetime', now)
148+
assert.match(time.textContent, /[A-Z][a-z]{2} \d{1,2}/)
149+
})
150+
133151
test('ignores malformed dates', function () {
134152
const time = document.createElement('relative-time')
135153
time.textContent = 'Jun 30'

0 commit comments

Comments
 (0)