Label.merge constructor

Label.merge(
  1. Label value,
  2. Label other
)

Implementation

factory Label.merge(Label value, Label other) {
  assert(value.key == other.key,
      'The two merged labels should have the same key');
  final cases = <Case>[];

  for (var otherCase in other.cases) {
    final existingCase = cases.cast<Case?>().firstWhere(
          (x) => x!.condition == otherCase.condition,
          orElse: () => null,
        );
    if (existingCase == null) {
      cases.add(otherCase);
    } else {
      cases.add(Case.merge(existingCase, otherCase));
    }
  }
  final casesOnlyInValue = value.cases.where(
    (x) => !other.cases.any((o) => o.condition == x.condition),
  );
  cases.addAll(casesOnlyInValue);

  final result = Label(
    key: value.key,
    cases: cases,
  );

  Logger.root.finer(
      '[{LABEL} Merging]:\n\n\tITEM1===================================\n$value\n\n\tITEM2===================================\n$other\n\n\tRESULT===================================:\:\n$result\n\n');

  return result;
}