Completed
Push — dev ( 763fa0...b49d63 )
by Fike
29s
created

Renderer.object   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
nc 3
dl 0
loc 9
rs 8.8571
nop 2
1
var DefaultHandlers = [
2
  Object.prototype.toString,
3
  Array.prototype.toString,
4
  Function.prototype.toString,
5
  Error.prototype.toString
6
]
7
8
var Renderer = {
9
  /**
10
   * @param {*} value
11
   * @param {boolean} expanded Whether to return expanded or short representation
12
   * @return {string}
13
   */
14
  any: function (value, expanded) {
15
    var type = typeof value
16
    switch (type) {
1 ignored issue
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
17
      case 'function':
18
        return value.name ? '<Function: ' + value.name + '>' : '<Function>'
19
      case 'undefined':
20
        return '<undefined>'
21
      case 'number':
22
      case 'boolean':
23
      case 'string':
24
      case 'symbol':
25
        return value
26
    }
27
    return value instanceof Error ? Renderer.error(value, expanded) : Renderer.object(value, expanded)
28
  },
29
  /**
30
   * @param {Error} value
31
   * @param {boolean} expanded Whether to return expanded or short representation
32
   * @return {string}
33
   */
34
  error: function (value, expanded) {
35
    if (expanded) {
36
      return [Renderer.error(value, false), 'Stack:', value.stack].join('\r\n')
37
    }
38
    if (DefaultHandlers.indexOf(value.toString) === -1) {
39
      return '<' + value.toString() + '>'
40
    }
41
    return '<' + (value.name || '<unknown error>') + ': ' + (value.message || '<no message>') + '>'
42
  },
43
  /**
44
   * @param {object} value
45
   * @param {boolean} expanded Whether to return expanded or short representation
46
   * @return {string}
47
   */
48
  object: function (value, expanded) {
49
    if (value === null) {
50
      return '<null>'
51
    }
52
    if (typeof value.toString === 'function' && DefaultHandlers.indexOf(value.toString) === -1 && !expanded) {
53
      return value.toString()
54
    }
55
    return JSON.stringify(value)
56
  }
57
}
58
59
module.exports = {
60
  Renderer: Renderer
61
}
62