From 107e9445a6ffdd7b1bc0a30cd78f9f350fcdd6f5 Mon Sep 17 00:00:00 2001 From: Christian Eland Date: Tue, 24 Nov 2020 17:51:17 -0300 Subject: [PATCH] refactor(remove-ramda): change unneeded function to logical OR operator As requested --- server/utils/logging.js | 11 +++------- server/utils/misc.js | 18 +---------------- test/misc.test.js | 45 ----------------------------------------- 3 files changed, 4 insertions(+), 70 deletions(-) diff --git a/server/utils/logging.js b/server/utils/logging.js index 6314121a..e1e6c7fb 100644 --- a/server/utils/logging.js +++ b/server/utils/logging.js @@ -8,9 +8,7 @@ const misc = require('./misc') const format = winston.format const path = require('path') -const dir = misc.assignDefaultValueIfUnexistant( - path.join(__dirname, 'logs'), process.env.NODE_LOGGING_DIR -) +const dir = process.env.NODE_LOGGING_DIR || path.join(__dirname, 'logs') const defaultLogOptions = { filename: dir + '/passport-%DATE%.log', @@ -57,7 +55,7 @@ function configure (cfg) { if (h !== prevConfigHash) { prevConfigHash = h - const level = misc.assignDefaultValueIfUnexistant('info', cfg.level) + const level = cfg.level || 'info' // Remove console log + rotary file transports R.forEach(l => logger.remove(l), R.filter(R.complement(R.isNil), [transport, consoleTransport])) @@ -103,7 +101,6 @@ function configure (cfg) { } stompClient = new Stomp(MQDetails) stompClient.connect( - // eslint-disable-next-line no-unused-vars sessionId => logger.info('Connected to STOMP server') //, The error callback is called successively until the connection succeeds... @@ -134,9 +131,7 @@ function log2 (level, msg) { level = level.toLowerCase() level = R.includes(level, levels) ? level : 'info' - if (!msg && msg !== '') { - msg = '' - } + msg = msg || '' // Convert arguments to a real array (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments#Description) const args = [].slice.call(arguments) diff --git a/server/utils/misc.js b/server/utils/misc.js index 03818539..b40b2210 100644 --- a/server/utils/misc.js +++ b/server/utils/misc.js @@ -10,21 +10,6 @@ const randomSecret = () => { return buf.toString('hex') } -/** - * Assigns defaultValue to object if object value is undefined, null or NaN - * @param {*} defaultValue - * @param {*} object - */ -const assignDefaultValueIfUnexistant = (defaultValue, object) => { - if (!defaultValue) { - throw new Error('defaultValue cannot be undefined, NaN or null') - } - if (!object) { - object = defaultValue - } - return object -} - const isObject = x => !R.isNil(x) && !Array.isArray(x) && typeof x === 'object' const pipePromise_ = R.reduce((p, fn) => Promise.resolve(p).then(fn)) @@ -159,6 +144,5 @@ module.exports = { getJWT: getJWT, verifyJWT: verifyJWT, encrypt: encrypt, - randomSecret: randomSecret, - assignDefaultValueIfUnexistant: assignDefaultValueIfUnexistant + randomSecret: randomSecret } diff --git a/test/misc.test.js b/test/misc.test.js index 6bd8032b..928ac630 100644 --- a/test/misc.test.js +++ b/test/misc.test.js @@ -7,51 +7,6 @@ const sinon = require('sinon') const assert = chai.assert -describe('misc.getDefaultValueIfUnexistant', () => { - it('shoud exist', () => { - assert.exists(misc.assignDefaultValueIfUnexistant) - }) - it('should be a function', () => { - assert.isFunction(misc.assignDefaultValueIfUnexistant) - }) - - it('should throw error if object if defaultValue is unexistant', () => { - const object = {} - let unexistant - assert.throws(() => { - misc.assignDefaultValueIfUnexistant(unexistant, object) - }, - 'defaultValue cannot be undefined, NaN or null') - }) - - it('should not throw error if defaultValue exists', () => { - const object = {} - const existant = 'existantValue' - assert.doesNotThrow(() => { - misc.assignDefaultValueIfUnexistant(existant, object) - }, - 'defaultValue cannot be undefined, NaN or null') - }) - - it('should return defaultValue if object is unexistant', () => { - let unexistantObject - const defaultValue = 'myDefaultValue' - assert.strictEqual( - misc.assignDefaultValueIfUnexistant(defaultValue, unexistantObject), - defaultValue - ) - }) - - it('should return object if object exists', () => { - const existantObject = { exists: true } - const defaultValue = 'anyNonEmptyValue' - assert.strictEqual( - misc.assignDefaultValueIfUnexistant(defaultValue, existantObject), - existantObject - ) - }) -}) - describe('misc.randomSecret', () => { it('should exist', () => { assert.exists(misc.randomSecret)