Detect continuous textlines.
Detect types have two ways (line, wrap). If u want to detect lines, u need one condition to judge. If the other one, u need two conditions to check the first tag and the closed tag.
Use lines detector as a sample.
- Import
import {
DetectMode,
TextlinesDetector,
LineDetectCondition,
} from "revt-detector";
- Define a condition
class LDC extends LineDetectCondition {
check(line: string) {
return line[1] !== " ";
}
}
let condition = new LDC();
- Init a detector
let mode = DetectMode.AUTO;
let TD = new TextlinesDetector({
data,
mode,
condition,
});
- Run and get info
const detectedLines = TD.detect();
const remainLines = TD.getRemain();
If u use wrap detector, plz define two conditions. The usecase as the following.
const topcond = new TopCondition();
const endcond = new EndCondition();
const WD = new WraplinesDetector({ data, topcond, endcond });
const results = WD.detect();
We also support hooks to define them. Below sample is a condition sample.
import { linecondition, wrapcondition } from "revt-detector";
export function lineOptions2Conditions(options: LineOptions) {
const symbol = options.symbol;
const nosymbol = options.nosymbol;
if (symbol && chkSymbolOptions(symbol)) {
return linecondition((line: string) =>
chkLineWithSymbolOptions(line, symbol),
);
}
if (chkNoSymbolOptions(options) && nosymbol) {
return linecondition((line: string) => nosymbol.check(line));
}
throw new Error(
"[Error] Line symbol chk failure. Plz chk u line symbol options.",
);
}
export function wrapOptions2Conditions(options: WrapOptions) {
const begsym = options.begsign;
const endsym = options.endsign;
const begsymchk = chkSymbolOptions(begsym);
const endsymchk = chkSymbolOptions(endsym);
const bothsymchk = begsymchk && endsymchk;
if (!bothsymchk) {
throw new Error(
"[Error] Wrap symbol chk failure. Plz chk u wrap symbol options.",
);
}
return wrapcondition(
(line: string) => chkLineWithSymbolOptions(line, begsym),
(line: string) => chkLineWithSymbolOptions(line, endsym),
);
}
Others
export { textDetector, textAutoDetector, wrapDetector } from "./detectors";