Skip to content

Commit 91c7284

Browse files
Finalize changes for 0.1.6 release
1 parent 739e80f commit 91c7284

4 files changed

Lines changed: 156 additions & 49 deletions

File tree

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The following example illustrates the basic usage of js2xmlparser:
7676
> <lastName>Smith</lastName>
7777
> </person>
7878

79-
Here's a more complex example that builds on the first:
79+
This is a more complex example that builds on the first:
8080

8181
var js2xmlparser = require("js2xmlparser");
8282

@@ -141,7 +141,44 @@ Here's a more complex example that builds on the first:
141141
> <notes>John&apos;s profile is not complete.</notes>
142142
> </person>
143143

144-
Here's an example that uses the convert map feature:
144+
This example uses the alias string feature:
145+
146+
var js2xmlparser = require("js2xmlparser");
147+
148+
var data = {
149+
"phone": [
150+
{
151+
"@": {
152+
"type": "home"
153+
},
154+
"#": "123-555-4567"
155+
},
156+
{
157+
"@": {
158+
"type": "work"
159+
},
160+
"#": "123-555-4567",
161+
"=": "telephone"
162+
},
163+
{
164+
"@": {
165+
"type": "cell"
166+
},
167+
"#": "456-555-7890"
168+
}
169+
]
170+
};
171+
172+
console.log(js2xmlparser("person", data));
173+
174+
> <?xml version="1.0" encoding="UTF-8"?>
175+
> <person>
176+
> <phone type="home">123-555-4567</phone>
177+
> <telephone type="work">123-555-4567</telephone>
178+
> <phone type="cell">456-555-7890</phone>
179+
> </person>
180+
181+
The following an example that uses the convert map feature:
145182

146183
var js2xmlparser = require("js2xmlparser");
147184

@@ -169,7 +206,7 @@ Here's an example that uses the convert map feature:
169206
> <dateOfBirth>1964-08-26T04:00:00.000Z</dateOfBirth>
170207
> </person>
171208

172-
Here's an example that wraps strings in CDATA tags instead of escaping invalid characters.
209+
Here's an example that wraps strings in CDATA tags instead of escaping invalid characters:
173210

174211
var js2xmlparser = require("js2xmlparser");
175212

example/example.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1919
*/
2020

21-
(function() {
21+
(function () {
2222
"use strict";
2323

2424
var js2xmlparser = require("../lib/js2xmlparser.js");
@@ -64,8 +64,7 @@
6464
"@": {
6565
"type": "work"
6666
},
67-
"#": "123-555-4567",
68-
"=": "telephone"
67+
"#": "123-555-4567"
6968
},
7069
{
7170
"@": {
@@ -74,8 +73,10 @@
7473
"#": "456-555-7890"
7574
}
7675
],
77-
"email": function() {return "john@smith.com";},
78-
"notes": "John's profile is not complete."
76+
"email": function () {
77+
return "john@smith.com";
78+
},
79+
"comment": "John's profile is not complete."
7980
};
8081

8182
console.log(js2xmlparser("person", example2));
@@ -85,39 +86,71 @@
8586
console.log("=========");
8687

8788
var example3 = {
88-
"email": function() {return "john@smith.com";},
89+
"phone": [
90+
{
91+
"@": {
92+
"type": "home"
93+
},
94+
"#": "123-555-4567"
95+
},
96+
{
97+
"@": {
98+
"type": "work"
99+
},
100+
"#": "123-555-4567",
101+
"=": "telephone"
102+
},
103+
{
104+
"@": {
105+
"type": "cell"
106+
},
107+
"#": "456-555-7890"
108+
}
109+
]
110+
};
111+
112+
console.log(js2xmlparser("person", example3));
113+
console.log();
114+
115+
console.log("EXAMPLE 4");
116+
console.log("=========");
117+
118+
var example4 = {
119+
"email": function () {
120+
return "john@smith.com";
121+
},
89122
"dateOfBirth": new Date(1964, 7, 26)
90123
};
91124

92-
var example3Options = {
125+
var example4Options = {
93126
convertMap: {
94-
"[object Date]": function(date) {
127+
"[object Date]": function (date) {
95128
return date.toISOString();
96129
},
97-
"[object Function]": function(func) {
130+
"[object Function]": function (func) {
98131
return func.toString();
99132
}
100133
}
101134
};
102135

103-
console.log(js2xmlparser("person", example3, example3Options));
136+
console.log(js2xmlparser("person", example4, example4Options));
104137
console.log();
105138

106-
console.log("EXAMPLE 4");
139+
console.log("EXAMPLE 5");
107140
console.log("=========");
108141

109-
var example4 = {
110-
"notes": {
142+
var example5 = {
143+
"comment": {
111144
"@": {
112145
"type": "status"
113146
},
114-
"#":"John's profile is not complete."
147+
"#": "John's profile is not complete."
115148
}
116149
};
117150

118-
var example4Options = {
151+
var example5Options = {
119152
useCDATA: true
120153
};
121154

122-
console.log(js2xmlparser("person", example4, example4Options));
155+
console.log(js2xmlparser("person", example5, example5Options));
123156
})();

lib/js2xmlparser.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179

180180
// When alias string property is present, use as alias for element name
181181
if (Object.prototype.toString.call(object[property]) === "[object Object]" &&
182-
aliasString in object[property]) {
182+
aliasString in object[property]) {
183183
elementName = object[property][aliasString];
184184
}
185185

@@ -220,18 +220,18 @@
220220
xml += addBreak("/>");
221221
}
222222
else if ((lengthExcludingAttributes === 1 ||
223-
(lengthExcludingAttributes === 2 && aliasString in object[property])) &&
224-
valueString in object[property]) { // Value string only
223+
(lengthExcludingAttributes === 2 && aliasString in object[property])) &&
224+
valueString in object[property]) { // Value string only
225225
xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName +
226-
">");
226+
">");
227227
}
228228
else { // Object with properties
229229
xml += addBreak(">");
230230

231231
// Create separate object for each property and pass to this function
232232
for (var subProperty in object[property]) {
233233
if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString &&
234-
subProperty !== valueString) {
234+
subProperty !== valueString) {
235235
tempObject = {};
236236
tempObject[subProperty] = object[property][subProperty];
237237

@@ -245,7 +245,7 @@
245245
// Everything else
246246
else {
247247
xml += addBreak(addIndent("<" + elementName + ">" + toString(object[property], false) + "</" +
248-
elementName + ">", level));
248+
elementName + ">", level));
249249
}
250250
}
251251
}

test/test.js

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,7 @@
12081208
}
12091209
}, defaultOptions);
12101210
res.should.equal("<base><a>b</a><a>c</a><a>d</a><e>f</e><e>g</e><e>h</e><e>i</e><e>j</e><k><l>m</l>" +
1211-
"<l>n</l><l>o</l></k></base>");
1211+
"<l>n</l><l>o</l></k></base>");
12121212
});
12131213

12141214
it("should correctly parse example 1", function () {
@@ -1247,8 +1247,7 @@
12471247
"@": {
12481248
"type": "work"
12491249
},
1250-
"#": "123-555-4567",
1251-
"=": "telephone"
1250+
"#": "123-555-4567"
12521251
},
12531252
{
12541253
"@": {
@@ -1257,15 +1256,17 @@
12571256
"#": "456-555-7890"
12581257
}
12591258
],
1260-
"email": function() {return "john@smith.com";},
1259+
"email": function () {
1260+
return "john@smith.com";
1261+
},
12611262
"notes": "John's profile is not complete."
12621263
}, defaultOptions);
12631264
res.should.equal("<person type=\"individual\"><firstName>John</firstName><lastName>Smith</lastName>" +
1264-
"<dateOfBirth>"+new Date(1964, 7, 26)+"</dateOfBirth><address " +
1265-
"type=\"home\"><streetAddress>3212 22nd St</streetAddress><city>Chicago</city><state>Illinois" +
1266-
"</state><zip>10000</zip></address><phone type=\"home\">123-555-4567</phone><telephone " +
1267-
"type=\"work\">123-555-4567</telephone><phone type=\"cell\">456-555-7890</phone><email>" +
1268-
"john@smith.com</email><notes>John&apos;s profile is not complete.</notes></person>");
1265+
"<dateOfBirth>" + new Date(1964, 7, 26) + "</dateOfBirth><address " +
1266+
"type=\"home\"><streetAddress>3212 22nd St</streetAddress><city>Chicago</city><state>Illinois" +
1267+
"</state><zip>10000</zip></address><phone type=\"home\">123-555-4567</phone><phone " +
1268+
"type=\"work\">123-555-4567</phone><phone type=\"cell\">456-555-7890</phone><email>" +
1269+
"john@smith.com</email><notes>John&apos;s profile is not complete.</notes></person>");
12691270
});
12701271

12711272
it("should correctly parse example 2 with pretty printing", function () {
@@ -1296,8 +1297,7 @@
12961297
"@": {
12971298
"type": "work"
12981299
},
1299-
"#": "123-555-4567",
1300-
"=": "telephone"
1300+
"#": "123-555-4567"
13011301
},
13021302
{
13031303
"@": {
@@ -1306,25 +1306,62 @@
13061306
"#": "456-555-7890"
13071307
}
13081308
],
1309-
"email": function() {return "john@smith.com";},
1309+
"email": function () {
1310+
return "john@smith.com";
1311+
},
13101312
"notes": "John's profile is not complete."
13111313
}, {
13121314
declaration: {
13131315
include: false
13141316
}
13151317
});
13161318
res.should.equal("<person type=\"individual\">\n\t<firstName>John</firstName>\n\t<lastName>Smith" +
1317-
"</lastName>\n\t<dateOfBirth>"+ new Date(1964, 7, 26) +
1318-
"</dateOfBirth>\n\t<address type=\"home\">\n\t\t<streetAddress>3212 22nd St</streetAddress>" +
1319-
"\n\t\t<city>Chicago</city>\n\t\t<state>Illinois</state>\n\t\t<zip>10000</zip>\n\t</address>" +
1320-
"\n\t<phone type=\"home\">123-555-4567</phone>\n\t<telephone type=\"work\">123-555-4567" +
1321-
"</telephone>\n\t<phone type=\"cell\">456-555-7890</phone>\n\t<email>john@smith.com</email>\n\t" +
1322-
"<notes>John&apos;s profile is not complete.</notes>\n</person>");
1319+
"</lastName>\n\t<dateOfBirth>" + new Date(1964, 7, 26) +
1320+
"</dateOfBirth>\n\t<address type=\"home\">\n\t\t<streetAddress>3212 22nd St</streetAddress>" +
1321+
"\n\t\t<city>Chicago</city>\n\t\t<state>Illinois</state>\n\t\t<zip>10000</zip>\n\t</address>" +
1322+
"\n\t<phone type=\"home\">123-555-4567</phone>\n\t<phone type=\"work\">123-555-4567" +
1323+
"</phone>\n\t<phone type=\"cell\">456-555-7890</phone>\n\t<email>john@smith.com</email>\n\t" +
1324+
"<notes>John&apos;s profile is not complete.</notes>\n</person>");
13231325
});
13241326

13251327
it("should correctly parse example 3", function () {
13261328
var res = js2xmlparser("person", {
1327-
"email": function() {return "john@smith.com";},
1329+
"phone": [
1330+
{
1331+
"@": {
1332+
"type": "home"
1333+
},
1334+
"#": "123-555-4567"
1335+
},
1336+
{
1337+
"@": {
1338+
"type": "work"
1339+
},
1340+
"#": "123-555-4567",
1341+
"=": "telephone"
1342+
},
1343+
{
1344+
"@": {
1345+
"type": "cell"
1346+
},
1347+
"#": "456-555-7890"
1348+
}
1349+
]
1350+
}, {
1351+
declaration: {
1352+
include: false
1353+
},
1354+
prettyPrinting: {
1355+
enabled: false
1356+
}
1357+
});
1358+
res.should.equal("<person><phone type=\"home\">123-555-4567</phone><telephone type=\"work\">" +
1359+
"123-555-4567</telephone><phone type=\"cell\">456-555-7890</phone></person>");
1360+
});
1361+
1362+
it("should correctly parse example 4", function () {
1363+
var res = js2xmlparser("person", {
1364+
"email": function () {return "john@smith.com";},
13281365
"dateOfBirth": new Date(Date.UTC(1964, 7, 26))
13291366
}, {
13301367
declaration: {
@@ -1337,22 +1374,22 @@
13371374
"[object Date]": function (date) {
13381375
return date.toISOString();
13391376
},
1340-
"[object Function]": function(func) {
1377+
"[object Function]": function (func) {
13411378
return func.toString();
13421379
}
13431380
}
13441381
});
13451382
res.should.equal("<person><email>function () {return &quot;john@smith.com&quot;;}</email>" +
1346-
"<dateOfBirth>1964-08-26T00:00:00.000Z</dateOfBirth></person>");
1383+
"<dateOfBirth>1964-08-26T00:00:00.000Z</dateOfBirth></person>");
13471384
});
13481385

1349-
it("should correctly parse example 4", function () {
1386+
it("should correctly parse example 5", function () {
13501387
var res = js2xmlparser("person", {
13511388
"notes": {
13521389
"@": {
13531390
"type": "status"
13541391
},
1355-
"#":"John's profile is not complete."
1392+
"#": "John's profile is not complete."
13561393
}
13571394
}, {
13581395
declaration: {
@@ -1364,7 +1401,7 @@
13641401
useCDATA: true
13651402
});
13661403
res.should.equal("<person><notes type=\"status\"><![CDATA[John's profile is not complete.]]></notes>" +
1367-
"</person>");
1404+
"</person>");
13681405
});
13691406
});
13701407
});

0 commit comments

Comments
 (0)