Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 28cb4a3

Browse files
flarniefacebook-github-bot
authored andcommitted
Add clarifying comments to 'getRemovalRange'
Summary: I found some of this code confusing and added comments. Will probably add more comments while looking at this part of the code. For now pushing this before I lose track of things. Not in any hurry to merge it. Closes #1103 Reviewed By: mitermayer Differential Revision: D7137918 Pulled By: mitermayer fbshipit-source-id: 350786c4817e122efbce146f6e6ccc26ed12ae0f
1 parent 8b3e8c9 commit 28cb4a3

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/model/transaction/removeEntitiesAtEdges.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,36 @@ function removeEntitiesAtEdges(
6565
});
6666
}
6767

68+
/**
69+
* Given a list of characters and an offset that is in the middle of an entity,
70+
* returns the start and end of the entity that is overlapping the offset.
71+
* Note: This method requires that the offset be in an entity range.
72+
*/
6873
function getRemovalRange(
6974
characters: List<CharacterMetadata>,
70-
key: ?string,
75+
entityKey: ?string,
7176
offset: number,
72-
): Object {
77+
): {
78+
start: number,
79+
end: number,
80+
} {
7381
var removalRange;
82+
83+
// Iterates through a list looking for ranges of matching items
84+
// based on the 'isEqual' callback.
85+
// Then instead of returning the result, call the 'found' callback
86+
// with each range.
87+
// Then filters those ranges based on the 'filter' callback
88+
//
89+
// Here we use it to find ranges of characters with the same entity key.
7490
findRangesImmutable(
75-
characters,
76-
(a, b) => a.getEntity() === b.getEntity(),
77-
element => element.getEntity() === key,
78-
(start, end) => {
91+
characters, // the list to iterate through
92+
(a, b) => a.getEntity() === b.getEntity(), // 'isEqual' callback
93+
element => element.getEntity() === entityKey, // 'filter' callback
94+
(start: number, end: number) => {
95+
// 'found' callback
7996
if (start <= offset && end >= offset) {
97+
// this entity overlaps the offset index
8098
removalRange = {start, end};
8199
}
82100
},

0 commit comments

Comments
 (0)