diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 88a7e0bfae..8accef01f5 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -389,6 +389,7 @@ namespace JS { P(seal) \ P(second) \ P(seconds) \ + P(segment) \ P(sensitivity) \ P(set) \ P(setBigInt64) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp index 6f06f03d99..6b1d3ffcae 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace JS::Intl { @@ -27,6 +28,7 @@ void SegmenterPrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.resolvedOptions, resolved_options, 0, attr); + define_native_function(vm.names.segment, segment, 1, attr); } // 18.3.4 Intl.Segmenter.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.segmenter.prototype.resolvedoptions @@ -51,4 +53,18 @@ JS_DEFINE_NATIVE_FUNCTION(SegmenterPrototype::resolved_options) return options; } +// 18.3.3 Intl.Segmenter.prototype.segment ( string ), https://tc39.es/ecma402/#sec-intl.segmenter.prototype.segment +JS_DEFINE_NATIVE_FUNCTION(SegmenterPrototype::segment) +{ + // 1. Let segmenter be the this value. + // 2. Perform ? RequireInternalSlot(segmenter, [[InitializedSegmenter]]). + auto* segmenter = TRY(typed_this_object(global_object)); + + // 3. Let string be ? ToString(string). + auto string = TRY(vm.argument(0).to_string(global_object)); + + // 4. Return ! CreateSegmentsObject(segmenter, string). + return Segments::create(global_object, *segmenter, move(string)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.h index f57084596e..3898bb4520 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.h @@ -20,6 +20,7 @@ public: virtual ~SegmenterPrototype() override = default; private: + JS_DECLARE_NATIVE_FUNCTION(segment); JS_DECLARE_NATIVE_FUNCTION(resolved_options); }; diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js new file mode 100644 index 0000000000..9654df59f2 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.prototype.segment.js @@ -0,0 +1,12 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Intl.Segmenter.prototype.segment).toHaveLength(1); + }); + + test("returns segments object with shared segments prototype", () => { + const segmenter = new Intl.Segmenter(); + expect(Object.getPrototypeOf(segmenter.segment("hello"))).toBe( + Object.getPrototypeOf(segmenter.segment("friends")) + ); + }); +});