Skip to content

Commit e1de1c7

Browse files
committed
add tests
1 parent 9aedf90 commit e1de1c7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

web_src/js/webcomponents/absolute-date.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import {Temporal} from 'temporal-polyfill';
22

3+
export function toAbsoluteLocaleDate(str, lang, opts) {
4+
const plainDate = Temporal.PlainDate.from(str);
5+
return plainDate.toLocaleString(lang ?? [], opts);
6+
}
7+
38
window.customElements.define('absolute-date', class extends HTMLElement {
49
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];
510

@@ -13,9 +18,8 @@ window.customElements.define('absolute-date', class extends HTMLElement {
1318
'';
1419

1520
// only use the first 10 characters, e.g. the `yyyy-mm-dd` part
16-
const plainDate = Temporal.PlainDate.from(this.getAttribute('date').substring(0, 10));
1721
if (!this.shadowRoot) this.attachShadow({mode: 'open'});
18-
this.shadowRoot.textContent = plainDate.toLocaleString(lang ?? [], {
22+
this.shadowRoot.textContent = toAbsoluteLocaleDate(this.getAttribute('date').substring(0, 10), lang, {
1923
...(year && {year}),
2024
...(month && {month}),
2125
...(weekday && {weekday}),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {toAbsoluteLocaleDate} from './absolute-date.js';
2+
3+
test('toAbsoluteLocaleDate', () => {
4+
expect(toAbsoluteLocaleDate('2024-03-15', 'en-US', {
5+
year: 'numeric',
6+
month: 'long',
7+
day: 'numeric',
8+
})).toEqual('March 15, 2024');
9+
10+
expect(toAbsoluteLocaleDate('2024-03-15', 'de-DE', {
11+
year: 'numeric',
12+
month: 'long',
13+
day: 'numeric',
14+
})).toEqual('15. März 2024');
15+
});

0 commit comments

Comments
 (0)