ui.static.regexInspector.js Maven / Gradle / Ivy
function regexInspectorDispose(textInput) {
textInput.popover("dispose");
}
function regexInspectorShow(textInput) {
textInput.popover("show");
}
function regexInspector(textInput, options) {
textInput.popover("dispose");
textInput.popover({
trigger: 'focus',
placement: 'bottom',
container: 'body',
html: true,
content: function () {
return generatePopupHtml(textInput.val(), options);
},
title: function() {
return options.title ? options.title : "Regex matches";
},
});
if (!options.disableChangeHook) {
textInput.on("keypress, input", function () {
textInput.popover('hide');
textInput.popover('show');
});
} else {
textInput.popover('show');
}
}
function generatePopupHtml(inputValue, options) {
let result;
try {
result = processRegex(inputValue, options);
} catch (e) {
return 'Invalid regex: ' + e + '';
}
let html = "";
html += "";
html += "";
html += "Hits ("+result.matching.length+") ";
html += "Misses ("+result.filtered.length+") ";
html += " ";
html += "";
html += "";
result.matching.forEach(function (value) {
html += value + "
";
});
html += " ";
html += "";
result.filtered.forEach(function (value) {
html += value + "
";
});
html += " ";
html += " ";
html += "
";
return html;
}
function processRegex(inputValue, options) {
let value = options.valuePreProcessor ? options.valuePreProcessor(inputValue) : inputValue;
let data = options.source;
if (value.trim().length === 0) {
return {
matching: data,
filtered: []
};
}
let matching = [];
let filtered = [];
let regex = options.matchEntire ? RegExp("^" + value + "$") : RegExp(value);
data.forEach(function (value) {
if (value.match(regex)) {
matching.push(value);
} else {
filtered.push(value);
}
});
return {
matching: matching,
filtered: filtered
};
}