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 /**
21 *
22 * @param {object} data data object to mogrify
23 * @param {boolean} sanitize mogrify if truue
24 * @returns {boolean} whether data would be mogrified
25 */
26 function _sanitizer(data, sanitize = true) {
27 let unclean = false;
28
29 const sensitiveFieldLength = data?.sensitiveField?.length;
30 if (sensitiveFieldLength) {
31 unclean = true;
32
33 if (sanitize) {
34 data.sensitiveField = '*'.repeat(sensitiveFieldLength);
35 }
36 }
37
38 return unclean;
39 }
40
41 module.exports = {};