approvals
Version:
Approval Tests Library - Capturing Human Intelligence
76 lines (75 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Scrubbers = void 0;
const DateScrubber_1 = require("./DateScrubber");
function _replaceRegex(text, regex, replacement) {
const capturedGuids = [];
const result = text.replace(regex, (match) => {
if (capturedGuids.indexOf(match) < 0) {
capturedGuids.push(match);
}
const index = capturedGuids.indexOf(match) + 1;
return replacement(index);
});
return result;
}
class Scrubbers {
static createReqexScrubber(regex, replacement) {
if (typeof replacement === "function") {
return (t) => _replaceRegex(t, regex, replacement);
}
else {
const replacementString = replacement;
return (t) => _replaceRegex(t, regex, () => replacementString);
}
}
static createGuidScrubber() {
const guidReqex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
return this.createReqexScrubber(guidReqex, (t) => `<guid_${t}>`);
}
static noScrubber(data) {
return data;
}
static guidScrubber(data) {
const guidRegex = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
const capturedGuids = [];
const result = data.replace(guidRegex, function (match) {
// The arguments are:
// 1: The whole match (string)
// 2..n+1: The captures (string or undefined)
// n+2: Starting position of match (0 = start)
// n+3: The subject string.
// (n = number of capture groups)
if (capturedGuids.indexOf(match) < 0) {
capturedGuids.push(match);
}
const index = capturedGuids.indexOf(match) + 1;
return `guid_${index}`;
});
return result;
}
static multiScrubber(scrubbers) {
return function (data) {
scrubbers.forEach(function (scrubber) {
data = scrubber(data);
});
return data;
};
}
}
exports.Scrubbers = Scrubbers;
/**
* This method exists as a convenient way to get an example scrubber for you to use.
* To use this template, simply inline the method in your IDE.
*/
Scrubbers.templates = class {
static regexScrubberWithLambda() {
return Scrubbers.createReqexScrubber(/your pattern here: [a-zA-Z]+d{4}/gi, (t) => `<your replacement_${t}>`);
}
static regexScrubber() {
return Scrubbers.createReqexScrubber(/your pattern here: [a-zA-Z]+d{4}/gi, `<your replacement>`);
}
static dateScrubber() {
return DateScrubber_1.DateScrubber.getScrubberFor("2014/05/13 16:30:59.786");
}
};