@handy-common-utils/misc-utils
Version:
Miscellaneous utilities
70 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathAwareReplacer = pathAwareReplacer;
exports.pathBasedReplacer = pathBasedReplacer;
/**
* Build a replacer function that can be passed to JSON.stringify(...).
* @param replacer The actual replacer function which could utilise additional information.
* @param options Options to control whether the pathArray and ancestors parameters would have values populated.
* By default all information available would be populated.
* There is no need to specify options unless you are extremely concerned about performance, for example if you need to frequently stringify 500MB objects.
* @param options.pathArray When false, pathArray would be an empty array. This would remove the need to construct a pathArray from the path string.
* @param options.ancestors When false, ancestors would be an empty array.
* @returns The replacer function that can be passed to JSON.stringify(...).
*/
function pathAwareReplacer(replacer, options) {
const currentPath = [];
const ancestors = [];
return function (key, value) {
if (typeof key === 'string' && key === '') { // root object
currentPath.length = 0;
ancestors.length = 0;
}
else {
let i = ancestors.length - 1;
for (; i >= 0; i--) {
if (ancestors[i] === this) {
ancestors.length = i + 1;
currentPath.length = i;
break;
}
}
if (i < 0) {
ancestors.push(this);
}
currentPath.push(key);
}
return replacer(key, value, currentPath.join('.'), this, (options === null || options === void 0 ? void 0 : options.pathArray) === false ? [] : [...currentPath], (options === null || options === void 0 ? void 0 : options.ancestors) === false ? [] : [...ancestors]);
};
}
/**
* Create a replacer function for JSON.stringify(...) from an array of path based rules.
* This function is useful for creating masking replacers which can apply masking based on the path of the property.
* @example
* import { mask, maskAll, maskEmail, maskFullName, pathBasedReplacer } from '@handy-common-utils/misc-utils';
* console.log(JSON.stringify(obj, pathBasedReplacer([
* [/.*\.x-api-key$/, maskAll],
* [/.*customer\.name$/, maskFullName],
* [/.*customer\..*[eE]mail$/, maskEmail],
* [/.*\.zip$/, (value: string) => value.slice(0, 3) + 'XX'],
* [/.*\.cc$/, () => undefined],
* [/.*\.ssn$/, mask],
* ])));
* @param rules Array of rules: if the regular expression tests true with the property path, the replacer function will be applied on the value
* @returns The replacer function built from those path based rules. It also has a `rules` property storing all the path based rules provided as inputs.
*/
function pathBasedReplacer(rules) {
const replacer = pathAwareReplacer(function (_key, value, path) {
for (const [regexp, replace] of rules) {
if (regexp.test(path)) {
return replace(value);
}
}
return value;
}, {
pathArray: false,
ancestors: false,
});
return Object.assign(replacer, { rules });
}
//# sourceMappingURL=stringify-replacer.js.map