mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:17:36 +00:00
LibJS: Implement Intl.NumberFormat.prototype.formatRangeToParts
This commit is contained in:
parent
b4a772cde2
commit
e9e187d15c
5 changed files with 236 additions and 0 deletions
|
@ -1862,4 +1862,43 @@ ThrowCompletionOr<String> format_numeric_range(GlobalObject& global_object, Numb
|
|||
return result.build();
|
||||
}
|
||||
|
||||
// 1.1.25 FormatNumericRangeToParts( numberFormat, x, y ), https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#sec-formatnumericrangetoparts
|
||||
ThrowCompletionOr<Array*> format_numeric_range_to_parts(GlobalObject& global_object, NumberFormat& number_format, MathematicalValue start, MathematicalValue end)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// 1. Let parts be ? PartitionNumberRangePattern(numberFormat, x, y).
|
||||
auto parts = TRY(partition_number_range_pattern(global_object, number_format, move(start), move(end)));
|
||||
|
||||
// 2. Let result be ! ArrayCreate(0).
|
||||
auto* result = MUST(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& 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]]).
|
||||
MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
|
||||
|
||||
// c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
|
||||
MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, move(part.value))));
|
||||
|
||||
// d. Perform ! CreateDataPropertyOrThrow(O, "source", part.[[Source]]).
|
||||
MUST(object->create_data_property_or_throw(vm.names.source, js_string(vm, part.source)));
|
||||
|
||||
// e. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
|
||||
MUST(result->create_data_property_or_throw(n, object));
|
||||
|
||||
// f. Increment n by 1.
|
||||
++n;
|
||||
}
|
||||
|
||||
// 5. Return result.
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue