Total Complexity | 17 |
Complexity/F | 5.67 |
Lines of Code | 61 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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
|
|||
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 |