From bdf36575c805efbf995fb233f6ea68deff6ba66a Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 2 Sep 2021 10:50:51 -0400 Subject: [PATCH] LibJS: Implement Intl.Locale.prototype.numeric --- .../LibJS/Runtime/Intl/LocalePrototype.cpp | 14 ++++++++++++++ .../LibJS/Runtime/Intl/LocalePrototype.h | 1 + .../Intl/Locale/Locale.prototype.numeric.js | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.numeric.js diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index a8e11e443f..fa82a5841d 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -52,6 +52,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.collation, collation, {}, Attribute::Configurable); define_native_accessor(vm.names.hourCycle, hour_cycle, {}, Attribute::Configurable); define_native_accessor(vm.names.numberingSystem, numbering_system, {}, Attribute::Configurable); + define_native_accessor(vm.names.numeric, numeric, {}, Attribute::Configurable); } // 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString @@ -109,4 +110,17 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::base_name) JS_ENUMERATE_LOCALE_KEYWORD_PROPERTIES #undef __JS_ENUMERATE +// 14.3.11 get Intl.Locale.prototype.numeric, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.numeric +JS_DEFINE_NATIVE_GETTER(LocalePrototype::numeric) +{ + // 1. Let loc be the this value. + // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). + auto* locale_object = typed_this(global_object); + if (!locale_object) + return {}; + + // 3. Return loc.[[Numeric]]. + return Value(locale_object->numeric()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index 02880cb91d..bdd557c569 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -27,6 +27,7 @@ private: JS_DECLARE_NATIVE_GETTER(collation); JS_DECLARE_NATIVE_GETTER(hour_cycle); JS_DECLARE_NATIVE_GETTER(numbering_system); + JS_DECLARE_NATIVE_GETTER(numeric); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.numeric.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.numeric.js new file mode 100644 index 0000000000..3dcab9f41b --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.numeric.js @@ -0,0 +1,16 @@ +describe("errors", () => { + test("called on non-Locale object", () => { + expect(() => { + Intl.Locale.prototype.numeric; + }).toThrowWithMessage(TypeError, "Not a Intl.Locale object"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + expect(new Intl.Locale("en").numeric).toBeFalse(); + expect(new Intl.Locale("en-u-kn-true").numeric).toBeTrue(); + expect(new Intl.Locale("en", { numeric: false }).numeric).toBeFalse(); + expect(new Intl.Locale("en-u-kn-false", { numeric: true }).numeric).toBeTrue(); + }); +});