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

LibJS: Implement Intl %SegmentsPrototype%.containing

This commit is contained in:
Idan Horowitz 2022-01-30 21:12:42 +02:00
parent 96af50bbba
commit 366468f1de
4 changed files with 79 additions and 1 deletions

View file

@ -105,6 +105,7 @@ namespace JS {
P(console) \
P(construct) \
P(constructor) \
P(containing) \
P(compare) \
P(copyWithin) \
P(cos) \

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Utf16View.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Intl/SegmentIterator.h>
#include <LibJS/Runtime/Intl/Segments.h>
#include <LibJS/Runtime/Intl/SegmentsPrototype.h>
namespace JS::Intl {
@ -25,6 +25,40 @@ void SegmentsPrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(*vm.well_known_symbol_iterator(), symbol_iterator, 0, attr);
define_native_function(vm.names.containing, containing, 1, attr);
}
// 18.5.2.1 %SegmentsPrototype%.containing ( index ), https://tc39.es/ecma402/#sec-%segmentsprototype%.containing
JS_DEFINE_NATIVE_FUNCTION(SegmentsPrototype::containing)
{
// 1. Let segments be the this value.
// 2. Perform ? RequireInternalSlot(segments, [[SegmentsSegmenter]]).
auto* segments = TRY(typed_this_object(global_object));
// 3. Let segmenter be segments.[[SegmentsSegmenter]].
auto const& segmenter = segments->segments_segmenter();
// 4. Let string be segments.[[SegmentsString]].
auto string = segments->segments_string();
// 5. Let len be the length of string.
auto length = string.length_in_code_units();
// 6. Let n be ? ToIntegerOrInfinity(index).
auto n = TRY(vm.argument(0).to_integer_or_infinity(global_object));
// 7. If n < 0 or n ≥ len, return undefined.
if (n < 0 || n >= length)
return js_undefined();
// 8. Let startIndex be ! FindBoundary(segmenter, string, n, before).
auto start_index = find_boundary(segmenter, string, n, Direction::Before, segments->boundaries_cache());
// 9. Let endIndex be ! FindBoundary(segmenter, string, n, after).
auto end_index = find_boundary(segmenter, string, n, Direction::After, segments->boundaries_cache());
// 10. Return ! CreateSegmentDataObject(segmenter, string, startIndex, endIndex).
return create_segment_data_object(global_object, segmenter, string, start_index, end_index);
}
// 18.5.2.2 %SegmentsPrototype% [ @@iterator ] ( ), https://tc39.es/ecma402/#sec-%segmentsprototype%-@@iterator

View file

@ -20,6 +20,7 @@ public:
virtual ~SegmentsPrototype() override = default;
private:
JS_DECLARE_NATIVE_FUNCTION(containing);
JS_DECLARE_NATIVE_FUNCTION(symbol_iterator);
};

View file

@ -10,6 +10,48 @@ describe("correct behavior", () => {
);
});
test("returns segments object with valid containing method", () => {
const string = "hello friends!";
const graphemeSegmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
const graphemeSegments = graphemeSegmenter.segment(string);
const graphemeSegment0 = graphemeSegments.containing(0);
expect(graphemeSegment0.segment).toBe("h");
expect(graphemeSegment0.index).toBe(0);
expect(graphemeSegment0.input).toBe(string);
expect(graphemeSegment0.isWordLike).toBeUndefined();
const graphemeSegment5 = graphemeSegments.containing(5);
expect(graphemeSegment5.segment).toBe(" ");
expect(graphemeSegment5.index).toBe(5);
expect(graphemeSegment5.input).toBe(string);
expect(graphemeSegment5.isWordLike).toBeUndefined();
const wordSegmenter = new Intl.Segmenter("en", { granularity: "word" });
const wordSegments = wordSegmenter.segment(string);
const wordSegment0 = wordSegments.containing(0);
expect(wordSegment0.segment).toBe("hello");
expect(wordSegment0.index).toBe(0);
expect(wordSegment0.input).toBe(string);
// FIXME: expect(wordSegment0.isWordLike).toBeTrue();
const wordSegment5 = wordSegments.containing(5);
expect(wordSegment5.segment).toBe(" ");
expect(wordSegment5.index).toBe(5);
expect(wordSegment5.input).toBe(string);
expect(wordSegment5.isWordLike).toBeFalse();
const sentenceSegmenter = new Intl.Segmenter("en", { granularity: "sentence" });
const sentenceSegments = sentenceSegmenter.segment(string);
const sentenceSegment0 = sentenceSegments.containing(0);
expect(sentenceSegment0.segment).toBe(string);
expect(sentenceSegment0.index).toBe(0);
expect(sentenceSegment0.input).toBe(string);
expect(sentenceSegment0.isWordLike).toBeUndefined();
const sentenceSegment5 = sentenceSegments.containing(5);
expect(sentenceSegment5.segment).toBe(sentenceSegment0.segment);
expect(sentenceSegment5.index).toBe(sentenceSegment0.index);
expect(sentenceSegment5.input).toBe(sentenceSegment0.input);
expect(sentenceSegment5.isWordLike).toBe(sentenceSegment0.isWordLike);
});
test("returns segments object segment iterator", () => {
const segmenter = new Intl.Segmenter();
const segments = segmenter.segment("hello friends!");