Skip to content

Commit

Permalink
Merge pull request #183 from github/extract-tostring-from-relative-ti…
Browse files Browse the repository at this point in the history
…me-into-element

Extract tostring from relative time into element
  • Loading branch information
keithamus authored Oct 21, 2022
2 parents cd96bc1 + 9022fc8 commit 34346d1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
42 changes: 38 additions & 4 deletions src/relative-time-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@ import ExtendedTimeElement from './extended-time-element.js'
import {localeFromElement} from './utils.js'

export default class RelativeTimeElement extends ExtendedTimeElement {
static get observedAttributes() {
return [...ExtendedTimeElement.observedAttributes, 'prefix']
}

getFormattedDate(): string | undefined {
const date = this.date
if (!date) return
return new RelativeTime(date, localeFromElement(this)).toString({
tense: this.tense,
format: this.format
})
const relativeTime = new RelativeTime(date, localeFromElement(this))
const format = this.format
const tense = this.tense
const micro = format === 'micro'
if (tense === 'past') {
return micro ? relativeTime.microTimeAgo() : relativeTime.timeAgo()
}
if (tense === 'future') {
return micro ? relativeTime.microTimeUntil() : relativeTime.timeUntil()
}
if (format === 'auto') {
const ago = micro ? relativeTime.microTimeAgo() : relativeTime.timeElapsed()
if (ago) {
return ago
}
const ahead = micro ? relativeTime.microTimeUntil() : relativeTime.timeAhead()
if (ahead) {
return ahead
}
}
if (format !== 'auto' && format !== 'micro') {
return relativeTime.formatDate(format)
}
return `${this.prefix ? `${this.prefix} ` : ''}${relativeTime.formatDate()}`
}

/** @deprecated */
get prefix(): string {
return this.getAttribute('prefix') ?? 'on'
}

/** @deprecated */
set prefix(value: string) {
this.setAttribute('prefix', value)
}

get tense(): Tense {
Expand Down
24 changes: 0 additions & 24 deletions src/relative-time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,6 @@ export default class RelativeTime {
this.locale = locale
}

toString({format = 'auto', tense = 'auto'}: {format?: Format; tense?: Tense} = {}): string | undefined {
const micro = format === 'micro'
if (tense === 'past') {
return micro ? this.microTimeAgo() : this.timeAgo()
}
if (tense === 'future') {
return micro ? this.microTimeUntil() : this.timeUntil()
}
if (format === 'auto') {
const ago = this.timeElapsed()
if (ago) {
return ago
}
const ahead = this.timeAhead()
if (ahead) {
return ahead
}
}
if (format !== 'auto' && format !== 'micro') {
return this.formatDate(format)
}
return `on ${this.formatDate()}`
}

timeElapsed(): string | undefined | null {
const ms = new Date().getTime() - this.date.getTime()
const sec = Math.round(ms / 1000)
Expand Down
18 changes: 18 additions & 0 deletions test/relative-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ suite('relative-time', function () {
assert.match(time.textContent, /on [A-Z][a-z]{2} \d{1,2}/)
})

test('uses `prefix` attribute to customise prefix', function () {
const now = new Date(Date.now() + 30 * 60 * 60 * 24 * 1000).toISOString()
const time = document.createElement('relative-time')
time.setAttribute('prefix', 'will happen by')
time.setAttribute('lang', 'en-US')
time.setAttribute('datetime', now)
assert.match(time.textContent, /will happen by [A-Z][a-z]{2} \d{1,2}/)
})

test('uses `prefix` attribute to customise prefix as empty string', function () {
const now = new Date(Date.now() + 30 * 60 * 60 * 24 * 1000).toISOString()
const time = document.createElement('relative-time')
time.setAttribute('prefix', '')
time.setAttribute('lang', 'en-US')
time.setAttribute('datetime', now)
assert.match(time.textContent, /[A-Z][a-z]{2} \d{1,2}/)
})

test('ignores malformed dates', function () {
const time = document.createElement('relative-time')
time.textContent = 'Jun 30'
Expand Down

0 comments on commit 34346d1

Please sign in to comment.