Skip to content

Commit 739e80f

Browse files
Finalize alias string additions
1 parent 8235ae8 commit 739e80f

6 files changed

Lines changed: 96 additions & 58 deletions

File tree

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.1.6 ##
2+
3+
* Addition of alias string option
4+
* Minor changes to examples
5+
* Minor fixes to tests
6+
17
## 0.1.5 ##
28

39
* Bug fixes

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ The js2xmlparser module contains one function which takes the following argument
4646
* `valueString` - the name of the property representing an element's value; note that any property with a name equal
4747
to the value string is ignored except in the context of supplying a value for a tag containing attributes (string,
4848
optional, default: "#")
49+
* `aliasString` - the name of the property representing an element's alias; the name of the containing element will
50+
be replaced with the alias (string, optional, default: "=")
4951
* `prettyPrinting` - pretty-printing options (object, optional)
5052
* `enabled` - specifies whether pretty-printing is enabled (boolean, optional, default: true)
5153
* `indentString` - indent string (string, optional, default: "\t")
@@ -101,6 +103,13 @@ Here's a more complex example that builds on the first:
101103
},
102104
"#": "123-555-4567"
103105
},
106+
{
107+
"@": {
108+
"type": "work"
109+
},
110+
"#": "123-555-4567",
111+
"=": "telephone"
112+
},
104113
{
105114
"@": {
106115
"type": "cell"
@@ -126,6 +135,7 @@ Here's a more complex example that builds on the first:
126135
> <zip>10000</zip>
127136
> </address>
128137
> <phone type="home">123-555-4567</phone>
138+
> <telephone type="work">123-555-4567</telephone>
129139
> <phone type="cell">456-555-7890</phone>
130140
> <email>john@smith.com</email>
131141
> <notes>John&apos;s profile is not complete.</notes>

example/example.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
},
6161
"#": "123-555-4567"
6262
},
63+
{
64+
"@": {
65+
"type": "work"
66+
},
67+
"#": "123-555-4567",
68+
"=": "telephone"
69+
},
6370
{
6471
"@": {
6572
"type": "cell"

lib/js2xmlparser.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
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 xmlDeclaration = true;
2525
var xmlVersion = "1.0";
2626
var xmlEncoding = "UTF-8";
2727
var attributeString = "@";
28-
var nodeAliasString = "=";
28+
var aliasString = "=";
2929
var valueString = "#";
3030
var prettyPrinting = true;
3131
var indentString = "\t";
@@ -37,7 +37,7 @@
3737
};
3838

3939
// Initialization
40-
var init = function(root, data, options) {
40+
var init = function (root, data, options) {
4141
// Set option defaults
4242
setOptionDefaults();
4343

@@ -86,12 +86,12 @@
8686
throw new Error("valueString option must be a string");
8787
}
8888
}
89-
if ("nodeAliasString" in options) {
90-
if (typeof options.nodeAliasString === "string") {
91-
nodeAliasString = options.nodeAliasString;
89+
if ("aliasString" in options) {
90+
if (typeof options.aliasString === "string") {
91+
aliasString = options.aliasString;
9292
}
9393
else {
94-
throw new Error("nodeAliasString option must be a string");
94+
throw new Error("aliasString option must be a string");
9595
}
9696
}
9797
if ("prettyPrinting" in options) {
@@ -156,7 +156,7 @@
156156
};
157157

158158
// Convert object to XML
159-
var toXML = function(object) {
159+
var toXML = function (object) {
160160
// Initialize arguments, if necessary
161161
var xml = arguments[1] || "";
162162
var level = arguments[2] || 0;
@@ -172,14 +172,15 @@
172172
elementName = "_" + property;
173173
}
174174

175-
// Allow to use an alias for element names
176-
if (Object.prototype.toString.call(object[property]) === "[object Object]" && nodeAliasString in object[property]) {
177-
elementName = object[property][nodeAliasString];
175+
// Skip alias string property
176+
if (elementName === aliasString) {
177+
continue;
178178
}
179179

180-
// Skip alias key
181-
if (elementName === nodeAliasString) {
182-
continue;
180+
// When alias string property is present, use as alias for element name
181+
if (Object.prototype.toString.call(object[property]) === "[object Object]" &&
182+
aliasString in object[property]) {
183+
elementName = object[property][aliasString];
183184
}
184185

185186
// Arrays
@@ -192,7 +193,6 @@
192193
xml = toXML(tempObject, xml, level);
193194
}
194195
}
195-
196196
// JSON-type objects with properties
197197
else if (Object.prototype.toString.call(object[property]) === "[object Object]") {
198198
xml += addIndent("<" + elementName, level);
@@ -204,7 +204,7 @@
204204
for (var attribute in object[property][attributeString]) {
205205
if (object[property][attributeString].hasOwnProperty(attribute)) {
206206
xml += " " + attribute + "=\"" +
207-
toString(object[property][attributeString][attribute], true) + "\"";
207+
toString(object[property][attributeString][attribute], true) + "\"";
208208
}
209209
}
210210
}
@@ -219,15 +219,19 @@
219219
if (lengthExcludingAttributes === 0) { // Empty object
220220
xml += addBreak("/>");
221221
}
222-
else if (lengthExcludingAttributes === 1 && valueString in object[property]) { // Value string only
223-
xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName + ">");
222+
else if ((lengthExcludingAttributes === 1 ||
223+
(lengthExcludingAttributes === 2 && aliasString in object[property])) &&
224+
valueString in object[property]) { // Value string only
225+
xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName +
226+
">");
224227
}
225228
else { // Object with properties
226229
xml += addBreak(">");
227230

228231
// Create separate object for each property and pass to this function
229232
for (var subProperty in object[property]) {
230-
if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString && subProperty !== valueString) {
233+
if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString &&
234+
subProperty !== valueString) {
231235
tempObject = {};
232236
tempObject[subProperty] = object[property][subProperty];
233237

@@ -266,7 +270,7 @@
266270
};
267271

268272
// Add indenting to data for pretty printing
269-
var addIndent = function(data, level) {
273+
var addIndent = function (data, level) {
270274
if (prettyPrinting) {
271275

272276
var indent = "";
@@ -280,14 +284,14 @@
280284
};
281285

282286
// Add line break to data for pretty printing
283-
var addBreak = function(data) {
287+
var addBreak = function (data) {
284288
return prettyPrinting ? data + "\n" : data;
285289
};
286290

287291
// Convert anything into a valid XML string representation
288-
var toString = function(data, isAttribute) {
292+
var toString = function (data, isAttribute) {
289293
// Recursive function used to handle nested functions
290-
var functionHelper = function(data) {
294+
var functionHelper = function (data) {
291295
if (Object.prototype.toString.call(data) === "[object Function]") {
292296
return functionHelper(data());
293297
}
@@ -334,14 +338,14 @@
334338
};
335339

336340
// Revert options back to their default settings
337-
var setOptionDefaults = function() {
341+
var setOptionDefaults = function () {
338342
useCDATA = false;
339343
convertMap = {};
340344
xmlDeclaration = true;
341345
xmlVersion = "1.0";
342346
xmlEncoding = "UTF-8";
343347
attributeString = "@";
344-
nodeAliasString = "=";
348+
aliasString = "=";
345349
valueString = "#";
346350
prettyPrinting = true;
347351
indentString = "\t";

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Parses JavaScript objects into XML",
44
"keywords": ["convert","converter","js","json","object","objects","parse","parser","xml"],
55
"homepage": "http://www.kourlas.net",
6-
"version": "0.1.5",
6+
"version": "0.1.6",
77
"author": "Michael Kourlas <michael@kourlas.net>",
88
"main": "./lib/js2xmlparser.js",
99
"repository" : {

0 commit comments

Comments
 (0)