From bf5d96648bf2265a1e65d1708f4c13d6bb8a0b5c Mon Sep 17 00:00:00 2001 From: Marie Lucca Date: Wed, 20 Aug 2025 12:31:46 -0400 Subject: [PATCH] chore: fix month microformat --- src/duration-format-ponyfill.ts | 6 +++++- test/relative-time.js | 23 +++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/duration-format-ponyfill.ts b/src/duration-format-ponyfill.ts index 15ccd7d..685dd57 100644 --- a/src/duration-format-ponyfill.ts +++ b/src/duration-format-ponyfill.ts @@ -110,7 +110,11 @@ export default class DurationFormat { : unitStyle === 'numeric' ? {} : {style: 'unit', unit: nfUnit, unitDisplay: unitStyle} - list.push(new Intl.NumberFormat(locale, nfOpts).format(value)) + list.push( + nfUnit === 'month' && unitStyle === 'narrow' + ? `${value}mo` + : new Intl.NumberFormat(locale, nfOpts).format(value), + ) } return new ListFormat(locale, { type: 'unit', diff --git a/test/relative-time.js b/test/relative-time.js index de5c102..f209cdc 100644 --- a/test/relative-time.js +++ b/test/relative-time.js @@ -532,6 +532,17 @@ suite('relative-time', function () { assert.equal(time.shadowRoot.textContent, '10y') }) + test('micro formats months', async () => { + const datetime = new Date() + datetime.setMonth(datetime.getMonth() - 2) + const time = document.createElement('relative-time') + time.setAttribute('tense', 'past') + time.setAttribute('datetime', datetime) + time.setAttribute('format', 'micro') + await Promise.resolve() + assert.equal(time.shadowRoot.textContent, '2mo') + }) + test('micro formats future times', async () => { const now = new Date(Date.now() + 3 * 1000).toISOString() const time = document.createElement('relative-time') @@ -1941,7 +1952,7 @@ suite('relative-time', function () { datetime: '2022-09-24T14:46:00.000Z', tense: 'future', format: 'micro', - expected: '1m', + expected: '1mo', }, { datetime: '2022-10-23T14:46:00.000Z', @@ -1991,7 +2002,7 @@ suite('relative-time', function () { datetime: '2022-11-24T14:46:00.000Z', tense: 'future', format: 'micro', - expected: '1m', + expected: '1mo', }, { datetime: '2023-10-23T14:46:00.000Z', @@ -2023,7 +2034,7 @@ suite('relative-time', function () { datetime: '2022-11-24T14:46:00.000Z', tense: 'past', format: 'micro', - expected: '1m', + expected: '1mo', }, { datetime: '2022-10-25T14:46:00.000Z', @@ -2073,7 +2084,7 @@ suite('relative-time', function () { datetime: '2022-09-23T14:46:00.000Z', tense: 'past', format: 'micro', - expected: '1m', + expected: '1mo', }, { datetime: '2021-10-25T14:46:00.000Z', @@ -2178,13 +2189,13 @@ suite('relative-time', function () { { datetime: '2021-10-30T14:46:00.000Z', format: 'elapsed', - expected: '11m 29d', + expected: '11mo 29d', }, { datetime: '2021-10-30T14:46:00.000Z', format: 'elapsed', precision: 'month', - expected: '11m', + expected: '11mo', }, { datetime: '2021-10-29T14:46:00.000Z',