ts-logs
Version:
This package provide a skd for audit and manager logs in nodejs and express
49 lines • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MaskAttribute = void 0;
const mask_sub_key_util_1 = require("./mask-sub-key.util");
const MaskAttribute = (mask, object) => {
let result = object;
const key = mask.key;
const nDigitsDisplay = (typeof mask?.nCharDisplay === 'undefined') ? 0 : mask.nCharDisplay;
const regexPattern = `.(?=.{${nDigitsDisplay}})`;
const regex = new RegExp(regexPattern, 'g');
const hasObject = (typeof object !== 'undefined');
const isArr = (Array.isArray(object));
const isDate = (object instanceof Date);
const isObj = (typeof object === 'object');
const isFn = (typeof object === 'function');
const keys = (hasObject && !isArr && !isDate && isObj && !isFn) ? Object.keys(object) : [];
let i = 0;
while (object && keys[i]) {
const currentKey = keys[i];
const keyValue = object?.[currentKey] ?? null;
const isTargetKey = currentKey === key;
const isStr = (typeof keyValue === 'string');
const isNum = (typeof keyValue === 'number');
if (isTargetKey && (isStr || isNum)) {
const val = isNum ? keyValue.toString() : keyValue;
const hash = val.replace(regex, '*');
result = Object.assign({}, result, { [currentKey]: hash });
i++;
continue;
}
else if (keyValue && (typeof keyValue === 'object')) {
if (Array.isArray(keyValue)) {
const subs = keyValue.map((el) => (0, exports.MaskAttribute)(mask, el));
result = Object.assign({}, result, { [currentKey]: subs });
}
else if ((typeof keyValue !== 'function') && !(keyValue instanceof Date)) {
const sub = (0, exports.MaskAttribute)(mask, keyValue);
result = Object.assign({}, result, { [currentKey]: sub });
}
}
i++;
}
result = (!isArr && !isDate && isObj && !isFn) ? Object.assign({}, object, result) : object;
result = (0, mask_sub_key_util_1.default)(key, result, (val) => val.replace(regex, '*'));
return result;
};
exports.MaskAttribute = MaskAttribute;
exports.default = exports.MaskAttribute;
//# sourceMappingURL=mask-attribute.util.js.map