Explorar o código

LibJS: Implement Intl.Segmenter.prototype.segment

Idan Horowitz %!s(int64=3) %!d(string=hai) anos
pai
achega
9001a8cbe1

+ 1 - 0
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)                           \

+ 16 - 0
Userland/Libraries/LibJS/Runtime/Intl/SegmenterPrototype.cpp

@@ -7,6 +7,7 @@
 #include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Intl/Segmenter.h>
 #include <LibJS/Runtime/Intl/SegmenterPrototype.h>
+#include <LibJS/Runtime/Intl/Segments.h>
 
 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));
+}
+
 }

+ 1 - 0
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);
 };
 

+ 12 - 0
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"))
+        );
+    });
+});