@darkobits/mask-string
Version:
Mask tokens in a string.
26 lines (25 loc) • 958 B
JavaScript
import maskAll from "./mask-all.js";
function maskString(pattern, str, maskChar = "*") {
if (typeof str !== "string") {
throw new TypeError(`Expected second argument to be of type "string", got "${typeof str}".`);
}
const patterns = Array.isArray(pattern) ? pattern : [pattern];
let output = str;
patterns.forEach((curPattern) => {
if (typeof curPattern !== "string" && !(curPattern instanceof RegExp)) {
throw new TypeError(`Expected pattern to be of type "string" or "RegExp", got "${typeof curPattern}".`);
}
const regEx = new RegExp(curPattern);
let matchResults = regEx.exec(output);
while (matchResults) {
const [token] = matchResults;
output = output.slice(0, matchResults.index) + maskAll(token, maskChar) + output.slice(matchResults.index + token.length);
matchResults = regEx.exec(output);
}
});
return output;
}
export {
maskString as default
};
//# sourceMappingURL=mask.js.map