Comparing version 0.0.2 to 0.0.3
243
jsontoxml.js
@@ -1,163 +0,120 @@ | ||
/* | ||
copyright Ryan Day 2010 <http://ryanday.org> [MIT Licensed] | ||
//copyright Ryan Day 2010 <http://ryanday.org> [MIT Licensed] | ||
THIS MODULE EXPORTS 2 METHODS: | ||
exports.json_to_xml | ||
exports.obj_to_xml | ||
ARGUMENTS: | ||
obj | ||
the object you would like to render to xml | ||
in the case of json_to_xml this is a string which is "JSON.parse"d for you | ||
add_xml_header | ||
boolean if you would like the xml header included in the output | ||
NOTE: you can always add it you your output by passing it in your input if the default doesnt work for you | ||
'<?xml version="1.0" encoding="utf-8"?>' | ||
var process_to_xml = function(node_data,options){ | ||
EXAMPLE USE: | ||
return (function fn(node_data,node_descriptor){ | ||
var xml = "" | ||
, type = typeof node_data | ||
; | ||
jsonxml.obj_to_xml({ | ||
node:'text content', | ||
parent:[ | ||
{name:'taco',text:'beef taco',children:{salsa:'hot!'}}, | ||
{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[ | ||
{name:'salsa',text:'mild'}, | ||
'hi', | ||
{name:'salsa',text:'weak',attrs:{type:2}} | ||
]}, | ||
{name:'taco',attrs:'mood="party!"'} | ||
], | ||
parent2:{ | ||
hi:'is a nice thing to say', | ||
node:'i am another not special child node' | ||
date:function(){ | ||
return (new Date())+''; | ||
} | ||
} | ||
}) | ||
//if value is an array create child nodes from values | ||
if(node_data instanceof Array){ | ||
node_data.map(function(v){ | ||
xml += fn(v,1); | ||
//entries that are values of an array are the only ones that can be special node descriptors | ||
}); | ||
} else if(node_data instanceof Date) { | ||
// cast dates to ISO 8601 date (soap likes it) | ||
xml += node_data.toJSON?node_data.toJSON():node_data+''; | ||
} else if (type == 'object'){ | ||
outputs: // ! output is not tabbed this is an example | ||
if(node_descriptor == 1 && node_data.name){ | ||
var content = "" | ||
, name = node_data.name | ||
, attributes = "" | ||
; | ||
<node>text content</node> | ||
<parent> | ||
<taco> | ||
beef taco | ||
<salsa>hot!</salsa> | ||
</taco> | ||
<taco> | ||
fish taco | ||
<salsa>mild</salsa> | ||
hi | ||
<salsa type="2">weak</salsa> | ||
</taco> | ||
<taco mood='party!'/> | ||
</parent> | ||
<parent2> | ||
<hi>is a nice thing to say</hi> | ||
<node>i am another not special child node</node> | ||
<date>Sun Sep 26 2010 17:27:29 GMT-0700 (PDT)</date> | ||
</parent2> | ||
if(node_data.attrs) { | ||
if(typeof node_data.attrs != 'object') { | ||
attributes +=' '+node_data.attrs; | ||
node_data.attrs = {}; | ||
} | ||
var attrs = node_data.attrs; | ||
<node>text content</node> | ||
<parent> | ||
<taco>beef taco</taco> | ||
<taco mood="sad"> | ||
fish taco | ||
<salsa>mild</salsa> | ||
hi | ||
<salsa type="2">weak</salsa> | ||
</taco> | ||
<taco mood="party!"/> | ||
</parent> | ||
<parent2> | ||
<hi>is a nice thing to say</hi> | ||
<node>i am another not special child node</node> | ||
<date>Mon Sep 27 2010 19:04:44 GMT-0700 (PDT)</date> | ||
</parent2> | ||
*/ | ||
var process_to_xml = function fn(node_data,node_descriptor){ | ||
var xml = ""; | ||
//if value is an array create child nodes from values | ||
if(node_data instanceof Array){ | ||
node_data.map(function(v){ | ||
xml += fn(v,1); | ||
//entries that are values of an array are the only ones that can be special node descriptors | ||
}); | ||
} else { | ||
switch(typeof node_data){ | ||
case 'object': | ||
if(node_descriptor == 1 && node_data.name){ | ||
var | ||
content = "", | ||
name = node_data.name, | ||
attributes = ""; | ||
for(var i in attrs){ | ||
attributes += ' '+i+'="'+(options.esc?esc(attrs[i]):attrs[i])+'"'; | ||
} | ||
} | ||
if(node_data.attrs) { | ||
if(typeof node_data.attrs != 'object') { | ||
attributes +=' '+node_data.attrs; | ||
node_data.attrs = {}; | ||
} | ||
//later attributes can be added here | ||
if(node_data.text || node_data.value) { | ||
var c = (node_data.value || '')+(node_data.text || ''); | ||
content += (options.escape?esc(c):c); | ||
} | ||
var attrs = node_data.attrs; | ||
if(node_data.children){ | ||
content += fn(node_data.children); | ||
} | ||
for(var i in attrs){ | ||
attributes += ' '+i+'="'+attrs[i]+'"'; | ||
} | ||
} | ||
//later attributes can be added here | ||
if(node_data.text || node_data.value) { | ||
content += (node_data.value || '')+(node_data.text || ''); | ||
} | ||
if(node_data.children){ | ||
content += fn(node_data.children); | ||
} | ||
if(content.length) { | ||
xml +='<'+name+attributes+'>'+content+'</'+name+'>'; | ||
} else { | ||
xml +='<'+name+attributes+'/>'; | ||
} | ||
if(content.length) { | ||
xml +='<'+name+attributes+'>'+content+'</'+name+'>'; | ||
} else { | ||
xml +='<'+name+attributes+'/>'; | ||
} | ||
} else { | ||
} else { | ||
for( var i in node_data){ | ||
var content = fn(node_data[i]); | ||
if(content.length) { | ||
xml +='<'+i+'>'+content+'</'+i+'>'; | ||
} else { | ||
xml +='<'+i+'/>'; | ||
} | ||
} | ||
} | ||
break; | ||
case 'function': | ||
xml += node_data(xml,fn); | ||
break; | ||
default: | ||
xml += node_data+''; | ||
} | ||
} | ||
return xml; | ||
for( var i in node_data){ | ||
var content = fn(node_data[i]); | ||
if(content.length) { | ||
xml +='<'+i+'>'+content+'</'+i+'>'; | ||
} else { | ||
xml +='<'+i+'/>'; | ||
} | ||
} | ||
} | ||
} else if (type == 'function'){ | ||
xml += node_data(xml,fn); | ||
} else { | ||
xml += options.escape?esc(node_data+''):node_data+''; | ||
} | ||
return xml; | ||
}(node_data)) | ||
}; | ||
var xml_header = '<?xml version="1.0" encoding="utf-8"?>'; | ||
module.exports = function(obj,options){ | ||
exports.json_to_xml = function(json,add_xml_header){ | ||
try{ | ||
var obj = JSON.parse(json); | ||
} catch(e){ | ||
return false; | ||
} | ||
if(typeof obj == 'string' || obj instanceof Buffer) { | ||
try{ | ||
obj = JSON.parse(obj.toString()); | ||
} catch(e){ | ||
return false; | ||
} | ||
} | ||
return this.obj_to_xml(obj,add_xml_header); | ||
var xmlheader = ''; | ||
if(options && typeof options == 'object') { | ||
xmlheader = options.xmlHeader?xml_header:''; | ||
} else if(options) xmlheader = xml_header; | ||
var xml = process_to_xml(obj,options||{}); | ||
return xmlheader+xml; | ||
} | ||
exports.obj_to_xml = function(obj,add_xml_header){ | ||
return (add_xml_header?xml_header:'')+process_to_xml(obj); | ||
} | ||
module.exports.json_to_xml= | ||
module.exports.obj_to_xml = module.exports; | ||
module.exports.escape = esc; | ||
function esc(str){ | ||
return str.replace(/&/g, '&') | ||
.replace(/</g, '<') | ||
.replace(/>/g, '>') | ||
.replace(/"/g, '"'); | ||
} | ||
module.exports.cdata = cdata; | ||
function cdata(str){ | ||
return "<!CDATA[["+str.replace(/]]>/g,'')+']]>'; | ||
}; | ||
{ | ||
"name":"jsontoxml", | ||
"version":"0.0.2", | ||
"description":"This renders a simple javascript object structure into reasonably complicated xml/html. js objects are easier to modify than strings so no need to parse a whole dom to reliably add a few elements", | ||
"homepage":"http://github.com/soldair/node-jsontoxml", | ||
"main":"./jsontoxml.js", | ||
"people":{ | ||
"author":{ | ||
"name":"Ryan Day", | ||
"email":"soldair@gmail.com", | ||
"url":"http://ryanday.org" | ||
} | ||
}, | ||
"repository":{ | ||
"type":"git", | ||
"url":"git://github.com/soldair/node-jsontoxml.git" | ||
}, | ||
"engines":{"node":">=0.2.0"} | ||
"name":"jsontoxml", | ||
"version":"0.0.3", | ||
"description":"This is a library designed to render js objects as xml. Its not made to parse or otherwise edit existing xml/html structures.", | ||
"homepage":"http://github.com/soldair/node-jsontoxml", | ||
"main":"./jsontoxml.js", | ||
"devDependencies":{ | ||
"tap":"*" | ||
}, | ||
"scripts":{ | ||
"test":"tap ./test.js" | ||
}, | ||
"people":{ | ||
"author":{ | ||
"name":"Ryan Day", | ||
"email":"soldair@gmail.com", | ||
"url":"http://ryanday.org" | ||
} | ||
}, | ||
"repository":{ | ||
"type":"git", | ||
"url":"git://github.com/soldair/node-jsontoxml.git" | ||
}, | ||
"license":"MIT", | ||
"engines":{"node":">=0.2.0"} | ||
} |
102
test.js
@@ -1,63 +0,55 @@ | ||
/* | ||
copyright Ryan Day 2010 <http://ryanday.org> [MIT Licensed] | ||
//copyright Ryan Day 2010 <http://ryanday.org> [MIT Licensed] | ||
no testing framework required! | ||
var test = require('tap').test | ||
, jsonxml = require("./jsontoxml.js") | ||
; | ||
HOW TO: | ||
node test.js | ||
*/ | ||
var jsonxml = require("./jsontoxml.js"); | ||
var date = (new Date())+''; | ||
var date = (new Date()); | ||
var input = { | ||
node:'text content', | ||
parent:[ | ||
{name:'taco',text:'beef taco',children:{salsa:'hot!'}}, | ||
{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[ | ||
{name:'salsa',text:'mild'}, | ||
'hi', | ||
{name:'salsa',text:'weak',attrs:{type:2}} | ||
]}, | ||
{name:'taco',attrs:"mood='party!'"} | ||
], | ||
parent2:{ | ||
hi:'is a nice thing to say', | ||
node:'i am another not special child node', | ||
date:function(){ | ||
return date; | ||
} | ||
} | ||
node:'text content', | ||
parent:[ | ||
{name:'taco',text:'beef taco',children:{salsa:'hot!'}}, | ||
{name:'taco',text:'fish taco',attrs:{mood:'sad'},children:[ | ||
{name:'salsa',text:'mild'}, | ||
'hi', | ||
{name:'salsa',text:'weak',attrs:{type:2}} | ||
]}, | ||
{name:'taco',attrs:{mood:"party!"}} | ||
], | ||
parent2:{ | ||
hi:'this & this is a nice thing to say', | ||
node:'i am another not special child node', | ||
date:function(){ | ||
return date+''; | ||
}, | ||
date2:date | ||
} | ||
}; | ||
var expected = '<node>text content</node>' | ||
+'<parent>' | ||
+'<taco>' | ||
+'beef taco' | ||
+'<salsa>hot!</salsa>' | ||
+'</taco>' | ||
+'<taco mood="sad">' | ||
+'fish taco' | ||
+'<salsa>mild</salsa>' | ||
+'hi' | ||
+'<salsa type="2">weak</salsa>' | ||
+'</taco>' | ||
+"<taco mood='party!'/>" | ||
+'</parent>' | ||
+'<parent2>' | ||
+'<hi>is a nice thing to say</hi>' | ||
+'<node>i am another not special child node</node>' | ||
+'<date>'+date+'</date>' | ||
+'</parent2>'; | ||
+'<parent>' | ||
+'<taco>' | ||
+'beef taco' | ||
+'<salsa>hot!</salsa>' | ||
+'</taco>' | ||
+'<taco mood="sad">' | ||
+'fish taco' | ||
+'<salsa>mild</salsa>' | ||
+'hi' | ||
+'<salsa type="2">weak</salsa>' | ||
+'</taco>' | ||
+"<taco mood=\"party!\"/>" | ||
+'</parent>' | ||
+'<parent2>' | ||
+'<hi>this & this is a nice thing to say</hi>' | ||
+'<node>i am another not special child node</node>' | ||
+'<date>'+date+'</date>' | ||
+'<date2>'+date.toJSON()+'</date2>' | ||
+'</parent2>'; | ||
var result = jsonxml.obj_to_xml(input); | ||
test("creates correct object",function(t){ | ||
var result = jsonxml(input,{escape:true}); | ||
t.equals(result,expected,' should have generated correct xml'); | ||
t.end() | ||
}); | ||
if(result == expected){ | ||
console.log('PASSED!'); | ||
} else { | ||
console.log('FAILED!'); | ||
console.log(result); | ||
console.log('did not match expected!'); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
8554
7
91
0
1
141
1