mirror of
https://github.com/thelounge/thelounge
synced 2024-11-27 06:20:26 +00:00
dd05ee3a65
Co-authored-by: Eric Nemchik <eric@nemchik.com> Co-authored-by: Pavel Djundik <xPaw@users.noreply.github.com>
36 lines
933 B
TypeScript
36 lines
933 B
TypeScript
// Create plain text entries corresponding to areas of the text that match no
|
|
// existing entries. Returns an empty array if all parts of the text have been
|
|
|
|
import {Part} from "./merge";
|
|
|
|
// parsed into recognizable entries already.
|
|
function fill(existingEntries: Part[], text: string) {
|
|
let position = 0;
|
|
|
|
// Fill inner parts of the text. For example, if text is `foobarbaz` and both
|
|
// `foo` and `baz` have matched into an entry, this will return a dummy entry
|
|
// corresponding to `bar`.
|
|
const result = existingEntries.reduce<Part[]>((acc, textSegment) => {
|
|
if (textSegment.start > position) {
|
|
acc.push({
|
|
start: position,
|
|
end: textSegment.start,
|
|
});
|
|
}
|
|
|
|
position = textSegment.end;
|
|
return acc;
|
|
}, []);
|
|
|
|
// Complete the unmatched end of the text with a dummy entry
|
|
if (position < text.length) {
|
|
result.push({
|
|
start: position,
|
|
end: text.length,
|
|
});
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export default fill;
|