word-frequency-counter
Version:
38 lines (37 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Protection = void 0;
const generator_1 = require("../utils/randomStringGenerator/generator");
class Protection {
randomStringGenerator;
protectionMap;
constructor(protectedWords, randomStringGenerator) {
this.randomStringGenerator = randomStringGenerator;
this.protectionMap = this.buildProtectionMap(protectedWords);
}
buildProtectionMap = (protectedWords) => {
const protectionMap = new Map();
const randomStrings = (0, generator_1.generateRandomStrings)(protectedWords.length, this.randomStringGenerator).values();
protectedWords.forEach((protectedWord) => {
protectionMap.set(protectedWord, `${randomStrings.next().value}`);
});
return protectionMap;
};
addWordProtection = (text) => {
let protectedString = text;
this.protectionMap.forEach((protectedWordRandomized, originalWord) => {
const originalWordMatcher = new RegExp(originalWord, "g");
protectedString = protectedString.replace(originalWordMatcher, protectedWordRandomized);
});
return protectedString;
};
removeWordProtection = (text) => {
let unprotectedString = text;
this.protectionMap.forEach((protectedRandomisedWord, originalWord) => {
const protectedWordMatcher = new RegExp(protectedRandomisedWord, "g");
unprotectedString = unprotectedString.replace(protectedWordMatcher, originalWord);
});
return unprotectedString;
};
}
exports.Protection = Protection;