1 |
|
2 | Utils = require './Utils'
|
3 | Inline = require './Inline'
|
4 |
|
5 |
|
6 |
|
7 | class Dumper
|
8 |
|
9 |
|
10 | @indentation: 4
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | dump: (input, inline = 0, indent = 0, exceptionOnInvalidType = false, objectEncoder = null) ->
|
24 | output = ''
|
25 | prefix = (if indent then Utils.strRepeat(' ', indent) else '')
|
26 |
|
27 | if inline <= 0 or typeof(input) isnt 'object' or input instanceof Date or Utils.isEmpty(input)
|
28 | output += prefix + Inline.dump(input, exceptionOnInvalidType, objectEncoder)
|
29 |
|
30 | else
|
31 | if input instanceof Array
|
32 | for value in input
|
33 | willBeInlined = (inline - 1 <= 0 or typeof(value) isnt 'object' or Utils.isEmpty(value))
|
34 |
|
35 | output +=
|
36 | prefix +
|
37 | '-' +
|
38 | (if willBeInlined then ' ' else "\n") +
|
39 | @dump(value, inline - 1, (if willBeInlined then 0 else indent + @indentation), exceptionOnInvalidType, objectEncoder) +
|
40 | (if willBeInlined then "\n" else '')
|
41 |
|
42 | else
|
43 | for key, value of input
|
44 | willBeInlined = (inline - 1 <= 0 or typeof(value) isnt 'object' or Utils.isEmpty(value))
|
45 |
|
46 | output +=
|
47 | prefix +
|
48 | Inline.dump(key, exceptionOnInvalidType, objectEncoder) + ':' +
|
49 | (if willBeInlined then ' ' else "\n") +
|
50 | @dump(value, inline - 1, (if willBeInlined then 0 else indent + @indentation), exceptionOnInvalidType, objectEncoder) +
|
51 | (if willBeInlined then "\n" else '')
|
52 |
|
53 | return output
|
54 |
|
55 |
|
56 | module.exports = Dumper
|