Skip to content

Commit

Permalink
fix: GetNumberOption throws a RangeError
Browse files Browse the repository at this point in the history
  • Loading branch information
kartva committed Aug 22, 2024
1 parent d7c1a00 commit 7be8c24
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components/experimental/src/duration/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub enum FractionalDigits {
#[default]
ShowAll,
/// Use the given number of fractional digits.
/// Default [`FractionalDigits::ShowAll`] behavior is used if this value is out of the range 0..=9.
/// This value must be in the range 0..=9.
/// Fractional digits are rounded to zero if necessary.
Fixed(u8),
}
Expand Down
14 changes: 9 additions & 5 deletions components/experimental/src/duration/validated_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub enum DurationFormatterOptionsError {
/// Returned when a previous unit's style is set to [`FieldStyle::Numeric`] or [`FieldStyle::TwoDigit`] and the following unit's style is not
/// [`FieldStyle::Fractional`], [`FieldStyle::Numeric`], or [`FieldStyle::TwoDigit`].
PreviousNumeric,

/// Returned when the number of fractional digits is out of acceptable range. See [`FractionalDigits::Fixed`].
FractionalDigitsOutOfRange,
}

impl ValidatedDurationFormatterOptions {
Expand Down Expand Up @@ -191,7 +194,7 @@ impl ValidatedDurationFormatterOptions {
// 27. Set durationFormat.[[FractionalDigits]] to ? GetNumberOption(options, "fractionalDigits", 0, 9, undefined).
if let FractionalDigits::Fixed(i) = builder.fractional_digits {
if i > 9 {
builder.fractional_digits = FractionalDigits::ShowAll;
return Err(DurationFormatterOptionsError::FractionalDigitsOutOfRange);
}
}

Expand Down Expand Up @@ -430,14 +433,15 @@ mod tests {
use super::*;

#[test]
fn test_fractional_digit_clamp() {
fn test_fractional_digit_error() {
let options = DurationFormatterOptions {
fractional_digits: FractionalDigits::Fixed(10),
..Default::default()
};

let validated = ValidatedDurationFormatterOptions::validate(options).unwrap();

assert_eq!(validated.fractional_digits, FractionalDigits::ShowAll);
assert_eq!(
ValidatedDurationFormatterOptions::validate(options),
Err(DurationFormatterOptionsError::FractionalDigitsOutOfRange)
);
}
}

0 comments on commit 7be8c24

Please sign in to comment.