Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple polyfill for Intl.ListFormat #1585

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ミリ秒"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked Japanese since it uses a different comma style

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diesieben07 Hey sorry for the tag, just checking if u had a look at this

);
});
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 lf = Intl.ListFormat;
try {
Intl.ListFormat = undefined;
Settings.resetCaches();
f();
} finally {
Intl.ListFormat = lf;
}
});
};

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