|
26 | 26 | setOptionDefaults(); |
27 | 27 |
|
28 | 28 | // Error checking for root element |
29 | | - if (typeof root !== "string") |
| 29 | + if (typeof root !== "string") { |
30 | 30 | throw new Error("root element must be a string"); |
| 31 | + } |
31 | 32 |
|
32 | 33 | // Error checking and variable initialization for options |
33 | 34 | if (typeof options === "object" && options !== null) { |
34 | 35 | if ("declaration" in options) { |
35 | 36 | if ("include" in options.declaration) { |
36 | | - if (typeof options.declaration.include === "boolean") |
| 37 | + if (typeof options.declaration.include === "boolean") { |
37 | 38 | xmlDeclaration = options.declaration.include; |
38 | | - else |
| 39 | + } |
| 40 | + else { |
39 | 41 | throw new Error("declaration.include option must be a boolean"); |
| 42 | + } |
40 | 43 | } |
41 | 44 |
|
42 | 45 | if ("encoding" in options.declaration) { |
43 | | - |
44 | | - if (typeof options.declaration.encoding === "string" || options.declaration.encoding === null) |
| 46 | + if (typeof options.declaration.encoding === "string" || options.declaration.encoding === null) { |
45 | 47 | xmlEncoding = options.declaration.encoding; |
46 | | - else |
| 48 | + } |
| 49 | + else { |
47 | 50 | throw new Error("declaration.encoding option must be a string or null"); |
| 51 | + } |
48 | 52 | } |
49 | 53 | } |
50 | 54 | if ("wrapArray" in options) { |
51 | | - if ("enabled" in options.wrapArray) |
52 | | - if (typeof options.wrapArray.enabled === "boolean") |
| 55 | + if ("enabled" in options.wrapArray) { |
| 56 | + if (typeof options.wrapArray.enabled === "boolean") { |
53 | 57 | wrapArray = options.wrapArray.enabled; |
54 | | - else |
| 58 | + } |
| 59 | + else { |
55 | 60 | throw new Error("wrapArray.enabled option must be a boolean"); |
56 | | - |
57 | | - if ("elementName" in options.wrapArray) |
58 | | - if (typeof options.wrapArray.elementName === "string") |
| 61 | + } |
| 62 | + } |
| 63 | + if ("elementName" in options.wrapArray) { |
| 64 | + if (typeof options.wrapArray.elementName === "string") { |
59 | 65 | wrapArrayItem = options.wrapArray.elementName; |
60 | | - else |
| 66 | + } |
| 67 | + else { |
61 | 68 | throw new Error("wrapArray.elementName option must be a boolean"); |
| 69 | + } |
| 70 | + } |
62 | 71 | } |
63 | 72 | if ("useCDATA" in options) { |
64 | | - if (typeof options.useCDATA === "boolean") |
| 73 | + if (typeof options.useCDATA === "boolean") { |
65 | 74 | useCDATA = options.useCDATA; |
66 | | - else |
| 75 | + } |
| 76 | + else { |
67 | 77 | throw new Error("useCDATA option must be a boolean"); |
| 78 | + } |
68 | 79 | } |
69 | 80 | if ("callFunctions" in options) { |
70 | | - if (typeof options.callFunctions === "boolean") |
| 81 | + if (typeof options.callFunctions === "boolean") { |
71 | 82 | callFunctions = options.callFunctions; |
72 | | - else |
| 83 | + } |
| 84 | + else { |
73 | 85 | throw new Error("callFunctions option must be a boolean"); |
| 86 | + } |
74 | 87 | } |
75 | 88 | if ("attributeString" in options) { |
76 | | - if (typeof options.attributeString === "string") |
| 89 | + if (typeof options.attributeString === "string") { |
77 | 90 | attributeString = options.attributeString; |
78 | | - else |
| 91 | + } |
| 92 | + else { |
79 | 93 | throw new Error("attributeString option must be a string"); |
| 94 | + } |
80 | 95 | } |
81 | 96 | if ("valueString" in options) { |
82 | | - if (typeof options.valueString === "string") |
| 97 | + if (typeof options.valueString === "string") { |
83 | 98 | valueString = options.valueString; |
84 | | - else |
| 99 | + } |
| 100 | + else { |
85 | 101 | throw new Error("valueString option must be a string"); |
| 102 | + } |
86 | 103 | } |
87 | 104 | if ("prettyPrinting" in options) { |
88 | 105 | if ("enabled" in options.prettyPrinting) { |
89 | | - if (typeof options.prettyPrinting.enabled === "boolean") |
| 106 | + if (typeof options.prettyPrinting.enabled === "boolean") { |
90 | 107 | prettyPrinting = options.prettyPrinting.enabled; |
91 | | - else |
| 108 | + } |
| 109 | + else { |
92 | 110 | throw new Error("prettyPrinting.enabled option must be a boolean"); |
| 111 | + } |
93 | 112 | } |
94 | 113 |
|
95 | 114 | if ("indentString" in options.prettyPrinting) { |
96 | | - if (typeof options.prettyPrinting.indentString === "string") |
| 115 | + if (typeof options.prettyPrinting.indentString === "string") { |
97 | 116 | indentString = options.prettyPrinting.indentString; |
98 | | - else |
| 117 | + } |
| 118 | + else { |
99 | 119 | throw new Error("prettyPrinting.indentString option must be a string"); |
| 120 | + } |
100 | 121 | } |
101 | 122 | } |
102 | 123 | if ("convertMap" in options) { |
103 | | - if (Object.prototype.toString.call(options.convertMap) === "[object Object]") |
| 124 | + if (Object.prototype.toString.call(options.convertMap) === "[object Object]") { |
104 | 125 | convertMap = options.convertMap; |
105 | | - else |
| 126 | + } |
| 127 | + else { |
106 | 128 | throw new Error("convertMap option must be an object"); |
| 129 | + } |
107 | 130 | } |
108 | 131 | } |
109 | 132 |
|
110 | 133 | // Error checking and variable initialization for data |
111 | | - if (typeof data !== "string" && typeof data !== "object") |
| 134 | + if (typeof data !== "string" && typeof data !== "object") { |
112 | 135 | throw new Error("data must be an object or a string"); |
| 136 | + } |
113 | 137 |
|
114 | | - if (typeof data === "string") |
| 138 | + if (typeof data === "string") { |
115 | 139 | data = JSON.parse(data); |
| 140 | + } |
116 | 141 |
|
117 | 142 | var tempData = {}; |
118 | 143 | tempData[root] = data; // Add root element to object |
|
126 | 151 | var xml = arguments[1] || ""; |
127 | 152 | var level = arguments[2] || 0; |
128 | 153 |
|
| 154 | + var i; |
| 155 | + var tempObject = {}; |
| 156 | + |
129 | 157 | for (var property in object) { |
130 | 158 | // Arrays |
131 | 159 | if (Object.prototype.toString.call(object[property]) === "[object Array]") { |
132 | 160 | if (wrapArray) { |
133 | 161 | // Create separate XML elements for each array element, but wrap all elements in a single element |
134 | 162 | xml += addBreak(addIndent("<" + property + ">", level)); |
135 | | - for (var i = 0; i < object[property].length; i++) { |
136 | | - var tempObject = {}; |
| 163 | + for (i = 0; i < object[property].length; i++) { |
| 164 | + tempObject = {}; |
137 | 165 | tempObject[wrapArrayItem] = object[property][i]; |
138 | 166 | xml = toXML(tempObject, xml, level + 1); |
139 | 167 | } |
140 | 168 | xml += addBreak(addIndent("</" + property + ">", level)); |
141 | 169 | } |
142 | 170 | else { |
143 | 171 | // Create separate XML elements for each array element |
144 | | - for (var i = 0; i < object[property].length; i++) { |
145 | | - var tempObject = {}; |
| 172 | + for (i = 0; i < object[property].length; i++) { |
| 173 | + tempObject = {}; |
146 | 174 | tempObject[property] = object[property][i]; |
147 | 175 |
|
148 | 176 | xml = toXML(tempObject, xml, level); |
|
158 | 186 | var lengthExcludingAttributes = Object.keys(object[property]).length; |
159 | 187 | if (Object.prototype.toString.call(object[property][attributeString]) === "[object Object]") { |
160 | 188 | lengthExcludingAttributes -= 1; |
161 | | - for (var attribute in object[property][attributeString]) |
162 | | - xml += " " + attribute + "=\"" + toString(object[property][attributeString][attribute]) + "\""; |
| 189 | + for (var attribute in object[property][attributeString]) { |
| 190 | + if (object[property][attributeString].hasOwnProperty(attribute)) { |
| 191 | + xml += " " + attribute + "=\"" + toString(object[property][attributeString][attribute]) + |
| 192 | + "\""; |
| 193 | + } |
| 194 | + } |
163 | 195 | } |
164 | 196 |
|
165 | | - if (lengthExcludingAttributes === 0) // Empty object |
| 197 | + if (lengthExcludingAttributes === 0) { // Empty object |
166 | 198 | xml += addBreak("/>"); |
167 | | - else if (lengthExcludingAttributes === 1 && valueString in object[property]) // Value string only |
| 199 | + } |
| 200 | + else if (lengthExcludingAttributes === 1 && valueString in object[property]) { // Value string only |
168 | 201 | xml += addBreak(">" + toString(object[property][valueString], true) + "</" + property + ">"); |
| 202 | + } |
169 | 203 | else { // Object with properties |
170 | 204 | xml += addBreak(">"); |
171 | 205 |
|
172 | 206 | // Create separate object for each property and pass to this function |
173 | 207 | for (var subProperty in object[property]) { |
174 | 208 | if (subProperty !== attributeString) { |
175 | | - var tempObject = {}; |
| 209 | + tempObject = {}; |
176 | 210 | tempObject[subProperty] = object[property][subProperty]; |
177 | 211 |
|
178 | 212 | xml = toXML(tempObject, xml, level + 1); |
|
194 | 228 | xml = xml.replace(/\s+$/g, ""); |
195 | 229 |
|
196 | 230 | // Add XML declaration |
197 | | - if (xmlDeclaration) |
198 | | - if (xmlEncoding === null) |
| 231 | + if (xmlDeclaration) { |
| 232 | + if (xmlEncoding === null) { |
199 | 233 | xml = addBreak("<?xml version=\"" + xmlVersion + "\"?>") + xml; |
200 | | - else |
| 234 | + } |
| 235 | + else { |
201 | 236 | xml = addBreak("<?xml version=\"" + xmlVersion + "\" encoding=\"" + xmlEncoding + "\"?>") + xml; |
| 237 | + } |
| 238 | + } |
202 | 239 | } |
203 | 240 |
|
204 | 241 | return xml; |
|
227 | 264 | var toString = function(data) { |
228 | 265 | // Recursive function used to handle nested functions |
229 | 266 | var functionHelper = function(data) { |
230 | | - if (Object.prototype.toString.call(data) === "[object Function]") |
| 267 | + if (Object.prototype.toString.call(data) === "[object Function]") { |
231 | 268 | return functionHelper(data()); |
232 | | - else |
| 269 | + } |
| 270 | + else { |
233 | 271 | return data; |
| 272 | + } |
234 | 273 | }; |
235 | 274 |
|
236 | 275 | // Convert map |
|
0 commit comments