Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor(remove-ramda): change unneeded function to logical OR operator
As requested
  • Loading branch information
christian-hawk committed Nov 24, 2020
1 parent 925146c commit 107e944
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 70 deletions.
11 changes: 3 additions & 8 deletions server/utils/logging.js
Expand Up @@ -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',
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -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...
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 1 addition & 17 deletions server/utils/misc.js
Expand Up @@ -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))
Expand Down Expand Up @@ -159,6 +144,5 @@ module.exports = {
getJWT: getJWT,
verifyJWT: verifyJWT,
encrypt: encrypt,
randomSecret: randomSecret,
assignDefaultValueIfUnexistant: assignDefaultValueIfUnexistant
randomSecret: randomSecret
}
45 changes: 0 additions & 45 deletions test/misc.test.js
Expand Up @@ -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)
Expand Down

0 comments on commit 107e944

Please sign in to comment.