@blinkk/editor
Version:
Structured content editor with live previews.
63 lines • 2.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.escapeRegExp = exports.IncludeExcludeFilter = void 0;
const dataType_1 = require("@blinkk/selective-edit/dist/src/utility/dataType");
class IncludeExcludeFilter {
constructor(config) {
this.config = config;
// Force all of the includes and excludes to be regex.
this.convertRegex(this.config.includes);
this.convertRegex(this.config.excludes);
}
convertRegex(values) {
if (!values) {
return;
}
for (let i = 0; i < values.length; i++) {
if (!dataType_1.DataType.isRegExp(values[i])) {
values[i] = new RegExp(values[i]);
}
}
}
filter(values) {
return values.filter(this.matches, this);
}
matches(value) {
// If there are includes, it needs to match at least one of them.
let matchesIncludes = false;
if (this.config.includes) {
for (const test of this.config.includes) {
if (test.test(value)) {
matchesIncludes = true;
break;
}
}
}
else {
matchesIncludes = true;
}
// If there are excludes, it cannot match any of them.
let matchesExcludes = false;
if (this.config.excludes) {
for (const test of this.config.excludes) {
if (test.test(value)) {
matchesExcludes = true;
break;
}
}
}
return matchesIncludes && !matchesExcludes;
}
}
exports.IncludeExcludeFilter = IncludeExcludeFilter;
/**
* Escapes a string to be used as a 'constant' in a regex.
*
* @param value string to be escaped
* @returns escaped string to use in regex.
*/
function escapeRegExp(value) {
return value.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
}
exports.escapeRegExp = escapeRegExp;
//# sourceMappingURL=filter.js.map