summaryrefslogtreecommitdiff
path: root/src/YamlDumper.js
blob: ffc4e7dede5d375ed55294f16cc84a6d1c843b18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

/**
 * YamlDumper dumps JS variables to YAML strings.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
var YamlDumper = function(){};
YamlDumper.prototype =
{
	/**
	 * Dumps a JS value to YAML.
	 *
	 * @param	mixed	 input	The JS value
	 * @param	integer inline The level where you switch to inline YAML
	 * @param	integer indent The level o indentation indentation (used internally)
	 *
	 * @return string	The YAML representation of the JS value
	 */
	dump: function(input, inline, indent)
	{
		if ( inline == undefined ) inline = 0;
		if ( indent == undefined ) indent = 0;
		var output = '';
		var prefix = indent ? this.strRepeat(' ', indent) : '';
		var yaml;

		if ( inline <= 0 || !this.isObject(input) || this.isEmpty(input) )
		{
			yaml = new YamlInline();
			output += prefix + yaml.dump(input);
		}
		else
		{
			var isAHash = !this.arrayEquals(this.getKeys(input), this.range(0,input.length - 1));
			var willBeInlined;
			
			for ( var key in input )
			{
				if ( input.hasOwnProperty(key) )
				{
					willBeInlined = inline - 1 <= 0 || !this.isObject(input[key]) || this.isEmpty(input[key]);
					
					if ( isAHash ) yaml = new YamlInline();
					
					output += 
						prefix + '' +
						(isAHash ? yaml.dump(key)+':' : '-') + '' +
						(willBeInlined ? ' ' : "\n") + '' +
						this.dump(input[key], inline - 1, (willBeInlined ? 0 : indent + 2)) + '' +
						(willBeInlined ? "\n" : '');
				}
			}
		}

		return output;
	},
	
	strRepeat: function(str /* String */, count /* Integer */)
	{
		var i;
		var result = '';
		for ( i = 0; i < count; i++ ) result += str;
		return result;
	},
	
	isObject: function(input)
	{
		return this.isDefined(input) && typeof(input) == 'object';
	},
	
	isEmpty: function(input)
	{
		var ret = input == undefined || input == null || input == '' || input == 0 || input == "0" || input == false;
		if ( !ret && typeof(input) == "object" && !(input instanceof Array)){
			var propCount = 0;
			for ( var key in input )
				if ( input.hasOwnProperty(key) ) propCount++;
			ret = !propCount;
		}
		return ret;
	},
	
	isDefined: function(input)
	{
		return input != undefined && input != null;
	},
	
	getKeys: function(tab)
	{
		var ret = [];
		
		for ( var name in tab )
		{
			if ( tab.hasOwnProperty(name) )
			{
				ret.push(name);
			}
		}
		
		return ret;
	},
	
	range: function(start, end)
	{
		if ( start > end ) return [];
		
		var ret = [];
		
		for ( var i = start; i <= end; i++ )
		{
			ret.push(i);
		}
		
		return ret;
	},
	
	arrayEquals: function(a,b)
	{
		if ( a.length != b.length ) return false;
		
		var len = a.length;
		
		for ( var i = 0; i < len; i++ )
		{
			if ( a[i] != b[i] ) return false;
		}
		
		return true;
	}
};