Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first test ok

  • Loading branch information...
commit 858a45664198fe234a093b52910f4914c3abdf18 1 parent 80ec9ed
Nicolas Thouvenin touv authored
Showing with 33 additions and 13 deletions.
  1. +1 −1  index.js
  2. +20 −11 lib/xml-mapping.js
  3. +12 −1 test/toxml.js
2  index.js
View
@@ -1 +1 @@
-module.exports = require('./lib/xml-mapping');
+module.exports = require('./lib/xml-mapping.js');
31 lib/xml-mapping.js
View
@@ -1,4 +1,4 @@
-var save = function(obj, addHeader) {
+exports.dump = function(obj, addHeader) {
var out = addHeader ? '<?xml version="1.0" encoding="UTF-8"?>\n' : '';
var default_tag_name = 'row';
@@ -26,6 +26,7 @@ var save = function(obj, addHeader) {
var parse = function(o, n) {
var type = getype(o);
+ var numobj = 0;
if (type != 'array' && type != 'object') {
write('<[DATA[ ' + getval(o) + ' ]]>');
}
@@ -45,7 +46,7 @@ var save = function(obj, addHeader) {
else if (type == 'object') {
for(var key in o){
if (o.hasOwnProperty(key) && o[key]){
- var tv = gettype(o[key]);
+ var tv = getype(o[key]);
if (tv != 'object') {
if (key == '$text') {
write(getval(o[key]));
@@ -55,14 +56,22 @@ var save = function(obj, addHeader) {
}
else if (type != 'array') {
// TODO special attributes
- write(key + '=' + quoting(getval(o[key])));
+ write(' '+key + '=' + quoting(getval(o[key])));
}
else if (type == 'array') {
parse(o[key], key);
}
- else if (tv == 'object') {
- write('<'+key+'>');
- parse(o[key], key);
+ else {
+ // ...
+ }
+ }
+ else {
+ ++numobj;
+ write('<'+key);
+ if (parse(o[key], key) == 0) {
+ write('/>');
+ }
+ else {
write('</'+key+'>');
}
}
@@ -72,18 +81,18 @@ var save = function(obj, addHeader) {
}
}
+ return numobj;
}
var o= {};
- if (obj.length == 1) {
+ if (Object.keys(obj).length == 1) {
o = obj;
}
else {
- o[this.default_tag_name] = obj;
- }
- parse(o, this.default_tag_name);
+ o[default_tag_name] = obj;
+ }
+ parse(o, default_tag_name);
return out;
};
-exports.toXML = save;
13 test/toxml.js
View
@@ -1,6 +1,6 @@
var XMLMapping = require('../');
-exports['test'] = function (test) {
+exports['t01'] = function (test) {
var data = {
key : {
key1: 'value'
@@ -10,3 +10,14 @@ exports['test'] = function (test) {
test.equal(string, '<key key1="value"/>')
test.done();
};
+exports['t02'] = function (test) {
+ var data = {
+ key : {
+ key1: 'value1',
+ key2: 'value2',
+ }
+ };
+ var string = XMLMapping.dump(data);
+ test.equal(string, '<key key1="value1" key2="value2"/>')
+ test.done();
+};
Please sign in to comment.
Something went wrong with that request. Please try again.