Skip to content

Commit b99fa94

Browse files
committed
added special key for node element name aliases
1 parent 6a5e23c commit b99fa94

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

lib/js2xmlparser.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
var xmlVersion = "1.0";
2626
var xmlEncoding = "UTF-8";
2727
var attributeString = "@";
28+
var nodeAliasString = "=";
2829
var valueString = "#";
2930
var prettyPrinting = true;
3031
var indentString = "\t";
@@ -85,6 +86,14 @@
8586
throw new Error("valueString option must be a string");
8687
}
8788
}
89+
if ("nodeAliasString" in options) {
90+
if (typeof options.nodeAliasString === "string") {
91+
nodeAliasString = options.nodeAliasString;
92+
}
93+
else {
94+
throw new Error("attributeString option must be a string");
95+
}
96+
}
8897
if ("prettyPrinting" in options) {
8998
if ("enabled" in options.prettyPrinting) {
9099
if (typeof options.prettyPrinting.enabled === "boolean") {
@@ -163,6 +172,16 @@
163172
elementName = "_" + property;
164173
}
165174

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];
178+
}
179+
180+
// Skip alias key
181+
if (elementName === nodeAliasString) {
182+
continue;
183+
}
184+
166185
// Arrays
167186
if (Object.prototype.toString.call(object[property]) === "[object Array]") {
168187
// Create separate XML elements for each array element
@@ -173,6 +192,7 @@
173192
xml = toXML(tempObject, xml, level);
174193
}
175194
}
195+
176196
// JSON-type objects with properties
177197
else if (Object.prototype.toString.call(object[property]) === "[object Object]") {
178198
xml += addIndent("<" + elementName, level);
@@ -321,6 +341,7 @@
321341
xmlVersion = "1.0";
322342
xmlEncoding = "UTF-8";
323343
attributeString = "@";
344+
nodeAliasString = "=";
324345
valueString = "#";
325346
prettyPrinting = true;
326347
indentString = "\t";

test/test.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,107 @@
968968
res.should.equal("<base><a><![CDATA[b'c]]></a></base>");
969969
});
970970
});
971+
972+
describe("nodeAliasString", function () {
973+
974+
it("should raise an error when options is defined and options.nodeAliasString is undefined", function () {
975+
var res;
976+
try {
977+
res = js2xmlparser(defaultRoot, defaultData, {
978+
nodeAliasString: undefined
979+
});
980+
} catch (e) {
981+
e.should.match(/nodeAliasString option must be a string/);
982+
}
983+
should.not.exist(res);
984+
});
985+
986+
it("should raise an error when options is defined and options.nodeAliasString is an object", function () {
987+
var res;
988+
try {
989+
res = js2xmlparser(defaultRoot, defaultData, {
990+
nodeAliasString: {}
991+
});
992+
} catch (e) {
993+
e.should.match(/nodeAliasString option must be a string/);
994+
}
995+
should.not.exist(res);
996+
});
997+
998+
it("should raise an error when options is defined and options.nodeAliasString is an array", function () {
999+
var res;
1000+
try {
1001+
res = js2xmlparser(defaultRoot, defaultData, {
1002+
nodeAliasString: []
1003+
});
1004+
} catch (e) {
1005+
e.should.match(/nodeAliasString option must be a string/);
1006+
}
1007+
should.not.exist(res);
1008+
});
1009+
1010+
it("should raise an error when options is defined and options.nodeAliasString is a number", function () {
1011+
var res;
1012+
try {
1013+
res = js2xmlparser(defaultRoot, defaultData, {
1014+
nodeAliasString: 2
1015+
});
1016+
} catch (e) {
1017+
e.should.match(/nodeAliasString option must be a string/);
1018+
}
1019+
should.not.exist(res);
1020+
});
1021+
1022+
it("should raise an error when options is defined and options.nodeAliasString is a boolean", function () {
1023+
var res;
1024+
try {
1025+
res = js2xmlparser(defaultRoot, defaultData, {
1026+
nodeAliasString: true
1027+
});
1028+
} catch (e) {
1029+
e.should.match(/nodeAliasString option must be a string/);
1030+
}
1031+
should.not.exist(res);
1032+
});
1033+
1034+
it("should create XML with node alias string '=' when options.nodeAliasString is not specified", function () {
1035+
var res = js2xmlparser(defaultRoot, {
1036+
a: {
1037+
"=": "b"
1038+
}
1039+
}, defaultOptions);
1040+
res.should.equal("<base><b></b></base>");
1041+
});
1042+
1043+
it("should create XML with node alias string '__alias' when options.nodeAliasString is '__alias'", function () {
1044+
var res = js2xmlparser(defaultRoot, {
1045+
a: {
1046+
"__alias": "b"
1047+
}
1048+
}, {
1049+
declaration: {
1050+
include: false
1051+
},
1052+
prettyPrinting: {
1053+
enabled: false
1054+
},
1055+
nodeAliasString: "__alias"
1056+
});
1057+
res.should.equal("<base><b></b></base>");
1058+
});
1059+
1060+
it("should create XML with options.nodeAliasString and data is an array", function () {
1061+
var res = js2xmlparser(defaultRoot, {
1062+
a: [{
1063+
"=": "b"
1064+
}, {
1065+
"=": "c"
1066+
}]
1067+
}, defaultOptions);
1068+
res.should.equal("<base><b></b><c></c></base>");
1069+
});
1070+
1071+
})
9711072
});
9721073

9731074
describe("data", function () {

0 commit comments

Comments
 (0)