UNPKG

text-stream-search

Version:

Searches for occurrences of a given search term in Node.js text streams

32 lines 1.24 kB
/** * RegexSearch looks for the given regex in the text stream. */ export class RegexSearch { constructor(regex, resolve, reject, text, timeoutDuration) { this.resolve = resolve; this.reject = reject; this.text = text; this.searchRegexp = new RegExp(regex); if (timeoutDuration != null) { this.timeoutDuration = timeoutDuration; setTimeout(this.onTimeout.bind(this), timeoutDuration); } } /** Scan checks the stream text for occurrences of the searchRegexp. */ scan() { const matches = this.searchRegexp.exec(this.text.toString()); if (matches) { const match = matches[0]; if (match == null) { throw new Error(`Regex "{this.regex}" does not contain a capture group`); } this.resolve(match); } } /** OnTimeOut is called after this subscription times out. */ onTimeout() { var _a; this.reject(new Error(`Regex ${this.searchRegexp.toString()} not found within ${(_a = this.timeoutDuration) !== null && _a !== void 0 ? _a : -1} ms. The captured text so far is:\n${this.text.toString()}`)); } } //# sourceMappingURL=regex-search.js.map