matchesValue method

bool matchesValue(
  1. Object? keyValue,
  2. RegExp regExp, {
  3. EntityHandler? entityHandler,
})

Matches a keyValue with regExp (see the class documentation). An entity (or reference) is matched by its ID.

Implementation

bool matchesValue(
  Object? keyValue,
  RegExp regExp, {
  EntityHandler? entityHandler,
}) {
  // A `null` matches only `!~` (as `!=` and `!~ [...]`):
  bool matchesText(Object? v) =>
      v == null ? not : regExp.hasMatch(valueToText(v)) != not;

  if (keyValue == null) return not;

  // The values of a to-many relationship: any of them (as a SQL join of its
  // rows: a `null`, e.g. an unsaved reference, isn't a row); without any,
  // `!~` matches (a `NULL` row).
  Iterable<Object>? values;
  if (keyValue is EntityReferenceList) {
    values = (keyValue.ids ?? const []).nonNulls;
  } else if (keyValue is Iterable) {
    values = keyValue.map((v) => _entityID(v, entityHandler)).nonNulls;
  }

  if (values != null) {
    return (not && values.isEmpty) || values.any(matchesText);
  }

  return matchesText(_entityID(keyValue, entityHandler));
}