Skip to content

Commit 31e1f4e

Browse files
Reconcile examples across entire project
1 parent 787d4a0 commit 31e1f4e

3 files changed

Lines changed: 41 additions & 71 deletions

File tree

README.md

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ This is a more complex example that builds on the first:
107107
"@": {
108108
"type": "work"
109109
},
110-
"#": "123-555-4567",
111-
"=": "telephone"
110+
"#": "789-555-4567"
112111
},
113112
{
114113
"@": {
@@ -117,7 +116,9 @@ This is a more complex example that builds on the first:
117116
"#": "456-555-7890"
118117
}
119118
],
120-
"email": function() {return "john@smith.com";},
119+
"email": function() {
120+
return "john@smith.com";
121+
},
121122
"notes": "John's profile is not complete."
122123
};
123124

@@ -135,7 +136,7 @@ This is a more complex example that builds on the first:
135136
> <zip>10000</zip>
136137
> </address>
137138
> <phone type="home">123-555-4567</phone>
138-
> <telephone type="work">123-555-4567</telephone>
139+
> <phone type="work">789-555-4567</telephone>
139140
> <phone type="cell">456-555-7890</phone>
140141
> <email>john@smith.com</email>
141142
> <notes>John&apos;s profile is not complete.</notes>
@@ -146,44 +147,33 @@ This example uses the alias string feature:
146147
var js2xmlparser = require("js2xmlparser");
147148

148149
var data = {
149-
"phone": [
150+
"telephone": [
151+
"123-555-4567",
150152
{
151-
"@": {
152-
"type": "home"
153-
},
154-
"#": "123-555-4567"
155-
},
156-
{
157-
"@": {
158-
"type": "work"
159-
},
160-
"#": "123-555-4567",
161-
"=": "telephone"
153+
"#": "789-555-4567",
154+
"=": "fax"
162155
},
163-
{
164-
"@": {
165-
"type": "cell"
166-
},
167-
"#": "456-555-7890"
168-
}
156+
"456-555-7890"
169157
]
170158
};
171159

172160
console.log(js2xmlparser("person", data));
173161

174162
> <?xml version="1.0" encoding="UTF-8"?>
175163
> <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>
164+
> <telephone>123-555-4567</telephone>
165+
> <fax>789-555-4567</fax>
166+
> <telephone>456-555-7890</telephone>
179167
> </person>
180168

181169
The following an example that uses the convert map feature:
182170

183171
var js2xmlparser = require("js2xmlparser");
184172

185173
var data = {
186-
"email": function() {return "john@smith.com";},
174+
"email": function() {
175+
return "john@smith.com";
176+
},
187177
"dateOfBirth": new Date(1964, 7, 26)
188178
}
189179

@@ -202,8 +192,10 @@ The following an example that uses the convert map feature:
202192

203193
> <?xml version="1.0" encoding="UTF-8"?>
204194
> <person>
205-
> <email>function () {return &quot;john@smith.com&quot;;}</email>
206-
> <dateOfBirth>1964-08-26T04:00:00.000Z</dateOfBirth>
195+
> <email>function () {
196+
> return &quot;john@smith.com&quot;;
197+
> }</email>
198+
> <dateOfBirth>1964-08-26T05:00:00.000Z</dateOfBirth>
207199
> </person>
208200

209201
Here's an example that wraps strings in CDATA tags instead of escaping invalid characters:

example/example.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@": {
6565
"type": "work"
6666
},
67-
"#": "123-555-4567"
67+
"#": "789-555-4567"
6868
},
6969
{
7070
"@": {
@@ -73,7 +73,9 @@
7373
"#": "456-555-7890"
7474
}
7575
],
76-
"email": function () {return "john@smith.com";},
76+
"email": function () {
77+
return "john@smith.com";
78+
},
7779
"comment": "John's profile is not complete."
7880
};
7981

@@ -84,26 +86,13 @@
8486
console.log("=========");
8587

8688
var example3 = {
87-
"phone": [
88-
{
89-
"@": {
90-
"type": "home"
91-
},
92-
"#": "123-555-4567"
93-
},
89+
"telephone": [
90+
"123-555-4567",
9491
{
95-
"@": {
96-
"type": "work"
97-
},
98-
"#": "123-555-4567",
99-
"=": "telephone"
92+
"#": "789-555-4567",
93+
"=": "fax"
10094
},
101-
{
102-
"@": {
103-
"type": "cell"
104-
},
105-
"#": "456-555-7890"
106-
}
95+
"456-555-7890"
10796
]
10897
};
10998

test/test.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,26 +1326,13 @@
13261326

13271327
it("should correctly parse example 3", function () {
13281328
var res = js2xmlparser("person", {
1329-
"phone": [
1330-
{
1331-
"@": {
1332-
"type": "home"
1333-
},
1334-
"#": "123-555-4567"
1335-
},
1329+
"telephone": [
1330+
"123-555-4567",
13361331
{
1337-
"@": {
1338-
"type": "work"
1339-
},
1340-
"#": "123-555-4567",
1341-
"=": "telephone"
1332+
"#": "789-555-4567",
1333+
"=": "fax"
13421334
},
1343-
{
1344-
"@": {
1345-
"type": "cell"
1346-
},
1347-
"#": "456-555-7890"
1348-
}
1335+
"456-555-7890"
13491336
]
13501337
}, {
13511338
declaration: {
@@ -1355,13 +1342,15 @@
13551342
enabled: false
13561343
}
13571344
});
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>");
1345+
res.should.equal("<person><telephone>123-555-4567</telephone><fax>789-555-4567</fax><telephone>" +
1346+
"456-555-7890</telephone></person>");
13601347
});
13611348

13621349
it("should correctly parse example 4", function () {
13631350
var res = js2xmlparser("person", {
1364-
"email": function () {return "john@smith.com";},
1351+
"email": function () {
1352+
return "john@smith.com";
1353+
},
13651354
"dateOfBirth": new Date(Date.UTC(1964, 7, 26))
13661355
}, {
13671356
declaration: {
@@ -1379,8 +1368,8 @@
13791368
}
13801369
}
13811370
});
1382-
res.should.equal("<person><email>function () {return &quot;john@smith.com&quot;;}</email>" +
1383-
"<dateOfBirth>1964-08-26T00:00:00.000Z</dateOfBirth></person>");
1371+
res.should.equal("<person><email>function () {\r\n return &quot;john@smith.com" +
1372+
"&quot;;\r\n }</email><dateOfBirth>1964-08-26T00:00:00.000Z</dateOfBirth></person>");
13841373
});
13851374

13861375
it("should correctly parse example 5", function () {

0 commit comments

Comments
 (0)