revt-detector
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

What is this

Detect continuous textlines.

How the detector works

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.

How to use

Basic Use

Use lines detector as a sample.

  1. Import
import {
  DetectMode,
  TextlinesDetector,
  LineDetectCondition,
} from "revt-detector";
  1. Define a condition
class LDC extends LineDetectCondition {
  check(line: string) {
    return line[1] !== " ";
  }
}

let condition = new LDC();
  1. Init a detector
let mode = DetectMode.AUTO;
let TD = new TextlinesDetector({
  data,
  mode,
  condition,
});
  1. Run and get info
const detectedLines = TD.detect();
const remainLines = TD.getRemain();

P.S

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();

Appendix

Hooks

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";

Readme

Keywords

none

Package Sidebar

Install

npm i revt-detector

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

86.6 kB

Total Files

44

Last publish

Collaborators

  • rokamin