mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:27:44 +00:00
LibJS: Convert to_seconds_string_precision() to ThrowCompletionOr
This commit is contained in:
parent
f8f074f8a9
commit
dd483d84f8
4 changed files with 11 additions and 15 deletions
|
@ -286,14 +286,14 @@ ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject& global_objec
|
|||
}
|
||||
|
||||
// 13.16 ToSecondsStringPrecision ( normalizedOptions ), https://tc39.es/proposal-temporal/#sec-temporal-tosecondsstringprecision
|
||||
Optional<SecondsStringPrecision> to_seconds_string_precision(GlobalObject& global_object, Object const& normalized_options)
|
||||
ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject& global_object, Object const& normalized_options)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
// Let smallestUnit be ? ToSmallestTemporalUnit(normalizedOptions, « "year", "month", "week", "day", "hour" », undefined).
|
||||
auto smallest_unit = to_smallest_temporal_unit(global_object, normalized_options, { "year"sv, "month"sv, "week"sv, "day"sv, "hour"sv }, {});
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
|
||||
// 2. If smallestUnit is "minute", then
|
||||
if (smallest_unit == "minute"sv) {
|
||||
|
@ -329,7 +329,7 @@ Optional<SecondsStringPrecision> to_seconds_string_precision(GlobalObject& globa
|
|||
VERIFY(!smallest_unit.has_value());
|
||||
|
||||
// 8. Let digits be ? GetStringOrNumberOption(normalizedOptions, "fractionalSecondDigits", « "auto" », 0, 9, "auto").
|
||||
auto digits_variant = TRY_OR_DISCARD(get_string_or_number_option<u8>(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv)));
|
||||
auto digits_variant = TRY(get_string_or_number_option<u8>(global_object, normalized_options, vm.names.fractionalSecondDigits, { "auto"sv }, 0, 9, js_string(vm, "auto"sv)));
|
||||
|
||||
// 9. If digits is "auto", then
|
||||
if (digits_variant.has<String>()) {
|
||||
|
|
|
@ -93,7 +93,7 @@ ThrowCompletionOr<String> to_temporal_overflow(GlobalObject&, Object const& norm
|
|||
ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const& normalized_options, String const& fallback);
|
||||
ThrowCompletionOr<String> to_show_calendar_option(GlobalObject&, Object const& normalized_options);
|
||||
ThrowCompletionOr<u64> to_temporal_rounding_increment(GlobalObject&, Object const& normalized_options, Optional<double> dividend, bool inclusive);
|
||||
Optional<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options);
|
||||
ThrowCompletionOr<SecondsStringPrecision> to_seconds_string_precision(GlobalObject&, Object const& normalized_options);
|
||||
Optional<String> to_largest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, String const& fallback, Optional<String> auto_value);
|
||||
Optional<String> to_smallest_temporal_unit(GlobalObject&, Object const& normalized_options, Vector<StringView> const& disallowed_units, Optional<String> fallback);
|
||||
void validate_temporal_unit_range(GlobalObject&, StringView largest_unit, StringView smallest_unit);
|
||||
|
|
|
@ -410,15 +410,13 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_string)
|
|||
}
|
||||
|
||||
// 6. Let precision be ? ToSecondsStringPrecision(options).
|
||||
auto precision = to_seconds_string_precision(global_object, *options);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto precision = TRY_OR_DISCARD(to_seconds_string_precision(global_object, *options));
|
||||
|
||||
// 7. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
|
||||
auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
||||
|
||||
// 8. Let roundedNs be ? RoundTemporalInstant(instant.[[Nanoseconds]], precision.[[Increment]], precision.[[Unit]], roundingMode).
|
||||
auto* rounded_ns = round_temporal_instant(global_object, instant->nanoseconds(), precision->increment, precision->unit, rounding_mode);
|
||||
auto* rounded_ns = round_temporal_instant(global_object, instant->nanoseconds(), precision.increment, precision.unit, rounding_mode);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
|
@ -426,7 +424,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_string)
|
|||
auto* rounded_instant = create_temporal_instant(global_object, *rounded_ns);
|
||||
|
||||
// 10. Return ? TemporalInstantToString(roundedInstant, timeZone, precision.[[Precision]]).
|
||||
auto string = temporal_instant_to_string(global_object, *rounded_instant, time_zone, precision->precision);
|
||||
auto string = temporal_instant_to_string(global_object, *rounded_instant, time_zone, precision.precision);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
|
|
|
@ -348,18 +348,16 @@ JS_DEFINE_NATIVE_FUNCTION(PlainTimePrototype::to_string)
|
|||
auto* options = TRY_OR_DISCARD(get_options_object(global_object, vm.argument(0)));
|
||||
|
||||
// 4. Let precision be ? ToSecondsStringPrecision(options).
|
||||
auto precision = to_seconds_string_precision(global_object, *options);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto precision = TRY_OR_DISCARD(to_seconds_string_precision(global_object, *options));
|
||||
|
||||
// 5. Let roundingMode be ? ToTemporalRoundingMode(options, "trunc").
|
||||
auto rounding_mode = TRY_OR_DISCARD(to_temporal_rounding_mode(global_object, *options, "trunc"sv));
|
||||
|
||||
// 6. Let roundResult be ! RoundTime(temporalTime.[[ISOHour]], temporalTime.[[ISOMinute]], temporalTime.[[ISOSecond]], temporalTime.[[ISOMillisecond]], temporalTime.[[ISOMicrosecond]], temporalTime.[[ISONanosecond]], precision.[[Increment]], precision.[[Unit]], roundingMode).
|
||||
auto round_result = round_time(global_object, temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), precision->increment, precision->unit, rounding_mode);
|
||||
auto round_result = round_time(global_object, temporal_time->iso_hour(), temporal_time->iso_minute(), temporal_time->iso_second(), temporal_time->iso_millisecond(), temporal_time->iso_microsecond(), temporal_time->iso_nanosecond(), precision.increment, precision.unit, rounding_mode);
|
||||
|
||||
// 7. Return ! TemporalTimeToString(roundResult.[[Hour]], roundResult.[[Minute]], roundResult.[[Second]], roundResult.[[Millisecond]], roundResult.[[Microsecond]], roundResult.[[Nanosecond]], precision.[[Precision]]).
|
||||
auto string = temporal_time_to_string(round_result.hour, round_result.minute, round_result.second, round_result.millisecond, round_result.microsecond, round_result.nanosecond, precision->precision);
|
||||
auto string = temporal_time_to_string(round_result.hour, round_result.minute, round_result.second, round_result.millisecond, round_result.microsecond, round_result.nanosecond, precision.precision);
|
||||
return js_string(vm, move(string));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue