operator + method

ChapterList operator +(
  1. ChapterList other
)

Merges two list of Chapters.

The merge will combine chapters with the same title, appending the contents from the second chapter into the first chapter.

Chapters are returned in the following order:

  • First the chapters from first, with the merged contents from second if applicable.
  • Then the chapters from second.

Implementation

ChapterList operator +(ChapterList other) {
  List<Chapter> result = [];
  result.addAll(chapters.map((e) {
    if (other.chapters.any((element) => element.heading == e.heading)) {
      return e +
          other.chapters
              .firstWhere((element) => element.heading == e.heading);
    }
    return e;
  }));
  result.addAll(other.chapters.where(
      (element) => !result.map((e) => e.heading).contains(element.heading)));
  return ChapterList(heading: heading, chapters: result);
}