Completed
Push — dev ( 41defb...db632c )
by Fike
36s
created

module.exports.factory   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
nc 4
dl 0
loc 18
rs 8.8571
nop 1
1
var Slf4j = require('@ama-team/voxengine-sdk').Logger.Slf4j
2
3
module.exports = {
4
  /**
5
   * @param {LoggerOptions} [options]
6
   *
7
   * @return {Function}
8
   */
9
  factory: function (options) {
10
    var name = 'ama-team.vsf.schema.defaults.deserializer'
11
    var logger = Slf4j.factory(options, name)
12
    return function (input) {
13
      if (!input || input === '') {
14
        logger.debug('Provided input is a falsey value, returning empty object')
15
        return {}
16
      }
17
      if (typeof input !== 'string') {
18
        logger.debug('Provided input is not a string, returning object with ' +
19
          'input as $.customData')
20
        return {customData: input}
21
      }
22
      try {
23
        return JSON.parse(input)
24
      } catch (e) {
25
        logger.warn('Failed to deserialize JSON from {}, returning object ' +
26
          'with input as $.customData', input)
27
        return {customData: input}
28
      }
29
    }
30
  }
31
}
32