LibJS: Add [[LanguageDisplay]] to Intl.DisplayNames's resolvedOptions

This commit is contained in:
Timothy Flynn 2022-01-12 14:18:22 -05:00 committed by Linus Groh
parent 27c845eef2
commit 1a3e6e8a7b
2 changed files with 56 additions and 0 deletions

View file

@ -116,6 +116,10 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::resolved_options)
MUST(options->create_data_property_or_throw(vm.names.type, js_string(vm, display_names->type_string())));
MUST(options->create_data_property_or_throw(vm.names.fallback, js_string(vm, display_names->fallback_string())));
// NOTE: Step 4c indicates languageDisplay must not be undefined, but it is only set when the type option is language.
if (display_names->has_language_display())
MUST(options->create_data_property_or_throw(vm.names.languageDisplay, js_string(vm, display_names->language_display_string())));
// 5. Return options.
return options;
}

View file

@ -26,6 +26,58 @@ describe("correct behavior", () => {
style: "short",
type: "language",
fallback: "code",
languageDisplay: "dialect",
});
const ar = new Intl.DisplayNames("ar", { type: "calendar" });
expect(ar.resolvedOptions()).toEqual({
locale: "ar",
style: "long",
type: "calendar",
fallback: "code",
});
const fr = new Intl.DisplayNames("fr", { type: "dateTimeField" });
expect(fr.resolvedOptions()).toEqual({
locale: "fr",
style: "long",
type: "dateTimeField",
fallback: "code",
});
});
test("all valid language displays", () => {
const en = new Intl.DisplayNames("en", { type: "language" });
expect(en.resolvedOptions()).toEqual({
locale: "en",
style: "long",
type: "language",
fallback: "code",
languageDisplay: "dialect",
});
const es419 = new Intl.DisplayNames("es-419", {
type: "language",
languageDisplay: "dialect",
});
expect(es419.resolvedOptions()).toEqual({
locale: "es-419",
style: "long",
type: "language",
fallback: "code",
languageDisplay: "dialect",
});
const zhHant = new Intl.DisplayNames(["zh-Hant"], {
type: "language",
languageDisplay: "standard",
});
expect(zhHant.resolvedOptions()).toEqual({
locale: "zh-Hant",
style: "long",
type: "language",
fallback: "code",
languageDisplay: "standard",
});
});