bump package version to 3.0.3
[squeep-logger-json-console] / lib / data-sanitizers.js
index e4d0f3c4bb8238a2d5f5bcd6595440c889139559..bc099fbf6008359c597cd86c0a40f0229b336c2b 100644 (file)
@@ -1,28 +1,41 @@
 'use strict';
 
 /**
- * Here are some sanitizers to replace data fields before logging.
- * Well, really, this just documents the expected format.
+ * Sanitizers replace data fields before logging.
+ * None are bundled, but here is an example of one.
  */
 
 /**
- * template for sanitizers
- * @param {Object} data
- * @param {Boolean} sanitize
- * @returns {Boolean}
+ * Example sanitizer function.
+ * Sanitizers are called initially with `sanitize` false, to only determine
+ * whether the data object will be changed.  If so, the sanitizer is then
+ * called again with `sanitize` true, but provided with a clone of the
+ * original data to update, to avoid leaking any applied changes back to the
+ * application.
+ * @param {object} data
+ * @param {boolean} sanitize
+ * @returns {boolean} whether sanitizer is applicable to data
  */
 /* istanbul ignore next */
+/**
+ *
+ * @param {object} data data object to mogrify
+ * @param {boolean} sanitize mogrify if truue
+ * @returns {boolean} whether data would be mogrified
+ */
 function _sanitizer(data, sanitize = true) {
   let unclean = false;
 
-  if (undefined) { // eslint-disable-line no-constant-condition
+  const sensitiveFieldLength = data?.sensitiveField?.length;
+  if (sensitiveFieldLength) {
     unclean = true;
-  }
-  if (unclean && sanitize) {
-    data;
+
+    if (sanitize) {
+      data.sensitiveField = '*'.repeat(sensitiveFieldLength);
+    }
   }
 
   return unclean;
 }
 
-module.exports = {};
\ No newline at end of file
+module.exports = {};