Skip to content

Commit

Permalink
Merge pull request #10 from ikr/master
Browse files Browse the repository at this point in the history
A new option: xmlDecl -- whether to include the XML declaration; true by default
  • Loading branch information
chilts committed Aug 14, 2013
2 parents a5b4e2c + 83e97e9 commit 9bd98c8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var convert = data2xml(); // or data2xml({})
var convert = require('data2xml')();
```

Note: in each example, I am leaving out the XML declaration. I am also pretty printing the output - the package doesn't
do this for you!
Note: in each example, I am leaving out the XML declaration (controlled by the `xmlDecl` option). I
am also pretty printing the output - the package doesn't do this for you!

```
var convert = require('data2xml')();
Expand Down
10 changes: 8 additions & 2 deletions data2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
var valid = {
'omit' : true, // no element is output : ''
'empty' : true, // an empty element is output : '<element></element>'
'closed' : true, // a closed element is output : '<element/>'
'closed' : true // a closed element is output : '<element/>'
};

var defaults = {
'attrProp' : '_attr',
'valProp' : '_value',
'undefined' : 'omit',
'null' : 'omit',
'xmlDecl' : true
};

var xmlHeader = '<?xml version="1.0" encoding="utf-8"?>\n';
Expand All @@ -29,6 +30,11 @@ module.exports = function(opts) {

opts.attrProp = opts.attrProp || defaults.attrProp;
opts.valProp = opts.valProp || defaults.valProp;

if (typeof opts.xmlDecl === 'undefined') {
opts.xmlDecl = defaults.xmlDecl;
}

if ( opts['undefined'] && valid[opts['undefined']] ) {
// nothing, this is fine
}
Expand All @@ -43,7 +49,7 @@ module.exports = function(opts) {
}

return function(name, data) {
var xml = xmlHeader;
var xml = opts.xmlDecl ? xmlHeader : '';
xml += makeElement(name, data, opts);
return xml;
};
Expand Down
53 changes: 31 additions & 22 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,26 @@ var declaration = '<?xml version="1.0" encoding="utf-8"?>\n';

// --------------------------------------------------------------------------------------------------------------------

var tests = [
{
name : 'one element structure with an xmlns',
element : 'topelement',
data : { '@' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' },
exp : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second></topelement>'
},
{
name : 'complex 4 element array with some attributes',
element : 'topelement',
data : { item : [
{ '@' : { type : 'a' }, '#' : 'val1' },
{ '@' : { type : 'b' }, '#' : 'val2' },
'val3',
{ '#' : 'val4' },
] },
exp : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>'
},
];

var convert = data2xml({ attrProp : '@', valProp : '#' });

test('some simple xml with \'@\' for attributes', function (t) {
var convert = data2xml({ attrProp : '@', valProp : '#' }),

tests = [{
name : 'one element structure with an xmlns',
element : 'topelement',
data : { '@' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' },
exp : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second></topelement>'
}, {
name : 'complex 4 element array with some attributes',
element : 'topelement',
data : { item : [
{ '@' : { type : 'a' }, '#' : 'val1' },
{ '@' : { type : 'b' }, '#' : 'val2' },
'val3',
{ '#' : 'val4' },
] },
exp : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>'
}];

tests.forEach(function(test) {
var xml = convert(test.element, test.data, { attrProp : '@', valProp : '#' });
t.equal(xml, test.exp, test.name);
Expand All @@ -49,4 +46,16 @@ test('some simple xml with \'@\' for attributes', function (t) {
t.end();
});

test('it\'s possible to omit the XML declaration', function (t) {
var convert = data2xml({xmlDecl: false});

t.equal(
convert('moo', {foo: 'bar', baz: 42}),
'<moo><foo>bar</foo><baz>42</baz></moo>',
'must not be declared as XML'
);

t.end();
});

// --------------------------------------------------------------------------------------------------------------------

0 comments on commit 9bd98c8

Please sign in to comment.