@ -2,7 +2,7 @@
use crate ::{
builtins ::{
options ::{ get_option , get_options_object , RoundingMode } ,
options ::{ get_option , get_options_object } ,
BuiltInBuilder , BuiltInConstructor , BuiltInObject , IntrinsicObject ,
} ,
context ::intrinsics ::{ Intrinsics , StandardConstructor , StandardConstructors } ,
@ -15,7 +15,10 @@ use crate::{
} ;
use boa_gc ::{ Finalize , Trace } ;
use boa_profiler ::Profiler ;
use temporal_rs ::{ components ::Duration as InnerDuration , options ::TemporalUnit } ;
use temporal_rs ::{
components ::Duration as InnerDuration ,
options ::{ RelativeTo , TemporalRoundingMode , TemporalUnit } ,
} ;
use super ::{
options ::{ get_temporal_rounding_increment , get_temporal_unit , TemporalUnitGroup } ,
@ -590,7 +593,6 @@ impl Duration {
. into ( ) )
}
// TODO: Migrate to `temporal_rs's Duration::round`
/// 7.3.20 `Temporal.Duration.prototype.round ( roundTo )`
pub ( crate ) fn round (
this : & JsValue ,
@ -599,7 +601,7 @@ impl Duration {
) -> JsResult < JsValue > {
// 1. Let duration be the this value.
// 2. Perform ? RequireInternalSlot(duration, [[InitializedTemporalDuration]]).
let _ duration = this
let duration = this
. as_object ( )
. and_then ( JsObject ::downcast_ref ::< Self > )
. ok_or_else ( | | {
@ -651,15 +653,15 @@ impl Duration {
// 10. Let relativeToRecord be ? ToRelativeTemporalObject(roundTo).
// 11. Let zonedRelativeTo be relativeToRecord.[[ZonedRelativeTo]].
// 12. Let plainRelativeTo be relativeToRecord.[[PlainRelativeTo]].
let ( _ plain_relative_to, _ zoned_relative_to) =
let ( plain_relative_to , zoned_relative_to ) =
super ::to_relative_temporal_object ( & round_to , context ) ? ;
// 13. Let roundingIncrement be ? ToTemporalRoundingIncrement(roundTo).
let _ rounding_increment = get_temporal_rounding_increment ( & round_to , context ) ? ;
let rounding_increment = get_temporal_rounding_increment ( & round_to , context ) ? ;
// 14. Let roundingMode be ? ToTemporalRoundingMode(roundTo, "halfExpand").
let _ rounding_mode = get_option ( & round_to , utf16 ! ( "roundingMode" ) , context ) ?
. unwrap_or ( RoundingMode ::HalfExpand ) ;
let rounding_mode =
get_option ::< TemporalRoundingMode > ( & round_to , utf16 ! ( "roundingMode" ) , context ) ? ;
// 15. Let smallestUnit be ? GetTemporalUnit(roundTo, "smallestUnit", datetime, undefined).
let smallest_unit = get_temporal_unit (
@ -678,11 +680,18 @@ impl Duration {
. with_message ( "smallestUnit or largestUnit must be present." )
. into ( ) ) ;
}
// NOTE:
Err ( JsNativeError ::range ( )
. with_message ( "not yet implemented." )
. into ( ) )
let rounded_duration = duration . inner . round (
rounding_increment ,
smallest_unit ,
largest_unit ,
rounding_mode ,
& RelativeTo {
date : plain_relative_to . as_ref ( ) ,
zdt : zoned_relative_to . as_ref ( ) ,
} ,
context ,
) ? ;
create_temporal_duration ( rounded_duration , None , context ) . map ( Into ::into )
}
/// 7.3.21 `Temporal.Duration.prototype.total ( totalOf )`