Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Mar 15, 2024
1 parent 9aedf90 commit e1de1c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions web_src/js/webcomponents/absolute-date.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {Temporal} from 'temporal-polyfill';

export function toAbsoluteLocaleDate(str, lang, opts) {
const plainDate = Temporal.PlainDate.from(str);
return plainDate.toLocaleString(lang ?? [], opts);
}

window.customElements.define('absolute-date', class extends HTMLElement {
static observedAttributes = ['date', 'year', 'month', 'weekday', 'day'];

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

// only use the first 10 characters, e.g. the `yyyy-mm-dd` part
const plainDate = Temporal.PlainDate.from(this.getAttribute('date').substring(0, 10));
if (!this.shadowRoot) this.attachShadow({mode: 'open'});
this.shadowRoot.textContent = plainDate.toLocaleString(lang ?? [], {
this.shadowRoot.textContent = toAbsoluteLocaleDate(this.getAttribute('date').substring(0, 10), lang, {
...(year && {year}),
...(month && {month}),
...(weekday && {weekday}),
Expand Down
15 changes: 15 additions & 0 deletions web_src/js/webcomponents/absolute-date.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {toAbsoluteLocaleDate} from './absolute-date.js';

test('toAbsoluteLocaleDate', () => {
expect(toAbsoluteLocaleDate('2024-03-15', 'en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
})).toEqual('March 15, 2024');

expect(toAbsoluteLocaleDate('2024-03-15', 'de-DE', {
year: 'numeric',
month: 'long',
day: 'numeric',
})).toEqual('15. März 2024');
});

0 comments on commit e1de1c7

Please sign in to comment.