Skip to content

Commit 8a8a3ba

Browse files
committed
feat!: Only change attribute names when creating JSON
BREAKING CHANGE The node match exactly the XML in order to easily filter it using callback
1 parent 443ae73 commit 8a8a3ba

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

src/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { traversableToJSON } from './traversableToJSON';
66
* Parse an ArrayBuffer or Uint8Array representing an XML
77
* @param {ArrayBuffer|Uint8Arra} xmlData
88
* @param {object} [options={}]
9-
* @param {string} [attributeNamePrefix='@_']
9+
* @param {string} [attributeNamePrefix='$']
1010
* @param {boolean} [attributesNodeName=false]
1111
* @param {string} [textNodeName='#text']
1212
* @param {boolean} [trimValues=true] should we remove ascii < 32

src/traversable/parseAttributesString.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parseString } from 'dynamic-typing';
22

3-
import { getAllMatches } from '../util';
3+
import { getAllMatches, isEmptyObject } from '../util';
44

55
const newLocal = '([^\\s=]+)\\s*(=\\s*([\'"])(.*?)\\3)?';
66
const attrsRegx = new RegExp(newLocal, 'g');
@@ -22,23 +22,16 @@ export function parseAttributesString(string, options) {
2222
match[4] = match[4].trim();
2323
}
2424
match[4] = options.attributeValueProcessor(match[4], attrName);
25-
attributes[options.attributeNamePrefix + attrName] = stringParseValue(
25+
attributes[attrName] = stringParseValue(
2626
match[4],
2727
options.dynamicTypingAttributeValue,
2828
);
2929
} else if (options.allowBooleanAttributes) {
30-
attributes[options.attributeNamePrefix + attrName] = true;
30+
attributes[attrName] = true;
3131
}
3232
}
3333
}
34-
if (!Object.keys(attributes).length) {
35-
return;
36-
}
37-
if (options.attributesNodeName) {
38-
const attrCollection = {};
39-
attrCollection[options.attributesNodeName] = attributes;
40-
return attrCollection;
41-
}
34+
if (isEmptyObject(attributes)) return;
4235
return attributes;
4336
}
4437

src/traversableToJSON.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,24 @@ export function traversableToJSON(node, options, parentTagName) {
3838
result[options.textNodeName] = asArray ? [node.value] : node.value;
3939
}
4040

41-
merge(result, node.attributes, arrayMode);
41+
if (node.attributes && !isEmptyObject(node.attributes)) {
42+
let attributes = options.parseAttributesString ? {} : node.attributes;
43+
if (options.attributeNamePrefix) {
44+
// need to rename the attributes
45+
const renamedAttributes = {};
46+
for (let key in node.attributes) {
47+
renamedAttributes[options.attributeNamePrefix + key] =
48+
node.attributes[key];
49+
}
50+
attributes = renamedAttributes;
51+
}
52+
if (options.attributesNodeName) {
53+
let encapsulatedAttributes = {};
54+
encapsulatedAttributes[options.attributesNodeName] = attributes;
55+
attributes = encapsulatedAttributes;
56+
}
57+
merge(result, attributes, arrayMode);
58+
}
4259

4360
const keys = Object.keys(node.children);
4461
for (let index = 0; index < keys.length; index++) {

src/util.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ export function isEmptyObject(obj) {
2525
/**
2626
* Copy all the properties of a into b.
2727
* @param {object} target
28-
* @param {object} a
28+
* @param {object} source
2929
*/
30-
export function merge(target, a, arrayMode) {
31-
if (!a) return;
32-
for (const key in a) {
30+
export function merge(target, source, arrayMode) {
31+
if (!source) return;
32+
for (const key in source) {
3333
if (arrayMode === 'strict') {
34-
target[key] = [a[key]];
34+
target[key] = [source[key]];
3535
} else {
36-
target[key] = a[key];
36+
target[key] = source[key];
3737
}
3838
}
3939
}

0 commit comments

Comments
 (0)