UNPKG

text-stream-search

Version:

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

39 lines (38 loc) 1.41 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const base_search_1 = __importDefault(require("./base-search")); const debug = debug_1.default("text-stream-search:string-search"); // Calls the given handler exactly one time // when text matches the given string class StringSearch extends base_search_1.default { constructor(query, accumulator, resolve, reject, timeout) { super(accumulator, resolve, reject, timeout); this.searchText = query; } // checks for matches async check(text) { if (this.matches(text)) { await this.foundMatch(); } } // Returns the display name for debug / error messages getDisplayName() { return `string '${this.searchText}'`; } // Returns whether the given text contains the search text this search is looking for matches(text) { const result = text.includes(this.searchText); if (result) { debug(`search for '${this.searchText}' found a match in '${text}'`); } else { debug(`no match in '${text}'`); } return result; } } exports.default = StringSearch;