UNPKG

text-stream-search

Version:

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

33 lines 1.27 kB
/** * StringSearch is the search for a particular string in the text stream. * The search is over once the string is found. * It reports success to the given resolve function once when it finds the query string. * When reaching the given timeoutDuration, it abourts the search and calls the given reject function. */ export class StringSearch { constructor(query, resolve, reject, text, timeout) { this.resolve = resolve; this.reject = reject; this.text = text; this.searchText = query; if (timeout != null) { this.timeoutDuration = timeout; setTimeout(this.onTimeout.bind(this), timeout); } } /** * Checks the given text for the searchText. * Calls the resolve function when it finds it. */ scan() { if (this.text.toString().includes(this.searchText)) { this.resolve(this.searchText); } } /** called after this subscription times out */ onTimeout() { var _a; this.reject(new Error(`Text "${this.searchText}" not found within ${(_a = this.timeoutDuration) !== null && _a !== void 0 ? _a : -1} ms. The captured text so far is:\n${this.text.toString()}`)); } } //# sourceMappingURL=string-search.js.map