simple-redact
Version:
A simple redact lib
53 lines (52 loc) • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./lib/utils");
var buildConfig_1 = require("./lib/buildConfig");
var ConfigRedact_1 = require("./typings/ConfigRedact");
var Redacter = /** @class */ (function () {
function Redacter(redactMethod) {
var _this = this;
if (redactMethod === void 0) { redactMethod = ConfigRedact_1.RedactMethod.REDACT; }
this.internalRedact = function (obj, toRedact, result) {
var availableKeys = Object.keys(result);
toRedact.forEach(function (fieldToRedact) {
var fieldToManipulate = fieldToRedact.field;
if (availableKeys.includes(fieldToManipulate)) {
if ((0, utils_1.isObject)(result[fieldToRedact.field]) &&
fieldToRedact.data &&
fieldToRedact.data.length > 0) {
_this.internalRedact(obj[fieldToRedact.field], fieldToRedact.data, result[fieldToRedact.field]);
return;
}
result[fieldToRedact.field] = _this.applyRedactMethod(result[fieldToRedact.field], fieldToRedact.type);
return;
}
});
return;
};
this.simpleRedact = function (obj, config) {
var result = JSON.parse(JSON.stringify(obj));
var toRedact = (0, buildConfig_1.buildConfig)(config);
_this.internalRedact(obj, toRedact, result);
return result;
};
this.redact = function (obj, config) {
var result = JSON.parse(JSON.stringify(obj));
_this.internalRedact(obj, config, result);
return result;
};
this.redactMethod = redactMethod;
}
Redacter.prototype.applyRedactMethod = function (fieldToRedact, method) {
fieldToRedact = String(fieldToRedact);
var methodToApply = method ? method : this.redactMethod;
if (methodToApply === ConfigRedact_1.RedactMethod.MASK) {
return '*'.repeat(fieldToRedact.length);
}
if (methodToApply === ConfigRedact_1.RedactMethod.REDACT) {
return '[REDACT]';
}
};
return Redacter;
}());
exports.default = Redacter;