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 Jun 6, 2024
1 parent 2008075 commit 09a9314
Show file tree
Hide file tree
Showing 4 changed files with 65 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(", ");
}
}
}

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;
}
}

export function hasLocaleWeekInfo() {
try {
return (
Expand Down
14 changes: 14 additions & 0 deletions test/duration/format.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global test expect */
import { Duration } from "../../src/luxon";

const Helpers = require("../helpers");

const dur = () =>
Duration.fromObject({
years: 1,
Expand Down Expand Up @@ -314,3 +316,15 @@ test("Duration#toHuman works in differt languages", () => {
"1 an, 2 mois, 1 semaine, 3 jours, 4 heures, 5 minutes, 6 secondes, 7 millisecondes"
);
});

Helpers.withoutLF("Duration#toHuman works without LF", () => {
expect(dur().toHuman()).toEqual(
"1 year, 2 months, 1 week, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds"
);
});

Helpers.withoutLF("Duration#toHuman falls back to commas", () => {
expect(dur().reconfigure({ locale: "ja" }).toHuman()).toEqual(
"1 年, 2 か月, 1 週間, 3 日, 4 時間, 5 分, 6 秒, 7 ミリ秒"
);
});
14 changes: 14 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ exports.withoutRTF = function (name, f) {
});
};

exports.withoutLF = function (name, f) {
const fullName = `With no ListFormat support, ${name}`;
test(fullName, () => {
const rtf = Intl.ListFormat;
try {
Intl.ListFormat = undefined;
Settings.resetCaches();
f();
} finally {
Intl.ListFormat = rtf;
}
});
};

exports.withoutLocaleWeekInfo = function (name, f) {
const fullName = `With no Intl.Locale.weekInfo support, ${name}`;
test(fullName, () => {
Expand Down

0 comments on commit 09a9314

Please sign in to comment.