Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit 59e95aa

Browse files
committed
minor style adjustments and adds an options parameters
1 parent caecce8 commit 59e95aa

1 file changed

Lines changed: 53 additions & 43 deletions

File tree

lib/index.js

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,88 @@ var parser = new expat.Parser('UTF-8');
44

55
// This object will hold the final result.
66
var obj = {};
7-
var currentParent = obj;
7+
var currentObject = obj;
88
var ancestors = [];
99

10+
var options = {}; //configuration options
1011
function startElement(name, attrs) {
11-
if (! (name in currentParent)) {
12-
currentParent[name] = attrs;
13-
} else if (!(currentParent[name] instanceof Array)) {
14-
// Put the existing object in an array.
15-
var newArray = [currentParent[name]];
16-
// Add the new object to the array.
17-
newArray.push(attrs);
18-
// Point to the new array.
19-
currentParent[name] = newArray;
20-
} else {
21-
// An array already exists, push the attributes on to it.
22-
currentParent[name].push(attrs);
23-
}
12+
if (! (name in currentObject)) {
13+
currentObject[name] = attrs;
14+
} else if (! (currentObject[name] instanceof Array)) {
15+
// Put the existing object in an array.
16+
var newArray = [currentObject[name]];
17+
// Add the new object to the array.
18+
newArray.push(attrs);
19+
// Point to the new array.
20+
currentObject[name] = newArray;
21+
} else {
22+
// An array already exists, push the attributes on to it.
23+
currentObject[name].push(attrs);
24+
}
2425

25-
// Store the current (old) parent.
26-
ancestors.push(currentParent);
26+
// Store the current (old) parent.
27+
ancestors.push(currentObject);
2728

28-
// We are now working with this object, so it becomes the current parent.
29-
if (currentParent[name] instanceof Array) {
30-
// If it is an array, get the last element of the array.
31-
currentParent = currentParent[name][currentParent[name].length - 1];
32-
} else {
33-
// Otherwise, use the object itself.
34-
currentParent = currentParent[name];
35-
}
29+
// We are now working with this object, so it becomes the current parent.
30+
if (currentObject[name] instanceof Array) {
31+
// If it is an array, get the last element of the array.
32+
currentObject = currentObject[name][currentObject[name].length - 1];
33+
} else {
34+
// Otherwise, use the object itself.
35+
currentObject = currentObject[name];
36+
}
3637
}
3738

38-
function charData(data) {
39+
function text(data) {
3940
data = data.trim();
4041
if (!data.length) {
4142
return;
4243
}
43-
currentParent['$t'] = data;
44+
currentObject['$t'] = data;
4445
}
4546

4647
function endElement(name) {
4748
// This should check to make sure that the name we're ending
4849
// matches the name we started on.
49-
5050
var ancestor = ancestors.pop();
51-
if ((Object.keys(currentParent).length == 1) && ('$t' in currentParent)) {
52-
if (ancestor[name] instanceof Array) {
53-
//console.log("list-replacing $t in " + name);
54-
ancestor[name].push(ancestor[name].pop()['$t']);
51+
if (!options.reversible) {
52+
if ((Object.keys(currentObject).length == 1) && ('$t' in currentObject)) {
53+
if (ancestor[name] instanceof Array) {
54+
//console.log("list-replacing $t in " + name);
55+
ancestor[name].push(ancestor[name].pop()['$t']);
56+
} else {
57+
//console.log("replacing $t in " + name);
58+
ancestor[name] = currentObject['$t'];
59+
}
5560
} else {
56-
//console.log("replacing $t in " + name);
57-
ancestor[name] = currentParent['$t'];
61+
//console.log("final " + name + ":");
62+
//console.log(currentObject);
5863
}
59-
} else {
60-
//console.log("final " + name + ":");
61-
//console.log(currentParent);
6264
}
6365

64-
currentParent = ancestor;
66+
currentObject = ancestor;
6567
}
6668

6769
parser.on('startElement', startElement);
68-
parser.on('text', charData);
70+
parser.on('text', text);
6971
parser.on('endElement', endElement);
7072

71-
module.exports.toJson = function(xml, _object) {
72-
_object = _object || false;
73-
if(parser.parse(xml)) {
74-
if(_object) {
73+
module.exports.toJson = function(xml, _options) {
74+
options = null;
75+
options = {
76+
object: false,
77+
reversible: false
78+
}
79+
80+
for (var opt in _options) {
81+
options[opt] = _options[opt];
82+
}
83+
84+
if (parser.parse(xml)) {
85+
if (options.object) {
7586
return obj;
7687
}
7788
return JSON.stringify(obj);
7889
}
7990
};
8091

81-

0 commit comments

Comments
 (0)