add method

dynamic add(
  1. String term,
  2. String field,
  3. Metadata? metadata
)

Add metadata for a term/field pair to this instance of match data.

term - The term this match data is associated with field - The field in which the term was found metadata - The metadata recorded about this term in this field

Implementation

add(String term, String field, Metadata? metadata) {
  metadata ??= {};
  if (!(this.metadata.containsKey(term))) {
    this.metadata[term] = {};
    this.metadata[term]![field] = metadata;
    return;
  }

  if (!this.metadata[term]!.containsKey(field)) {
    this.metadata[term]![field] = metadata;
    return;
  }

  Iterable<String> metadataKeys = metadata.keys;

  for (String key in metadataKeys) {
    if (this.metadata[term]![field]!.containsKey(key)) {
      this.metadata[term]![field]![key] = [
        ...this.metadata[term]![field]![key]!,
        ...metadata[key]!
      ];
    } else {
      this.metadata[term]![field]![key] = metadata[key]!;
    }
  }
}