2017-04-04 04:36:03 +00:00
|
|
|
// 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
|
2022-06-19 00:25:21 +00:00
|
|
|
|
|
|
|
import {Part} from "./merge";
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// parsed into recognizable entries already.
|
2022-06-19 00:25:21 +00:00
|
|
|
function fill(existingEntries: Part[], text: string) {
|
2017-03-18 08:35:17 +00:00
|
|
|
let position = 0;
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// 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`.
|
2022-06-19 00:25:21 +00:00
|
|
|
const result = existingEntries.reduce<Part[]>((acc, textSegment) => {
|
2017-03-18 08:35:17 +00:00
|
|
|
if (textSegment.start > position) {
|
2017-04-04 04:36:03 +00:00
|
|
|
acc.push({
|
2017-03-18 08:35:17 +00:00
|
|
|
start: position,
|
2017-11-15 06:35:15 +00:00
|
|
|
end: textSegment.start,
|
2017-03-18 08:35:17 +00:00
|
|
|
});
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
position = textSegment.end;
|
2017-04-04 04:36:03 +00:00
|
|
|
return acc;
|
|
|
|
}, []);
|
2017-03-18 08:35:17 +00:00
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Complete the unmatched end of the text with a dummy entry
|
2017-03-18 08:35:17 +00:00
|
|
|
if (position < text.length) {
|
|
|
|
result.push({
|
|
|
|
start: position,
|
2017-11-15 06:35:15 +00:00
|
|
|
end: text.length,
|
2017-03-18 08:35:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
export default fill;
|