js2xmlparser
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -0,1 +1,7 @@ | ||
## 0.1.6 ## | ||
* Addition of alias string option | ||
* Minor changes to examples | ||
* Minor fixes to tests | ||
## 0.1.5 ## | ||
@@ -2,0 +8,0 @@ |
@@ -21,3 +21,3 @@ /* jshint node:true */ | ||
(function() { | ||
(function () { | ||
"use strict"; | ||
@@ -66,2 +66,8 @@ | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
@@ -72,4 +78,6 @@ }, | ||
], | ||
"email": function() {return "john@smith.com";}, | ||
"notes": "John's profile is not complete." | ||
"email": function () { | ||
return "john@smith.com"; | ||
}, | ||
"comment": "John's profile is not complete." | ||
}; | ||
@@ -84,12 +92,44 @@ | ||
var example3 = { | ||
"email": function() {return "john@smith.com";}, | ||
"phone": [ | ||
{ | ||
"@": { | ||
"type": "home" | ||
}, | ||
"#": "123-555-4567" | ||
}, | ||
{ | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567", | ||
"=": "telephone" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
}, | ||
"#": "456-555-7890" | ||
} | ||
] | ||
}; | ||
console.log(js2xmlparser("person", example3)); | ||
console.log(); | ||
console.log("EXAMPLE 4"); | ||
console.log("========="); | ||
var example4 = { | ||
"email": function () { | ||
return "john@smith.com"; | ||
}, | ||
"dateOfBirth": new Date(1964, 7, 26) | ||
}; | ||
var example3Options = { | ||
var example4Options = { | ||
convertMap: { | ||
"[object Date]": function(date) { | ||
"[object Date]": function (date) { | ||
return date.toISOString(); | ||
}, | ||
"[object Function]": function(func) { | ||
"[object Function]": function (func) { | ||
return func.toString(); | ||
@@ -100,22 +140,22 @@ } | ||
console.log(js2xmlparser("person", example3, example3Options)); | ||
console.log(js2xmlparser("person", example4, example4Options)); | ||
console.log(); | ||
console.log("EXAMPLE 4"); | ||
console.log("EXAMPLE 5"); | ||
console.log("========="); | ||
var example4 = { | ||
"notes": { | ||
var example5 = { | ||
"comment": { | ||
"@": { | ||
"type": "status" | ||
}, | ||
"#":"John's profile is not complete." | ||
"#": "John's profile is not complete." | ||
} | ||
}; | ||
var example4Options = { | ||
var example5Options = { | ||
useCDATA: true | ||
}; | ||
console.log(js2xmlparser("person", example4, example4Options)); | ||
console.log(js2xmlparser("person", example5, example5Options)); | ||
})(); |
@@ -21,3 +21,3 @@ /* jshint node:true */ | ||
(function() { | ||
(function () { | ||
"use strict"; | ||
@@ -29,2 +29,3 @@ | ||
var attributeString = "@"; | ||
var aliasString = "="; | ||
var valueString = "#"; | ||
@@ -41,3 +42,3 @@ var prettyPrinting = true; | ||
// Initialization | ||
var init = function(root, data, options) { | ||
var init = function (root, data, options) { | ||
// Set option defaults | ||
@@ -91,2 +92,10 @@ setOptionDefaults(); | ||
} | ||
if ("aliasString" in options) { | ||
if (typeof options.aliasString === "string") { | ||
aliasString = options.aliasString; | ||
} | ||
else { | ||
throw new Error("aliasString option must be a string"); | ||
} | ||
} | ||
if ("prettyPrinting" in options) { | ||
@@ -154,3 +163,3 @@ if ("enabled" in options.prettyPrinting) { | ||
// Convert object to XML | ||
var toXML = function(object) { | ||
var toXML = function (object) { | ||
// Initialize arguments, if necessary | ||
@@ -171,2 +180,13 @@ var xml = arguments[1] || ""; | ||
// Skip alias string property | ||
if (elementName === aliasString) { | ||
continue; | ||
} | ||
// When alias string property is present, use as alias for element name | ||
if (Object.prototype.toString.call(object[property]) === "[object Object]" && | ||
aliasString in object[property]) { | ||
elementName = object[property][aliasString]; | ||
} | ||
// Arrays | ||
@@ -193,3 +213,3 @@ if (Object.prototype.toString.call(object[property]) === "[object Array]") { | ||
xml += " " + attribute + "=\"" + | ||
toString(object[property][attributeString][attribute], true) + "\""; | ||
toString(object[property][attributeString][attribute], true) + "\""; | ||
} | ||
@@ -209,4 +229,7 @@ } | ||
} | ||
else if (lengthExcludingAttributes === 1 && valueString in object[property]) { // Value string only | ||
xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName + ">"); | ||
else if ((lengthExcludingAttributes === 1 || | ||
(lengthExcludingAttributes === 2 && aliasString in object[property])) && | ||
valueString in object[property]) { // Value string only | ||
xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName + | ||
">"); | ||
} | ||
@@ -218,3 +241,4 @@ else { // Object with properties | ||
for (var subProperty in object[property]) { | ||
if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString && subProperty !== valueString) { | ||
if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString && | ||
subProperty !== valueString) { | ||
tempObject = {}; | ||
@@ -233,3 +257,3 @@ tempObject[subProperty] = object[property][subProperty]; | ||
xml += addBreak(addIndent("<" + elementName + ">" + toString(object[property], false) + "</" + | ||
elementName + ">", level)); | ||
elementName + ">", level)); | ||
} | ||
@@ -259,3 +283,3 @@ } | ||
// Add indenting to data for pretty printing | ||
var addIndent = function(data, level) { | ||
var addIndent = function (data, level) { | ||
if (prettyPrinting) { | ||
@@ -274,3 +298,3 @@ | ||
// Add line break to data for pretty printing | ||
var addBreak = function(data) { | ||
var addBreak = function (data) { | ||
return prettyPrinting ? data + "\n" : data; | ||
@@ -280,5 +304,5 @@ }; | ||
// Convert anything into a valid XML string representation | ||
var toString = function(data, isAttribute) { | ||
var toString = function (data, isAttribute) { | ||
// Recursive function used to handle nested functions | ||
var functionHelper = function(data) { | ||
var functionHelper = function (data) { | ||
if (Object.prototype.toString.call(data) === "[object Function]") { | ||
@@ -330,3 +354,3 @@ return functionHelper(data()); | ||
// Revert options back to their default settings | ||
var setOptionDefaults = function() { | ||
var setOptionDefaults = function () { | ||
useCDATA = false; | ||
@@ -338,2 +362,3 @@ convertMap = {}; | ||
attributeString = "@"; | ||
aliasString = "="; | ||
valueString = "#"; | ||
@@ -340,0 +365,0 @@ prettyPrinting = true; |
@@ -6,3 +6,3 @@ { | ||
"homepage": "http://www.kourlas.net", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"author": "Michael Kourlas <michael@kourlas.net>", | ||
@@ -9,0 +9,0 @@ "main": "./lib/js2xmlparser.js", |
@@ -49,2 +49,4 @@ # js2xmlparser # | ||
optional, default: "#") | ||
* `aliasString` - the name of the property representing an element's alias; the name of the containing element will | ||
be replaced with the alias (string, optional, default: "=") | ||
* `prettyPrinting` - pretty-printing options (object, optional) | ||
@@ -78,3 +80,3 @@ * `enabled` - specifies whether pretty-printing is enabled (boolean, optional, default: true) | ||
Here's a more complex example that builds on the first: | ||
This is a more complex example that builds on the first: | ||
@@ -108,2 +110,9 @@ var js2xmlparser = require("js2xmlparser"); | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567", | ||
"=": "telephone" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
@@ -132,2 +141,3 @@ }, | ||
> <phone type="home">123-555-4567</phone> | ||
> <telephone type="work">123-555-4567</telephone> | ||
> <phone type="cell">456-555-7890</phone> | ||
@@ -138,3 +148,3 @@ > <email>john@smith.com</email> | ||
Here's an example that uses the convert map feature: | ||
This example uses the alias string feature: | ||
@@ -144,2 +154,39 @@ var js2xmlparser = require("js2xmlparser"); | ||
var data = { | ||
"phone": [ | ||
{ | ||
"@": { | ||
"type": "home" | ||
}, | ||
"#": "123-555-4567" | ||
}, | ||
{ | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567", | ||
"=": "telephone" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
}, | ||
"#": "456-555-7890" | ||
} | ||
] | ||
}; | ||
console.log(js2xmlparser("person", data)); | ||
> <?xml version="1.0" encoding="UTF-8"?> | ||
> <person> | ||
> <phone type="home">123-555-4567</phone> | ||
> <telephone type="work">123-555-4567</telephone> | ||
> <phone type="cell">456-555-7890</phone> | ||
> </person> | ||
The following an example that uses the convert map feature: | ||
var js2xmlparser = require("js2xmlparser"); | ||
var data = { | ||
"email": function() {return "john@smith.com";}, | ||
@@ -168,3 +215,3 @@ "dateOfBirth": new Date(1964, 7, 26) | ||
Here's an example that wraps strings in CDATA tags instead of escaping invalid characters. | ||
Here's an example that wraps strings in CDATA tags instead of escaping invalid characters: | ||
@@ -171,0 +218,0 @@ var js2xmlparser = require("js2xmlparser"); |
185
test/test.js
@@ -971,2 +971,103 @@ /* jshint node:true */ | ||
}); | ||
describe("aliasString", function () { | ||
it("should raise an error when options is defined and options.aliasString is undefined", function () { | ||
var res; | ||
try { | ||
res = js2xmlparser(defaultRoot, defaultData, { | ||
aliasString: undefined | ||
}); | ||
} catch (e) { | ||
e.should.match(/aliasString option must be a string/); | ||
} | ||
should.not.exist(res); | ||
}); | ||
it("should raise an error when options is defined and options.aliasString is an object", function () { | ||
var res; | ||
try { | ||
res = js2xmlparser(defaultRoot, defaultData, { | ||
aliasString: {} | ||
}); | ||
} catch (e) { | ||
e.should.match(/aliasString option must be a string/); | ||
} | ||
should.not.exist(res); | ||
}); | ||
it("should raise an error when options is defined and options.aliasString is an array", function () { | ||
var res; | ||
try { | ||
res = js2xmlparser(defaultRoot, defaultData, { | ||
aliasString: [] | ||
}); | ||
} catch (e) { | ||
e.should.match(/aliasString option must be a string/); | ||
} | ||
should.not.exist(res); | ||
}); | ||
it("should raise an error when options is defined and options.aliasString is a number", function () { | ||
var res; | ||
try { | ||
res = js2xmlparser(defaultRoot, defaultData, { | ||
aliasString: 2 | ||
}); | ||
} catch (e) { | ||
e.should.match(/aliasString option must be a string/); | ||
} | ||
should.not.exist(res); | ||
}); | ||
it("should raise an error when options is defined and options.aliasString is a boolean", function () { | ||
var res; | ||
try { | ||
res = js2xmlparser(defaultRoot, defaultData, { | ||
aliasString: true | ||
}); | ||
} catch (e) { | ||
e.should.match(/aliasString option must be a string/); | ||
} | ||
should.not.exist(res); | ||
}); | ||
it("should create XML with alias string '=' when options.aliasString is not specified", function () { | ||
var res = js2xmlparser(defaultRoot, { | ||
a: { | ||
"=": "b" | ||
} | ||
}, defaultOptions); | ||
res.should.equal("<base><b></b></base>"); | ||
}); | ||
it("should create XML with alias string '__alias' when options.aliasString is '__alias'", function () { | ||
var res = js2xmlparser(defaultRoot, { | ||
a: { | ||
"__alias": "b" | ||
} | ||
}, { | ||
declaration: { | ||
include: false | ||
}, | ||
prettyPrinting: { | ||
enabled: false | ||
}, | ||
aliasString: "__alias" | ||
}); | ||
res.should.equal("<base><b></b></base>"); | ||
}); | ||
it("should create XML with options.aliasString and data is an array", function () { | ||
var res = js2xmlparser(defaultRoot, { | ||
a: [{ | ||
"=": "b" | ||
}, { | ||
"=": "c" | ||
}] | ||
}, defaultOptions); | ||
res.should.equal("<base><b></b><c></c></base>"); | ||
}); | ||
}); | ||
}); | ||
@@ -1111,3 +1212,3 @@ | ||
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>" + | ||
"<l>n</l><l>o</l></k></base>"); | ||
"<l>n</l><l>o</l></k></base>"); | ||
}); | ||
@@ -1149,2 +1250,8 @@ | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
@@ -1161,7 +1268,7 @@ }, | ||
res.should.equal("<person type=\"individual\"><firstName>John</firstName><lastName>Smith</lastName>" + | ||
"<dateOfBirth>"+new Date(1964, 7, 26)+"</dateOfBirth><address " + | ||
"type=\"home\"><streetAddress>3212 22nd St</streetAddress><city>Chicago</city><state>Illinois" + | ||
"</state><zip>10000</zip></address><phone type=\"home\">123-555-4567</phone><phone type=\"cell\">" + | ||
"456-555-7890</phone><email>john@smith.com</email><notes>John's profile is not complete." + | ||
"</notes></person>"); | ||
"<dateOfBirth>" + new Date(1964, 7, 26) + "</dateOfBirth><address " + | ||
"type=\"home\"><streetAddress>3212 22nd St</streetAddress><city>Chicago</city><state>Illinois" + | ||
"</state><zip>10000</zip></address><phone type=\"home\">123-555-4567</phone><phone " + | ||
"type=\"work\">123-555-4567</phone><phone type=\"cell\">456-555-7890</phone><email>" + | ||
"john@smith.com</email><notes>John's profile is not complete.</notes></person>"); | ||
}); | ||
@@ -1195,2 +1302,8 @@ | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
@@ -1211,7 +1324,8 @@ }, | ||
res.should.equal("<person type=\"individual\">\n\t<firstName>John</firstName>\n\t<lastName>Smith" + | ||
"</lastName>\n\t<dateOfBirth>"+ new Date(1964, 7, 26) + | ||
"</dateOfBirth>\n\t<address type=\"home\">\n\t\t<streetAddress>3212 22nd St</streetAddress>" + | ||
"\n\t\t<city>Chicago</city>\n\t\t<state>Illinois</state>\n\t\t<zip>10000</zip>\n\t</address>" + | ||
"\n\t<phone type=\"home\">123-555-4567</phone>\n\t<phone type=\"cell\">456-555-7890</phone>\n\t" + | ||
"<email>john@smith.com</email>\n\t<notes>John's profile is not complete.</notes>\n</person>"); | ||
"</lastName>\n\t<dateOfBirth>" + new Date(1964, 7, 26) + | ||
"</dateOfBirth>\n\t<address type=\"home\">\n\t\t<streetAddress>3212 22nd St</streetAddress>" + | ||
"\n\t\t<city>Chicago</city>\n\t\t<state>Illinois</state>\n\t\t<zip>10000</zip>\n\t</address>" + | ||
"\n\t<phone type=\"home\">123-555-4567</phone>\n\t<phone type=\"work\">123-555-4567" + | ||
"</phone>\n\t<phone type=\"cell\">456-555-7890</phone>\n\t<email>john@smith.com</email>\n\t" + | ||
"<notes>John's profile is not complete.</notes>\n</person>"); | ||
}); | ||
@@ -1221,4 +1335,23 @@ | ||
var res = js2xmlparser("person", { | ||
"email": function() {return "john@smith.com";}, | ||
"dateOfBirth": new Date(1964, 7, 26) | ||
"phone": [ | ||
{ | ||
"@": { | ||
"type": "home" | ||
}, | ||
"#": "123-555-4567" | ||
}, | ||
{ | ||
"@": { | ||
"type": "work" | ||
}, | ||
"#": "123-555-4567", | ||
"=": "telephone" | ||
}, | ||
{ | ||
"@": { | ||
"type": "cell" | ||
}, | ||
"#": "456-555-7890" | ||
} | ||
] | ||
}, { | ||
@@ -1230,3 +1363,19 @@ declaration: { | ||
enabled: false | ||
} | ||
}); | ||
res.should.equal("<person><phone type=\"home\">123-555-4567</phone><telephone type=\"work\">" + | ||
"123-555-4567</telephone><phone type=\"cell\">456-555-7890</phone></person>"); | ||
}); | ||
it("should correctly parse example 4", function () { | ||
var res = js2xmlparser("person", { | ||
"email": function () {return "john@smith.com";}, | ||
"dateOfBirth": new Date(Date.UTC(1964, 7, 26)) | ||
}, { | ||
declaration: { | ||
include: false | ||
}, | ||
prettyPrinting: { | ||
enabled: false | ||
}, | ||
convertMap: { | ||
@@ -1236,3 +1385,3 @@ "[object Date]": function (date) { | ||
}, | ||
"[object Function]": function(func) { | ||
"[object Function]": function (func) { | ||
return func.toString(); | ||
@@ -1243,6 +1392,6 @@ } | ||
res.should.equal("<person><email>function () {return "john@smith.com";}</email>" + | ||
"<dateOfBirth>1964-08-26T04:00:00.000Z</dateOfBirth></person>"); | ||
"<dateOfBirth>1964-08-26T00:00:00.000Z</dateOfBirth></person>"); | ||
}); | ||
it("should correctly parse example 4", function () { | ||
it("should correctly parse example 5", function () { | ||
var res = js2xmlparser("person", { | ||
@@ -1253,3 +1402,3 @@ "notes": { | ||
}, | ||
"#":"John's profile is not complete." | ||
"#": "John's profile is not complete." | ||
} | ||
@@ -1266,3 +1415,3 @@ }, { | ||
res.should.equal("<person><notes type=\"status\"><![CDATA[John's profile is not complete.]]></notes>" + | ||
"</person>"); | ||
"</person>"); | ||
}); | ||
@@ -1269,0 +1418,0 @@ }); |
95170
1753
244