FhirPathQuantity.fromString constructor

FhirPathQuantity.fromString(
  1. String quantityString
)

Implementation

factory FhirPathQuantity.fromString(String quantityString) {
  if (FhirPathQuantity.fhirPathQuantityRegex
      .hasMatch(quantityString.replaceAll(r"\'", "'"))) {
    final match = FhirPathQuantity.fhirPathQuantityRegex
        .firstMatch(quantityString.replaceAll(r"\'", "'"));
    final value = match?.namedGroup('value');
    if (value == null) {
      throw FhirPathEvaluationException(
          'Malformed quantity: $quantityString');
    }
    final unit = match?.namedGroup('unit');
    final time = match?.namedGroup('time');
    String unitString = '';

    if (unit == null && time == null) {
      // Special logic for UCUM, where the empty unit is '1';
      unitString = '1';
    } else if (unit != null) {
      unitString = unit;
    } else if (time != null) {
      unitString = time;
    }

    // Cannot just replace all apostrophes, as some units have one in the middle.
    if (unitString.startsWith("'")) {
      unitString = unitString.substring(1, unitString.length - 1);
    }

    // Escaped ' can all be removed
    unitString.replaceAll(r"\'", '');

    // Try to normalize time-valued units
    unitString = timeValuedQuantitiesUnits[unitString] ?? unitString;

    return FhirPathQuantity(num.parse(value), unitString);
  } else {
    throw FhirPathEvaluationException('Malformed quantity: $quantityString');
  }
}