initial commit
[squeep-authentication-module] / lib / common.js
diff --git a/lib/common.js b/lib/common.js
new file mode 100644 (file)
index 0000000..c0aaf37
--- /dev/null
@@ -0,0 +1,25 @@
+'use strict';
+
+const { common } = require('@squeep/api-dingus');
+
+
+/**
+ * Recursively freeze an object.
+ * @param {Object} o
+ * @returns {Object}
+ */
+const freezeDeep = (o) => {
+  Object.freeze(o);
+  Object.getOwnPropertyNames(o).forEach((prop) => {
+    if (Object.hasOwnProperty.call(o, prop)
+    &&  ['object', 'function'].includes(typeof o[prop]) // eslint-disable-line security/detect-object-injection
+    &&  !Object.isFrozen(o[prop])) { // eslint-disable-line security/detect-object-injection
+      return freezeDeep(o[prop]); // eslint-disable-line security/detect-object-injection
+    }
+  });
+  return o;
+}
+
+module.exports = Object.assign(Object.create(common), {
+  freezeDeep,
+});
\ No newline at end of file