Rational.parse constructor

Rational.parse(
  1. String source
)

Parses source as a Rational.

Implementation

factory Rational.parse(String source) {
  final match = _regExp.firstMatch(source);
  if (match == null || match[0] == null) {
    return throw FormatException('Invalid Ratio', source);
  }

  final integer = int.parse(match[1]!);
  if (match[2] == null) return Rational.fromMixed(integer);

  final numerator = int.parse(match[2]!);
  final denominator = int.parse(match[3]!);

  return Rational.fromMixed(integer, numerator, denominator);
}