UNPKG

geneea-nlp-client

Version:

The TypeScript Client for Geneea Interpretor G3 API.

61 lines (60 loc) 2.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JS2CpOffsetConverter = exports.Cp2JSOffsetConverter = void 0; /** * A tool for [converting][convert] a sorted series of code-point offsets in a [text] * to JavaScript string character offsets. */ class Cp2JSOffsetConverter { constructor(text) { this.lastCharOffset = 0; this.lastCodepointOffset = 0; this.textArray = [...text]; } /** * Converts a codepoint offset in [text] to a JS-char-based-offset. * * @param codepointOffset The offset in code points. * @throws RangeError if the [codepointOffset] is out of bounds of [text], * or when its value in this call is smaller than in the previous call. */ convert(codepointOffset) { if (codepointOffset < this.lastCodepointOffset) { throw new RangeError(); } this.lastCharOffset += this.textArray .splice(0, codepointOffset - this.lastCodepointOffset) .join("").length; this.lastCodepointOffset = codepointOffset; return this.lastCharOffset; } } exports.Cp2JSOffsetConverter = Cp2JSOffsetConverter; /** * A tool for [converting][convert] a sorted series of JavaScript string character offsets * in a [text] to code-point offsets. */ class JS2CpOffsetConverter { constructor(text) { this.text = text; this.lastCodepointOffset = 0; this.lastCharOffset = 0; } /** * Converts a JavaScript string char-based offset in [text] to a codepoint offset. * * @param charOffset The offset in JavaScript string characters. * @throws RangeError if the [charOffset] is out of bounds of [text], * or when its value in this call is smaller than in the previous call. */ convert(charOffset) { if (charOffset < this.lastCharOffset) { throw new RangeError(); } const cpOff = Array.from(this.text.slice(this.lastCharOffset, charOffset)).length; this.lastCodepointOffset += cpOff; this.lastCharOffset = charOffset; return this.lastCodepointOffset; } } exports.JS2CpOffsetConverter = JS2CpOffsetConverter;