toSingleton static method

List toSingleton(
  1. List input, {
  2. String? name,
  3. String? operation,
  4. List? collection,
})

Returns a singleton, based on the input.

Throws if input is not empty, or has single entry. Creates ValidatedQuantity from map inputs.

Anything else is merely returned unmodified.

Implementation

static List<dynamic> toSingleton(
  List<dynamic> input, {
  String? name,
  String? operation,
  List<dynamic>? collection,
}) {
  if (input.isEmpty) {
    return [];
  }

  if (input.length > 1) {
    throw FhirPathEvaluationException(
        'The $name is required to be '
        'either an empty value, or a single value. Instead it evaluated to: $input.',
        operation: operation,
        collection: collection);
  }

  final item = input.first;
  if (item is Map) {
    if (item['value'] != null && item['unit'] != null) {
      return [
        ValidatedQuantity(
            value: UcumDecimal.fromString(
                ((item['value']).value ?? double.nan).toString()),
            unit: item['unit'] as String)
      ];
    }
  }

  return input;
}