botbuilder-dialogs-adaptive
Version:
Rule system for the Microsoft BotBuilder dialog system.
77 lines • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RegexEntityRecognizer = void 0;
const textEntityRecognizer_1 = require("./textEntityRecognizer");
/**
* Matches input against a regular expression.
*/
class RegexEntityRecognizer extends textEntityRecognizer_1.TextEntityRecognizer {
/**
* Initializes a new instance of the [RegexEntityRecognizer](xref:botbuilder-dialogs-adaptive.RegexEntityRecognizer) class.
*
* @param name The name match result `typeName` value.
* @param pattern The regular expression pattern value.
*/
constructor(name, pattern) {
super();
if (name) {
this.name = name;
}
if (pattern) {
this.pattern = pattern;
}
}
/**
* Gets the regular expression pattern value.
*
* @returns The pattern.
*/
get pattern() {
return this._pattern;
}
/**
* Sets the pattern.
*/
set pattern(value) {
if (value.startsWith('(?i)')) {
value = value.substr(4);
}
this._pattern = value;
}
/**
* @protected
* Match recognizing implementation.
* @param text Text to match.
* @param _culture Culture to use.
* @returns The matched [ModelResult](xref:botbuilder-dialogs.ModelResult) list.
*/
_recognize(text, _culture) {
const results = [];
const matches = [];
let matched;
// eslint-disable-next-line security/detect-non-literal-regexp
const regexp = new RegExp(this._pattern, 'ig');
while ((matched = regexp.exec(text))) {
matches.push(matched);
if (regexp.lastIndex == text.length) {
break; // to avoid infinite loop
}
}
for (let i = 0; i < matches.length; i++) {
const match = matches[i];
const text = match[0];
const result = {
typeName: this.name,
text: text,
start: match.index,
end: match.index + text.length,
resolution: {},
};
results.push(result);
}
return results;
}
}
exports.RegexEntityRecognizer = RegexEntityRecognizer;
RegexEntityRecognizer.$kind = 'Microsoft.RegexEntityRecognizer';
//# sourceMappingURL=regexEntityRecognizer.js.map