bump package version to 3.0.3
[squeep-logger-json-console] / lib / data-sanitizers.js
1 'use strict';
2
3 /**
4 * Sanitizers replace data fields before logging.
5 * None are bundled, but here is an example of one.
6 */
7
8 /**
9 * Example sanitizer function.
10 * Sanitizers are called initially with `sanitize` false, to only determine
11 * whether the data object will be changed. If so, the sanitizer is then
12 * called again with `sanitize` true, but provided with a clone of the
13 * original data to update, to avoid leaking any applied changes back to the
14 * application.
15 * @param {Object} data
16 * @param {Boolean} sanitize
17 * @returns {Boolean} whether sanitizer is applicable to data
18 */
19 /* istanbul ignore next */
20 function _sanitizer(data, sanitize = true) {
21 let unclean = false;
22
23 const sensitiveFieldLength = data?.sensitiveField?.length;
24 if (sensitiveFieldLength) {
25 unclean = true;
26
27 if (sanitize) {
28 data.sensitiveField = '*'.repeat(sensitiveFieldLength);
29 }
30 }
31
32 return unclean;
33 }
34
35 module.exports = {};