@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
50 lines • 1.75 kB
JavaScript
import { DEFAULT_NOT_SEPARATORS_REGEX, DEFAULT_SEPARATORS_REGEX, } from './tokens';
export class TokenSkipper {
constructor(separators = DEFAULT_SEPARATORS_REGEX, notSeparators = 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;
}
}
//# sourceMappingURL=token-skipper.js.map