1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

LibJS: Implement Intl.Segmenter.prototype.segment

This commit is contained in:
Idan Horowitz 2022-01-30 01:31:59 +02:00 committed by Linus Groh
parent bbacea255f
commit 9001a8cbe1
4 changed files with 30 additions and 0 deletions

View file

@ -389,6 +389,7 @@ namespace JS {
P(seal) \
P(second) \
P(seconds) \
P(segment) \
P(sensitivity) \
P(set) \
P(setBigInt64) \

View file

@ -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));
}
}

View file

@ -20,6 +20,7 @@ public:
virtual ~SegmenterPrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(segment);
JS_DECLARE_NATIVE_FUNCTION(resolved_options);
};

View file

@ -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"))
);
});
});