Total Complexity | 18 |
Complexity/F | 6 |
Lines of Code | 63 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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) { |
||
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 | default: |
||
27 | // explicitly doing nothing |
||
28 | } |
||
29 | return value instanceof Error ? Renderer.error(value, expanded) : Renderer.object(value, expanded) |
||
30 | }, |
||
31 | /** |
||
32 | * @param {Error} value |
||
33 | * @param {boolean} expanded Whether to return expanded or short representation |
||
34 | * @return {string} |
||
35 | */ |
||
36 | error: function (value, expanded) { |
||
37 | if (expanded) { |
||
38 | return [Renderer.error(value, false), 'Stack:', value.stack].join('\r\n') |
||
39 | } |
||
40 | if (DefaultHandlers.indexOf(value.toString) === -1) { |
||
41 | return '<' + value.toString() + '>' |
||
42 | } |
||
43 | return '<' + (value.name || '<unknown error>') + ': ' + (value.message || '<no message>') + '>' |
||
44 | }, |
||
45 | /** |
||
46 | * @param {object} value |
||
47 | * @param {boolean} expanded Whether to return expanded or short representation |
||
48 | * @return {string} |
||
49 | */ |
||
50 | object: function (value, expanded) { |
||
51 | if (value === null) { |
||
52 | return '<null>' |
||
53 | } |
||
54 | if (typeof value.toString === 'function' && DefaultHandlers.indexOf(value.toString) === -1 && !expanded) { |
||
55 | return value.toString() |
||
56 | } |
||
57 | return JSON.stringify(value) |
||
58 | } |
||
59 | } |
||
60 | |||
61 | module.exports = { |
||
62 | Renderer: Renderer |
||
63 | } |
||
64 |