mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:47:37 +00:00
LibJS: Convert to_largest_temporal_unit() to ThrowCompletionOr
This commit is contained in:
parent
dd483d84f8
commit
92187591dd
3 changed files with 11 additions and 16 deletions
|
@ -380,7 +380,7 @@ static HashMap<StringView, StringView> plural_to_singular_units = {
|
|||
};
|
||||
|
||||
// 13.17 ToLargestTemporalUnit ( normalizedOptions, disallowedUnits, fallback [ , autoValue ] ), https://tc39.es/proposal-temporal/#sec-temporal-tolargesttemporalunit
|
||||
Optional<String> to_largest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector<StringView> const& disallowed_units, String const& fallback, Optional<String> auto_value)
|
||||
ThrowCompletionOr<String> to_largest_temporal_unit(GlobalObject& global_object, Object const& normalized_options, Vector<StringView> const& disallowed_units, String const& fallback, Optional<String> auto_value)
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
|
@ -391,13 +391,13 @@ Optional<String> to_largest_temporal_unit(GlobalObject& global_object, Object co
|
|||
// 4. Assert: autoValue is not present or disallowedUnits does not contain autoValue.
|
||||
|
||||
// 5. Let largestUnit be ? GetOption(normalizedOptions, "largestUnit", « String », « "auto", "year", "years", "month", "months", "week", "weeks", "day", "days", "hour", "hours", "minute", "minutes", "second", "seconds", "millisecond", "milliseconds", "microsecond", "microseconds", "nanosecond", "nanoseconds" », fallback).
|
||||
auto largest_unit_value = TRY_OR_DISCARD(get_option(global_object, normalized_options, vm.names.largestUnit, { OptionType::String }, { "auto"sv, "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, js_string(vm, fallback)));
|
||||
auto largest_unit_value = TRY(get_option(global_object, normalized_options, vm.names.largestUnit, { OptionType::String }, { "auto"sv, "year"sv, "years"sv, "month"sv, "months"sv, "week"sv, "weeks"sv, "day"sv, "days"sv, "hour"sv, "hours"sv, "minute"sv, "minutes"sv, "second"sv, "seconds"sv, "millisecond"sv, "milliseconds"sv, "microsecond"sv, "microseconds"sv, "nanosecond"sv, "nanoseconds"sv }, js_string(vm, fallback)));
|
||||
auto largest_unit = largest_unit_value.as_string().string();
|
||||
|
||||
// 6. If largestUnit is "auto" and autoValue is present, then
|
||||
if (largest_unit == "auto"sv && auto_value.has_value()) {
|
||||
// a. Return autoValue.
|
||||
return auto_value;
|
||||
return *auto_value;
|
||||
}
|
||||
|
||||
// 7. If largestUnit is in the Plural column of Table 12, then
|
||||
|
@ -409,8 +409,7 @@ Optional<String> to_largest_temporal_unit(GlobalObject& global_object, Object co
|
|||
// 8. If disallowedUnits contains largestUnit, then
|
||||
if (disallowed_units.contains_slow(largest_unit)) {
|
||||
// a. Throw a RangeError exception.
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::OptionIsNotValidValue, largest_unit, vm.names.largestUnit.as_string());
|
||||
return {};
|
||||
return vm.throw_completion<RangeError>(global_object, ErrorType::OptionIsNotValidValue, largest_unit, vm.names.largestUnit.as_string());
|
||||
}
|
||||
|
||||
// 9. Return largestUnit.
|
||||
|
|
|
@ -94,7 +94,7 @@ ThrowCompletionOr<String> to_temporal_rounding_mode(GlobalObject&, Object const&
|
|||
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);
|
||||
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);
|
||||
ThrowCompletionOr<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);
|
||||
String larger_of_two_temporal_units(StringView, StringView);
|
||||
|
|
|
@ -201,12 +201,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until)
|
|||
auto default_largest_unit = larger_of_two_temporal_units("second"sv, *smallest_unit);
|
||||
|
||||
// 7. Let largestUnit be ? ToLargestTemporalUnit(options, « "year", "month", "week", "day" », "auto", defaultLargestUnit).
|
||||
auto largest_unit = to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto largest_unit = TRY_OR_DISCARD(to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit)));
|
||||
|
||||
// 8. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit).
|
||||
validate_temporal_unit_range(global_object, *largest_unit, *smallest_unit);
|
||||
validate_temporal_unit_range(global_object, largest_unit, *smallest_unit);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
|
@ -223,7 +221,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::until)
|
|||
auto rounded_ns = difference_instant(global_object, instant->nanoseconds(), other->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode);
|
||||
|
||||
// 13. Let result be ! BalanceDuration(0, 0, 0, 0, 0, 0, roundedNs, largestUnit).
|
||||
auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, *largest_unit);
|
||||
auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit);
|
||||
|
||||
// 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
|
||||
return create_temporal_duration(global_object, 0, 0, 0, 0, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds);
|
||||
|
@ -255,12 +253,10 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since)
|
|||
auto default_largest_unit = larger_of_two_temporal_units("second"sv, *smallest_unit);
|
||||
|
||||
// 7. Let largestUnit be ? ToLargestTemporalUnit(options, « "year", "month", "week", "day" », "auto", defaultLargestUnit).
|
||||
auto largest_unit = to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit));
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto largest_unit = TRY_OR_DISCARD(to_largest_temporal_unit(global_object, *options, { "year"sv, "month"sv, "week"sv, "day"sv }, "auto"sv, move(default_largest_unit)));
|
||||
|
||||
// 8. Perform ? ValidateTemporalUnitRange(largestUnit, smallestUnit).
|
||||
validate_temporal_unit_range(global_object, *largest_unit, *smallest_unit);
|
||||
validate_temporal_unit_range(global_object, largest_unit, *smallest_unit);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
||||
|
@ -277,7 +273,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::since)
|
|||
auto rounded_ns = difference_instant(global_object, other->nanoseconds(), instant->nanoseconds(), rounding_increment, *smallest_unit, rounding_mode);
|
||||
|
||||
// 13. Let result be ! BalanceDuration(0, 0, 0, 0, 0, 0, roundedNs, largestUnit).
|
||||
auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, *largest_unit);
|
||||
auto result = balance_duration(global_object, 0, 0, 0, 0, 0, 0, *rounded_ns, largest_unit);
|
||||
|
||||
// 14. Return ? CreateTemporalDuration(0, 0, 0, 0, result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]).
|
||||
return create_temporal_duration(global_object, 0, 0, 0, 0, result->hours, result->minutes, result->seconds, result->milliseconds, result->microseconds, result->nanoseconds);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue