Skip to content

Commit f98106c

Browse files
Style changes to make JSHint happy
1 parent 84ce2f0 commit f98106c

1 file changed

Lines changed: 82 additions & 43 deletions

File tree

lib/js2xmlparser.js

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,93 +26,118 @@
2626
setOptionDefaults();
2727

2828
// Error checking for root element
29-
if (typeof root !== "string")
29+
if (typeof root !== "string") {
3030
throw new Error("root element must be a string");
31+
}
3132

3233
// Error checking and variable initialization for options
3334
if (typeof options === "object" && options !== null) {
3435
if ("declaration" in options) {
3536
if ("include" in options.declaration) {
36-
if (typeof options.declaration.include === "boolean")
37+
if (typeof options.declaration.include === "boolean") {
3738
xmlDeclaration = options.declaration.include;
38-
else
39+
}
40+
else {
3941
throw new Error("declaration.include option must be a boolean");
42+
}
4043
}
4144

4245
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) {
4547
xmlEncoding = options.declaration.encoding;
46-
else
48+
}
49+
else {
4750
throw new Error("declaration.encoding option must be a string or null");
51+
}
4852
}
4953
}
5054
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") {
5357
wrapArray = options.wrapArray.enabled;
54-
else
58+
}
59+
else {
5560
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") {
5965
wrapArrayItem = options.wrapArray.elementName;
60-
else
66+
}
67+
else {
6168
throw new Error("wrapArray.elementName option must be a boolean");
69+
}
70+
}
6271
}
6372
if ("useCDATA" in options) {
64-
if (typeof options.useCDATA === "boolean")
73+
if (typeof options.useCDATA === "boolean") {
6574
useCDATA = options.useCDATA;
66-
else
75+
}
76+
else {
6777
throw new Error("useCDATA option must be a boolean");
78+
}
6879
}
6980
if ("callFunctions" in options) {
70-
if (typeof options.callFunctions === "boolean")
81+
if (typeof options.callFunctions === "boolean") {
7182
callFunctions = options.callFunctions;
72-
else
83+
}
84+
else {
7385
throw new Error("callFunctions option must be a boolean");
86+
}
7487
}
7588
if ("attributeString" in options) {
76-
if (typeof options.attributeString === "string")
89+
if (typeof options.attributeString === "string") {
7790
attributeString = options.attributeString;
78-
else
91+
}
92+
else {
7993
throw new Error("attributeString option must be a string");
94+
}
8095
}
8196
if ("valueString" in options) {
82-
if (typeof options.valueString === "string")
97+
if (typeof options.valueString === "string") {
8398
valueString = options.valueString;
84-
else
99+
}
100+
else {
85101
throw new Error("valueString option must be a string");
102+
}
86103
}
87104
if ("prettyPrinting" in options) {
88105
if ("enabled" in options.prettyPrinting) {
89-
if (typeof options.prettyPrinting.enabled === "boolean")
106+
if (typeof options.prettyPrinting.enabled === "boolean") {
90107
prettyPrinting = options.prettyPrinting.enabled;
91-
else
108+
}
109+
else {
92110
throw new Error("prettyPrinting.enabled option must be a boolean");
111+
}
93112
}
94113

95114
if ("indentString" in options.prettyPrinting) {
96-
if (typeof options.prettyPrinting.indentString === "string")
115+
if (typeof options.prettyPrinting.indentString === "string") {
97116
indentString = options.prettyPrinting.indentString;
98-
else
117+
}
118+
else {
99119
throw new Error("prettyPrinting.indentString option must be a string");
120+
}
100121
}
101122
}
102123
if ("convertMap" in options) {
103-
if (Object.prototype.toString.call(options.convertMap) === "[object Object]")
124+
if (Object.prototype.toString.call(options.convertMap) === "[object Object]") {
104125
convertMap = options.convertMap;
105-
else
126+
}
127+
else {
106128
throw new Error("convertMap option must be an object");
129+
}
107130
}
108131
}
109132

110133
// Error checking and variable initialization for data
111-
if (typeof data !== "string" && typeof data !== "object")
134+
if (typeof data !== "string" && typeof data !== "object") {
112135
throw new Error("data must be an object or a string");
136+
}
113137

114-
if (typeof data === "string")
138+
if (typeof data === "string") {
115139
data = JSON.parse(data);
140+
}
116141

117142
var tempData = {};
118143
tempData[root] = data; // Add root element to object
@@ -126,23 +151,26 @@
126151
var xml = arguments[1] || "";
127152
var level = arguments[2] || 0;
128153

154+
var i;
155+
var tempObject = {};
156+
129157
for (var property in object) {
130158
// Arrays
131159
if (Object.prototype.toString.call(object[property]) === "[object Array]") {
132160
if (wrapArray) {
133161
// Create separate XML elements for each array element, but wrap all elements in a single element
134162
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 = {};
137165
tempObject[wrapArrayItem] = object[property][i];
138166
xml = toXML(tempObject, xml, level + 1);
139167
}
140168
xml += addBreak(addIndent("</" + property + ">", level));
141169
}
142170
else {
143171
// 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 = {};
146174
tempObject[property] = object[property][i];
147175

148176
xml = toXML(tempObject, xml, level);
@@ -158,21 +186,27 @@
158186
var lengthExcludingAttributes = Object.keys(object[property]).length;
159187
if (Object.prototype.toString.call(object[property][attributeString]) === "[object Object]") {
160188
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+
}
163195
}
164196

165-
if (lengthExcludingAttributes === 0) // Empty object
197+
if (lengthExcludingAttributes === 0) { // Empty object
166198
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
168201
xml += addBreak(">" + toString(object[property][valueString], true) + "</" + property + ">");
202+
}
169203
else { // Object with properties
170204
xml += addBreak(">");
171205

172206
// Create separate object for each property and pass to this function
173207
for (var subProperty in object[property]) {
174208
if (subProperty !== attributeString) {
175-
var tempObject = {};
209+
tempObject = {};
176210
tempObject[subProperty] = object[property][subProperty];
177211

178212
xml = toXML(tempObject, xml, level + 1);
@@ -194,11 +228,14 @@
194228
xml = xml.replace(/\s+$/g, "");
195229

196230
// Add XML declaration
197-
if (xmlDeclaration)
198-
if (xmlEncoding === null)
231+
if (xmlDeclaration) {
232+
if (xmlEncoding === null) {
199233
xml = addBreak("<?xml version=\"" + xmlVersion + "\"?>") + xml;
200-
else
234+
}
235+
else {
201236
xml = addBreak("<?xml version=\"" + xmlVersion + "\" encoding=\"" + xmlEncoding + "\"?>") + xml;
237+
}
238+
}
202239
}
203240

204241
return xml;
@@ -227,10 +264,12 @@
227264
var toString = function(data) {
228265
// Recursive function used to handle nested functions
229266
var functionHelper = function(data) {
230-
if (Object.prototype.toString.call(data) === "[object Function]")
267+
if (Object.prototype.toString.call(data) === "[object Function]") {
231268
return functionHelper(data());
232-
else
269+
}
270+
else {
233271
return data;
272+
}
234273
};
235274

236275
// Convert map

0 commit comments

Comments
 (0)