Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: supports the ability to serialize to xs:sequence types #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions jxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* bugfixes and code cleanup by user @laubstein
* https://github.com/tyrasd/jxon/pull/32
*
* adapted for nodejs and npm by @tyrasd (Martin Raifer <[email protected]>)
* adapted for nodejs and npm by @tyrasd (Martin Raifer <[email protected]>)
*/

(function(root, factory) {
Expand Down Expand Up @@ -54,7 +54,8 @@
trueIsEmpty: false,
autoDate: false,
ignorePrefixedNodes: false,
parseValues: false
parseValues: false,
sequenceKey: '_sequence'
};
var aCache = [];
var rIsNull = /^\s*$/;
Expand Down Expand Up @@ -228,18 +229,49 @@
} else if (oParentObj.constructor === Date) {
oParentEl.appendChild(oXMLDoc.createTextNode(oParentObj.toISOString()));
}
for (var sName in oParentObj) {

// Pull out properties names
var propNames = Object.getOwnPropertyNames(oParentObj);

// Look for optional _sequence
if (oParentObj.hasOwnProperty(opts.sequenceKey)) {

var sequence = oParentObj[opts.sequenceKey];

// sort prop names based on _sequence
propNames.sort(function(a, b) {

// Sort comparator
// find index of A and B
var aIndex = sequence.indexOf(a);
var bIndex = sequence.indexOf(b);

// If prop not named, put on end
if (aIndex < 0 || bIndex < 0) {
return -1;
}

// Subtract the indexes
return aIndex - bIndex;
})

}

propNames.forEach(function(sName, index) {

vValue = oParentObj[sName];
if ( vValue === undefined ) {
continue;
return;
}
if ( vValue === null ) {
vValue = {};
}
if ( sName === opts.sequenceKey) {
return;
}

if (isFinite(sName) || vValue instanceof Function) {
continue;
return;
}

/* verbosity level is 0 */
Expand Down Expand Up @@ -288,7 +320,7 @@
}
oParentEl.appendChild(oChild);
}
}
});
}
this.xmlToJs = this.build = function(oXMLParent, nVerbosity /* optional */ , bFreeze /* optional */ , bNesteAttributes /* optional */ ) {
var _nVerb = arguments.length > 1 && typeof nVerbosity === 'number' ? nVerbosity & 3 : /* put here the default verbosity level: */ 1;
Expand Down
23 changes: 18 additions & 5 deletions jxon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('JXON', function() {
});

var strTwo = JXON.jsToString(JXON.stringToJs('<element><a>first position</a><a>second position</a></element>'));

assert.equal(strOne, strTwo);
});

Expand Down Expand Up @@ -156,5 +156,25 @@ describe('JXON', function() {

assert.equal(strNull, strEmptyObj);
});
});

describe('xml sequences', function() {
it('as property', function () {
var strNull = JXON.jsToString({
'SequencedJS': {
'element2': 2,
'element1': 1,
'_sequence': ['element1', 'element2'],
'elementWithSequence': {
'element1': 1,
'element2': 2
}
}
});
var strEmptyObj = '<SequencedJS><element1>1</element1><element2>2</element2><elementWithSequence><element1>1</element1><element2>2</element2></elementWithSequence></SequencedJS>';

assert.equal(strNull, strEmptyObj);
});

});
});