Add micro tense phrasing#355
Conversation
There was a problem hiding this comment.
Pull request overview
Adds “micro” tense phrasing support so format="micro" can render localized narrow relative strings (e.g., “2w ago”, “in 3d”) when tense="past|future", while preserving the existing compact micro output when tense="auto". This also tightens format="micro" behavior around explicit threshold and the user “prefers absolute time” override.
Changes:
- Add a micro-specific relative formatter path using
Intl.RelativeTimeFormat(..., {style: 'narrow'})fortense="past"/tense="future". - Ensure
format="micro"honorsthresholdonly when the attribute is explicitly provided (otherwise preserves legacy behavior). - Add/adjust tests and README docs for micro tense/threshold/user-preference behavior.
Show a summary per file
| File | Description |
|---|---|
src/relative-time-element.ts |
Implements micro tense phrasing via Intl.RelativeTimeFormat when tense is past/future and keeps tense=auto compact output; includes explicit-threshold handling for micro. |
test/relative-time.js |
Adds coverage for micro threshold/date fallback, micro tense=auto compact behavior, localized micro tense phrasing, and absolute-time preference interactions. |
README.md |
Documents the new micro tense behavior and updates the tense behavior table to include format=micro. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 2
2c40f65 to
39c45ff
Compare
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
39c45ff to
9b4a607
Compare
| } | ||
|
|
||
| #getMicroRelativeFormat(duration: Duration): string { | ||
| const relativeFormat = new Intl.RelativeTimeFormat(this.#lang, { |
There was a problem hiding this comment.
This uses Intl.RelativeTimeFormat rather than string-concatenating English ago / in, so the compact tense output still follows lang.
| } else { | ||
| if (format === 'duration') { | ||
| newText = this.#getDurationFormat(duration) | ||
| if (this.format === 'micro' && this.tense !== 'auto' && Intl.RelativeTimeFormat) { |
There was a problem hiding this comment.
The tense phrasing is intentionally limited to micro duration output. Datetime fallback from #354 and user-preferred absolute output bypass this path, so we do not produce strings like Jan 1, 2024 ago.
| assert.equal(time.shadowRoot.textContent, 'hace 3 días') | ||
| }) | ||
|
|
||
| test('micro tense phrasing respects lang attribute', async () => { |
There was a problem hiding this comment.
This is the guard against accidentally making the feature English-only: the micro tense path should localize through Intl.RelativeTimeFormat.
Summary
This builds on #354 by making
format="micro"useful with explicit tense. Instead of forcing consumers to append their ownagosuffix orinprefix, micro output now uses localizedIntl.RelativeTimeFormatnarrow phrasing.Behavior after this PR:
format="micro" tense="past"renders values like2w agoformat="micro" tense="future"renders values likein 3dtense="auto"keeps the existing compact output like2wCloses #353
Tests
npm testnpm run lint