@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
54 lines • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenSkipper = void 0;
const tokens_1 = require("./tokens");
class TokenSkipper {
constructor(separators = tokens_1.DEFAULT_SEPARATORS_REGEX, notSeparators = tokens_1.DEFAULT_NOT_SEPARATORS_REGEX) {
this.separators = separators;
this.notSeparators = notSeparators;
this.notClosingSeparator = /[^?!)]/g;
}
/**
* Eg. skipWords('a? b',1, false) => 1
* @param text
* @param skipWordsCount how many words to skip
* @param skipClosingSeparators whether characters like ? must be skipped
*/
skipWords(text, skipWordsCount, skipClosingSeparators) {
let idx = 0;
for (let w = 0; w < skipWordsCount; w++) {
idx = this.skipSeparators(text, idx, this.notSeparators);
if (idx >= 0) {
idx = this.skipWord(text, idx);
}
if (idx < 0) {
if (w < skipWordsCount - 1) {
throw new Error(`skipWords('${text}', ${skipWordsCount}} failed because it only has ${w} words`);
}
}
}
if (idx >= 0) {
idx = this.skipSeparators(text, idx, skipClosingSeparators ? this.notSeparators : this.notClosingSeparator);
}
if (idx < 0) {
return text.length;
}
return idx;
}
skipWord(text, offset) {
const idx = text.substr(offset).search(this.separators);
if (idx < 0) {
return idx;
}
return idx + offset;
}
skipSeparators(text, offset, seps) {
const idx = text.substr(offset).search(seps);
if (idx < 0) {
return idx;
}
return idx + offset;
}
}
exports.TokenSkipper = TokenSkipper;
//# sourceMappingURL=token-skipper.js.map