redact-object
Version:
86 lines (85 loc) • 3.74 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = redact;
/* eslint-disable @typescript-eslint/no-explicit-any */
var lodash_isplainobject_1 = __importDefault(require("lodash.isplainobject"));
// Yoinked from lodash to save dependencies
function isObject(value) {
var type = typeof value;
return value !== null && (type === 'object' || type === 'function');
}
/**
* Checks for match
*
* @param keywords A list of keywords to look for
* @param key The string to check
* @param strict Use strict case if true
* @param partial Use partial matching if true
* @return True for match or false
*/
function isKeywordMatch(keywords, key, strict, partial) {
if (strict === void 0) { strict = false; }
if (partial === void 0) { partial = false; }
return keywords.some(function (keyword) {
var keyMatch = strict ? key : key.toLowerCase();
var keywordMatch = strict ? keyword : keyword.toLowerCase();
return partial ? keyMatch.indexOf(keywordMatch) !== -1 : keyMatch === keywordMatch;
});
}
/**
* Parses an object and redacts any keys listed in keywords
*
* @param target The target object to scan for redactable items
* @param keywords A list of members to redact
* @param replaceVal Optional custom replace value or function replacer
* @param config Optional config
* {
* partial: boolean, do partial matches, default false
* strict: boolean, do strict key matching, default true
* ignoreUnknown: boolean, ignore unknown types instead of error, default false
* }
* @return the new redacted object
*/
function redact(target, keywords, replaceVal, config) {
config = config || {};
var partial = Object.prototype.hasOwnProperty.call(config, 'partial') ? config.partial : true;
var strict = Object.prototype.hasOwnProperty.call(config, 'strict') ? config.strict : true;
var ignoreUnknown = Object.prototype.hasOwnProperty.call(config, 'ignoreUnknown') ? config.ignoreUnknown : false;
if (!isObject(target)) {
// If it's not an object then it's a primitive. Nothing to redact.
return target;
}
else if (Array.isArray(target)) {
// Create a new array with each value having been redacted
// Redact each value of the array.
return target.map(function (val) { return redact(val, keywords, replaceVal, config); });
}
else if ((0, lodash_isplainobject_1.default)(target)) {
return Object.keys(target).reduce(function (newObj, key) {
var isMatch = isKeywordMatch(keywords, key, strict, partial);
if (isMatch) {
newObj[key] = typeof replaceVal === 'function'
? replaceVal(target[key], key) : replaceVal || '[ REDACTED ]';
}
else {
newObj[key] = redact(target[key], keywords, replaceVal, config);
}
return newObj;
}, {});
}
// Redaction only works on arrays, plain objects, and primitives.
if (ignoreUnknown) {
// ignore the unknown type instead of throwing an error
return target;
}
else {
var targetType = typeof target;
if (targetType === 'object' && !(0, lodash_isplainobject_1.default)(target)) {
targetType += ' (not plain)';
}
throw new Error("Unsupported value type for redaction: ".concat(targetType));
}
}
;