mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 21:17:45 +00:00
LibJS: Implement Intl.ListFormat.prototype.formatToParts
This commit is contained in:
parent
cdba40f7ea
commit
5c06a91dfa
4 changed files with 291 additions and 0 deletions
|
@ -168,6 +168,7 @@ namespace JS {
|
|||
P(fontsize) \
|
||||
P(forEach) \
|
||||
P(format) \
|
||||
P(formatToParts) \
|
||||
P(fractionalSecondDigits) \
|
||||
P(freeze) \
|
||||
P(from) \
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/TypeCasts.h>
|
||||
#include <AK/Variant.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Intl/ListFormat.h>
|
||||
|
@ -191,6 +192,42 @@ static String format_list(ListFormat const& list_format, Vector<String> const& l
|
|||
return result.build();
|
||||
}
|
||||
|
||||
// 13.1.4 FormatListToParts ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlisttoparts
|
||||
static Array* format_list_to_parts(GlobalObject& global_object, ListFormat const& list_format, Vector<String> const& list)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let parts be CreatePartsFromList(listFormat, list).
|
||||
auto parts = create_parts_from_list(list_format, list);
|
||||
|
||||
// 2. Let result be ArrayCreate(0).
|
||||
auto result = Array::create(global_object, 0);
|
||||
|
||||
// 3. Let n be 0.
|
||||
size_t n = 0;
|
||||
|
||||
// 4. For each Record { [[Type]], [[Value]] } part in parts, do
|
||||
for (auto const& part : parts) {
|
||||
// a. Let O be OrdinaryObjectCreate(%Object.prototype%).
|
||||
auto* object = Object::create(global_object, global_object.object_prototype());
|
||||
|
||||
// b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
|
||||
object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type));
|
||||
|
||||
// c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
|
||||
object->create_data_property_or_throw(vm.names.value, js_string(vm, part.value));
|
||||
|
||||
// d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
|
||||
result->create_data_property_or_throw(n, object);
|
||||
|
||||
// e. Increment n by 1.
|
||||
++n;
|
||||
}
|
||||
|
||||
// 5. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
// 13.1.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable
|
||||
static Vector<String> string_list_from_iterable(GlobalObject& global_object, Value iterable)
|
||||
{
|
||||
|
@ -263,6 +300,7 @@ void ListFormatPrototype::initialize(GlobalObject& global_object)
|
|||
|
||||
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||
define_native_function(vm.names.format, format, 1, attr);
|
||||
define_native_function(vm.names.formatToParts, format_to_parts, 1, attr);
|
||||
}
|
||||
|
||||
// 13.4.3 Intl.ListFormat.prototype.format ( list ), https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.format
|
||||
|
@ -286,4 +324,24 @@ JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format)
|
|||
return js_string(vm, move(formatted));
|
||||
}
|
||||
|
||||
// 13.4.4 Intl.ListFormat.prototype.formatToParts ( list ), https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype.formatToParts
|
||||
JS_DEFINE_NATIVE_FUNCTION(ListFormatPrototype::format_to_parts)
|
||||
{
|
||||
auto list = vm.argument(0);
|
||||
|
||||
// 1. Let lf be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(lf, [[InitializedListFormat]]).
|
||||
auto* list_format = typed_this(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 3. Let stringList be ? StringListFromIterable(list).
|
||||
auto string_list = string_list_from_iterable(global_object, list);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
// 4. Return FormatListToParts(lf, stringList).
|
||||
return format_list_to_parts(global_object, *list_format, string_list);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ public:
|
|||
|
||||
private:
|
||||
JS_DECLARE_NATIVE_FUNCTION(format);
|
||||
JS_DECLARE_NATIVE_FUNCTION(format_to_parts);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue