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

lib/Schema/Defaults/Deserializer.js   A

Complexity

Total Complexity 6
Complexity/F 3

Size

Lines of Code 31
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 6
c 1
b 0
f 0
nc 2
mnd 1
bc 6
fnc 2
dl 0
loc 31
rs 10
bpm 3
cpm 3
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A module.exports.factory 0 22 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