Skip to content

Commit

Permalink
Add simple polyfill for Intl.ListFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsmydoing committed Jan 24, 2024
1 parent f257940 commit 6edaf37
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/impl/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { hasLocaleWeekInfo, hasRelative, padStart, roundTo, validateWeekSettings } from "./util.js";
import {
hasList,
hasLocaleWeekInfo,
hasRelative,
padStart,
roundTo,
validateWeekSettings,
} from "./util.js";
import * as English from "./english.js";
import Settings from "../settings.js";
import DateTime from "../datetime.js";
Expand Down Expand Up @@ -317,6 +324,26 @@ class PolyRelFormatter {
}
}

/**
* @private
*/
class PolyListFormatter {
constructor(intl, opts) {
this.opts = opts;
if (hasList()) {
this.lf = getCachedLF(intl, opts);
}
}

format(list) {
if (this.lf) {
return this.lf.format(list);
} else {
return list.join(", ");

Check warning on line 342 in src/impl/locale.js

View check run for this annotation

Codecov / codecov/patch

src/impl/locale.js#L341-L342

Added lines #L341 - L342 were not covered by tests
}
}
}

const fallbackWeekSettings = {
firstDay: 1,
minimalDays: 4,
Expand Down Expand Up @@ -499,7 +526,7 @@ export default class Locale {
}

listFormatter(opts = {}) {
return getCachedLF(this.intl, opts);
return new PolyListFormatter(this.intl, opts);
}

isEnglish() {
Expand Down
8 changes: 8 additions & 0 deletions src/impl/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export function hasRelative() {
}
}

export function hasList() {
try {
return typeof Intl !== "undefined" && !!Intl.ListFormat;
} catch (e) {
return false;

Check warning on line 51 in src/impl/util.js

View check run for this annotation

Codecov / codecov/patch

src/impl/util.js#L51

Added line #L51 was not covered by tests
}
}

export function hasLocaleWeekInfo() {
try {
return (
Expand Down

0 comments on commit 6edaf37

Please sign in to comment.