dynamic-xml-builder
Advanced tools
Comparing version
@@ -21,2 +21,3 @@ "use strict"; | ||
attrKey: null, | ||
valueSel: '_value', | ||
defVal: '', | ||
@@ -32,2 +33,3 @@ declaration: null | ||
* @property {String} attrKey attribute key to use when grouping attribute keys under object | ||
* @property {String} valueSel value selector | ||
* @property {String} defVal default value to use when element has no declared value | ||
@@ -171,5 +173,4 @@ * @property {Boolean|String} declaration true for default declaration, string for override | ||
getAttributeKeys() { | ||
let This = this; | ||
return Object.keys(this.elements) | ||
.filter(e => e[0] === This.options.attrSel) | ||
.filter(e => e[0] === this.options.attrSel && e !== this.options.valueSel) | ||
.map(e => e.substring(1)); | ||
@@ -179,7 +180,10 @@ } | ||
getElementKeys() { | ||
let This = this; | ||
return Object.keys(this.elements) | ||
.filter(e => e[0] !== This.options.attrSel); | ||
.filter(e => e[0] !== this.options.attrSel && e !== this.options.valueSel); | ||
} | ||
getElementValue() { | ||
return this.elements[this.options.valueSel]; | ||
} | ||
areElementsArray() { | ||
@@ -216,2 +220,3 @@ let elemKeys = this.getElementKeys(); | ||
let elemKeys = this.getElementKeys(); | ||
let elemVal = this.getElementValue(); | ||
@@ -263,3 +268,3 @@ let xml = '', preindent = ''; | ||
if (!elemKeys.length) { | ||
if (!elemKeys.length && !elemVal) { | ||
xml += '/>'; | ||
@@ -273,2 +278,10 @@ return xml; | ||
if (elemVal) { | ||
xml += elemVal; | ||
// wrap with parent | ||
xml += '</' + name + '>'; | ||
return xml; | ||
} | ||
// map elements | ||
@@ -306,2 +319,3 @@ let elemObjs = elemKeys.map(k => { | ||
let elemKeys = this.getElementKeys(); | ||
let elemVal = this.getElementValue(); | ||
@@ -367,2 +381,7 @@ let self = current || {}; | ||
if (elemVal) { | ||
obj[_options.valueSel] = elemVal; | ||
return self; | ||
} | ||
elemKeys.forEach(k => | ||
@@ -369,0 +388,0 @@ (!elements[k] || typeof(elements[k]) !== 'object') ? |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "dynamic XML builder", | ||
@@ -9,0 +9,0 @@ "repository": { |
@@ -137,2 +137,3 @@ dynamic-xml-builder | ||
attrSel|"_"|constructor|used to identify attributes (attrSel + attributeName, i.e. "_charset") | ||
valueSel|"_value"|constructor|used to set the node value directly | ||
defVal|""|constructor|default value to use when element value has not been provided | ||
@@ -139,0 +140,0 @@ indent|"\t"|toXML|indent definition, can be any string |
@@ -108,3 +108,11 @@ "use strict"; | ||
test('direct value assignment', function() { | ||
xmlo = new XMLObject('test', {indent: '', newLine: ''}); | ||
xmlo._value = 'TEST'; | ||
assert.equal(xmlo.toXML(), '<test>TEST</test>'); | ||
assert.deepEqual(xmlo.toObject(), {test: {_value: 'TEST'}}); | ||
}) | ||
test('attribute assignment', function() { | ||
@@ -111,0 +119,0 @@ |
26958
3.39%539
4.05%153
0.66%