function getRemovalRange()

in src/model/transaction/removeEntitiesAtEdges.js [69:104]


function getRemovalRange(
  characters: List<CharacterMetadata>,
  entityKey: ?string,
  offset: number,
): {
  start: number,
  end: number,
  ...
} {
  let removalRange;

  // Iterates through a list looking for ranges of matching items
  // based on the 'isEqual' callback.
  // Then instead of returning the result, call the 'found' callback
  // with each range.
  // Then filters those ranges based on the 'filter' callback
  //
  // Here we use it to find ranges of characters with the same entity key.
  findRangesImmutable(
    characters, // the list to iterate through
    (a, b) => a.getEntity() === b.getEntity(), // 'isEqual' callback
    element => element.getEntity() === entityKey, // 'filter' callback
    (start: number, end: number) => {
      // 'found' callback
      if (start <= offset && end >= offset) {
        // this entity overlaps the offset index
        removalRange = {start, end};
      }
    },
  );
  invariant(
    typeof removalRange === 'object',
    'Removal range must exist within character list.',
  );
  return removalRange;
}