pdf-to-markdown/src/javascript/models/HeadlineFinder.jsx

42 lines
1.3 KiB
React
Raw Normal View History

import { normalizedCharCodeArray } from '../stringFunctions.jsx'
2017-03-07 18:42:14 +01:00
export default class HeadlineFinder {
constructor(options) {
this.headlineCharCodes = normalizedCharCodeArray(options.headline);
this.stackedLineItems = [];
2017-03-07 18:42:14 +01:00
this.stackedChars = 0;
}
consume(lineItem) {
//TODO avoid join
const normalizedCharCodes = normalizedCharCodeArray(lineItem.text());
2017-03-07 18:42:14 +01:00
const matchAll = this.matchAll(normalizedCharCodes);
if (matchAll) {
this.stackedLineItems.push(lineItem);
2017-03-07 18:42:14 +01:00
this.stackedChars += normalizedCharCodes.length;
if (this.stackedChars == this.headlineCharCodes.length) {
return this.stackedLineItems;
2017-03-07 18:42:14 +01:00
}
} else {
if (this.stackedChars > 0) {
this.stackedChars = 0;
this.stackedLineItems = [];
this.consume(lineItem); // test again without stack
2017-03-07 18:42:14 +01:00
}
}
return null;
}
matchAll(normalizedCharCodes) {
for (var i = 0; i < normalizedCharCodes.length; i++) {
const headlineChar = this.headlineCharCodes[this.stackedChars + i];
const textItemChar = normalizedCharCodes[i];
if (textItemChar != headlineChar) {
return false;
}
}
return true;
}
}