match function

bool match(
  1. dynamic left,
  2. dynamic right, {
  3. IsMatching? isMatching,
  4. num precision = 1.0e-12,
})

Returns true if left matches right.

  • Handles build-in collection objects recursively.
  • Use the callback isMatching to register a custom matcher.
  • Numerical objects may be matched within a given precision. For a precision of 1.0e-12, the comparison 1.0 == 0.999999999999 returns true.

Implementation

bool match(
  dynamic left,
  dynamic right, {
  IsMatching? isMatching,
  num precision = 1.0e-12,
}) {
  // User provided matcher.
  if (isMatching != null) {
    return isMatching(left, right);
  }

  if (left == right) return true;

  if (left is num && right is num) {
    return ((right - left).abs() < precision.abs()) ? true : false;
  }

  if (left is Iterable && right is Iterable) {
    if (left.runtimeType != right.runtimeType) {
      if (left is List && right is List) {
        // Can't check the entry type of an empty list.
        if (left.isEmpty) return false;
      } else if (left is Set && right is Set) {
        // Can't check the entry type of an empty set.
        if (left.isEmpty) return false;
      } else {
        return false;
      }
    }
    if (left.length != right.length) return false;
    final lit = left.iterator;
    final rit = right.iterator;
    while (lit.moveNext() && rit.moveNext()) {
      if (!match(lit.current, rit.current, precision: precision)) {
        return false;
      }
    }
    return true;
  }
  if (left is Map && right is Map) {
    // Can't check key and value type of an empty map.
    if (left.runtimeType != right.runtimeType && left.isEmpty) return false;
    if (left.length != right.length) return false;
    for (final key in left.keys) {
      if (!match(left[key], right[key], precision: precision)) {
        return false;
      }
    }
    return true;
  }

  return false;
}