execute method

  1. @override
List execute(
  1. List results,
  2. Map<String, dynamic> passed
)
override

The iterable, nested function that evaluates the entire FHIRPath expression one object at a time

Implementation

@override
List execute(List results, Map<String, dynamic> passed) {
  final executedBefore = before.execute(results.toList(), passed);
  final executedAfter = after.execute(results.toList(), passed);
  if (executedBefore.isEmpty) {
    if (executedAfter.isEmpty) {
      return [true];
    } else {
      return [false];
    }
  } else if (executedBefore.length != executedAfter.length) {
    return [false];
  } else {
    executedBefore.removeWhere(
      (lhsElement) =>
          executedAfter.indexWhere((rhsElement) {
            if (lhsElement is FhirDateTime ||
                lhsElement is FhirDate ||
                rhsElement is FhirDateTime ||
                rhsElement is FhirDate) {
              /// As long as one is, we convert them both to strings then back
              /// to DateTimes
              final lhsDateTime = FhirDateTime(lhsElement.toString());
              final rhsDateTime = FhirDateTime(rhsElement.toString());

              final equals = lhsDateTime.isEqual(rhsDateTime);
              if (equals != null) {
                return equals;
              } else {
                return false;
              }
            } else if (lhsElement is ValidatedQuantity) {
              return lhsElement.equivalent(rhsElement);
            } else if (rhsElement is ValidatedQuantity) {
              return rhsElement.equivalent(lhsElement);
            } else if (lhsElement is num || rhsElement is num) {
              final num? lhsNum = num.tryParse(lhsElement.toString());
              final num? rhsNum = num.tryParse(rhsElement.toString());
              final int? sigDigsLhs = lhsNum
                  ?.toStringAsExponential()
                  .split('e')
                  .first
                  .replaceAll('.', '')
                  .length;
              final int? sigDigsRhs = rhsNum
                  ?.toStringAsExponential()
                  .split('e')
                  .first
                  .replaceAll('.', '')
                  .length;
              if (sigDigsLhs == null || sigDigsRhs == null) {
                return false;
              } else {
                if (sigDigsLhs < sigDigsRhs) {
                  return lhsNum?.toStringAsPrecision(sigDigsLhs) ==
                      rhsNum?.toStringAsPrecision(sigDigsLhs);
                } else {
                  return lhsNum?.toStringAsPrecision(sigDigsRhs) ==
                      rhsNum?.toStringAsPrecision(sigDigsRhs);
                }
              }
            } else if (lhsElement is String || rhsElement is String) {
              return lhsElement.toString().toLowerCase() ==
                  rhsElement.toString().toLowerCase();
            } else {
              return lhsElement == rhsElement || rhsElement == lhsElement;
            }
          }) !=
          -1,
    );
    return [executedBefore.isEmpty];
  }
}