-
Notifications
You must be signed in to change notification settings - Fork 28
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
Attributes are not included in simple result for elements without children #19
Comments
yes, I would also expect that. I will look into this later this weekend. |
Actually it is a feature, But this is a situation where some decision need to be made, and there will always be situations that some developers would expect the opposite (not the fault of the developer). To get the result you expected, you can use the following method it return what is expected, but other developers would think "why is errors now an object and not a string". function logicalSimplify(children) {
var out = {};
if (children.length === 1 && typeof children[0] == 'string') {
return children[0];
}
// map each object
children.forEach(function(child) {
if (typeof child !== 'object') {
return;
}
if (!out[child.tagName])
out[child.tagName] = [];
var kids = logicalSimplify(child.children);
out[child.tagName].push(kids);
if (Object.keys(child.attributes).length) {
kids._attributes = child.attributes;
}
});
for (var i in out) {
if (out[i].length == 1) {
out[i] = out[i][0];
}
}
return out;
}; To resolve that issue, I made the |
That's the problem with xml=>json, it's hard to tell intent. The nice thing is that using the |
Wasn't sure whether this was a feature or bug, but I'm thinking it might be the latter.
Here is the input
and this is the output
if I take out the
anything
tags, themsg
attribute is not included anymoreresult:
but I'm expecting:
It looks like the presence of an additional child tag is required to treat the element as an object instead of as a string value
The text was updated successfully, but these errors were encountered: