@abi-software/map-utilities 1.2.2-beta.2 → 1.2.2-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,47 @@ const capitalise = term => {
4
4
  return term;
5
5
  };
6
6
 
7
+ const convertNodeToObject = (node) => {
8
+ const obj = {};
9
+
10
+ if (node.attributes) {
11
+ for (let i = 0; i < node.attributes.length; i++) {
12
+ const attr = node.attributes[i];
13
+ obj[`@${attr.nodeName}`] = attr.nodeValue;
14
+ }
15
+ }
16
+
17
+ for (let i = 0; i < node.childNodes.length; i++) {
18
+ const child = node.childNodes[i];
19
+ if (child.nodeType === Node.ELEMENT_NODE) {
20
+ const childResult = convertNodeToObject(child);
21
+ if (obj[child.nodeName]) {
22
+ if (!Array.isArray(obj[child.nodeName])) {
23
+ obj[child.nodeName] = [obj[child.nodeName]];
24
+ }
25
+ obj[child.nodeName].push(childResult);
26
+ } else {
27
+ obj[child.nodeName] = childResult;
28
+ }
29
+ } else if (child.nodeType === Node.TEXT_NODE && child.nodeValue.trim() !== '') {
30
+ return child.nodeValue.trim();
31
+ }
32
+ }
33
+
34
+ return obj;
35
+ };
36
+
37
+ const xmlToJSON = (xmlText) => {
38
+ const parser = new DOMParser();
39
+ const xmlDoc = parser.parseFromString(xmlText, "text/xml");
40
+
41
+ const result = {};
42
+ result[xmlDoc.documentElement.nodeName] = convertNodeToObject(xmlDoc.documentElement);
43
+
44
+ return result;
45
+ };
46
+
7
47
  export {
8
48
  capitalise,
49
+ xmlToJSON,
9
50
  };