diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-08-24 23:26:44 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-26 22:04:09 +0100 |
commit | a061d874c90d30b4c88234b1de6511a9a016b1db (patch) | |
tree | bc71ef1f5b27a5f5616bdf28ef79a441ba1fb8ce /Userland/Libraries | |
parent | 17bb65277507eab5bcfb8257d145d4164257d584 (diff) | |
download | serenity-a061d874c90d30b4c88234b1de6511a9a016b1db.zip |
LibJS: Implement Intl.DisplayNames.prototype.resolvedOptions
Diffstat (limited to 'Userland/Libraries')
4 files changed, 61 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 91084eaca6..c70477df7d 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -263,6 +263,7 @@ namespace JS { P(length) \ P(link) \ P(load) \ + P(locale) \ P(localeCompare) \ P(localeMatcher) \ P(log) \ @@ -323,6 +324,7 @@ namespace JS { P(reject) \ P(repeat) \ P(resolve) \ + P(resolvedOptions) \ P(reverse) \ P(revocable) \ P(revoke) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp index 43ba4404a1..397dcd41cb 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.cpp @@ -46,6 +46,7 @@ void DisplayNamesPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.of, of, 1, attr); + define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr); } // 12.4.3 Intl.DisplayNames.prototype.of ( code ), https://tc39.es/ecma402/#sec-Intl.DisplayNames.prototype.of @@ -99,4 +100,30 @@ JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::of) return js_undefined(); } +// 12.4.4 Intl.DisplayNames.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-Intl.DisplayNames.prototype.resolvedOptions +JS_DEFINE_NATIVE_FUNCTION(DisplayNamesPrototype::resolved_options) +{ + // 1. Let displayNames be this value. + // 2. Perform ? RequireInternalSlot(displayNames, [[InitializedDisplayNames]]). + auto* display_names = typed_this(global_object); + if (!display_names) + return {}; + + // 3. Let options be ! OrdinaryObjectCreate(%Object.prototype%). + auto* options = Object::create(global_object, global_object.object_prototype()); + + // 4. For each row of Table 8, except the header row, in table order, do + // a. Let p be the Property value of the current row. + // b. Let v be the value of displayNames's internal slot whose name is the Internal Slot value of the current row. + // c. Assert: v is not undefined. + // d. Perform ! CreateDataPropertyOrThrow(options, p, v). + options->create_data_property_or_throw(vm.names.locale, js_string(vm, display_names->locale())); + options->create_data_property_or_throw(vm.names.style, js_string(vm, display_names->style_string())); + options->create_data_property_or_throw(vm.names.type, js_string(vm, display_names->type_string())); + options->create_data_property_or_throw(vm.names.fallback, js_string(vm, display_names->fallback_string())); + + // 5. Return options. + return options; +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.h index 76ae2f86b1..38cc6a7c6a 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DisplayNamesPrototype.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(of); + JS_DECLARE_NATIVE_FUNCTION(resolved_options); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js new file mode 100644 index 0000000000..fac851778f --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DisplayNames/DisplayNames.prototype.resolvedOptions.js @@ -0,0 +1,31 @@ +describe("correct behavior", () => { + test("length is 0", () => { + expect(Intl.DisplayNames.prototype.resolvedOptions).toHaveLength(0); + }); + + test("all valid types", () => { + const en = new Intl.DisplayNames("en", { type: "region" }); + expect(en.resolvedOptions()).toEqual({ + locale: "en", + style: "long", + type: "region", + fallback: "code", + }); + + const es419 = new Intl.DisplayNames("es-419", { type: "script", fallback: "none" }); + expect(es419.resolvedOptions()).toEqual({ + locale: "es-419", + style: "long", + type: "script", + fallback: "none", + }); + + const zhHant = new Intl.DisplayNames(["zh-Hant"], { type: "language", style: "short" }); + expect(zhHant.resolvedOptions()).toEqual({ + locale: "zh-Hant", + style: "short", + type: "language", + fallback: "code", + }); + }); +}); |