• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalIntelligence / fast-xml-parser / 14973142022

12 May 2025 01:11PM CUT coverage: 97.547%. Remained the same
14973142022

push

github

web-flow
export types in fxp.d.ts for better module usability (#744)

1090 of 1134 branches covered (96.12%)

8908 of 9132 relevant lines covered (97.55%)

488239.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.16
/src/xmlparser/node2json.js
1
'use strict';
5✔
2

5✔
3
import XmlNode from './xmlNode.js';
5✔
4

5✔
5
const METADATA_SYMBOL = XmlNode.getMetaDataSymbol();
5✔
6

5✔
7
/**
5✔
8
 * 
5✔
9
 * @param {array} node 
5✔
10
 * @param {any} options 
5✔
11
 * @returns 
5✔
12
 */
5✔
13
export default function prettify(node, options){
5✔
14
  return compress( node, options);
670✔
15
}
670✔
16

5✔
17
/**
5✔
18
 * 
5✔
19
 * @param {array} arr 
5✔
20
 * @param {object} options 
5✔
21
 * @param {string} jPath 
5✔
22
 * @returns object
5✔
23
 */
5✔
24
function compress(arr, options, jPath){
3,490✔
25
  let text;
3,490✔
26
  const compressedObj = {};
3,490✔
27
  for (let i = 0; i < arr.length; i++) {
3,490✔
28
    const tagObj = arr[i];
4,610✔
29
    const property = propName(tagObj);
4,610✔
30
    let newJpath = "";
4,610✔
31
    if(jPath === undefined) newJpath = property;
4,610✔
32
    else newJpath = jPath + "." + property;
3,830✔
33

4,610✔
34
    if(property === options.textNodeName){
4,610✔
35
      if(text === undefined) text = tagObj[property];
1,790✔
36
      else text += "" + tagObj[property];
165✔
37
    }else if(property === undefined){
4,610!
38
      continue;
×
39
    }else if(tagObj[property]){
2,820✔
40
      
2,820✔
41
      let val = compress(tagObj[property], options, newJpath);
2,820✔
42
      const isLeaf = isLeafTag(val, options);
2,820✔
43
      if (tagObj[METADATA_SYMBOL] !== undefined) {
2,820✔
44
        val[METADATA_SYMBOL] = tagObj[METADATA_SYMBOL]; // copy over metadata
60✔
45
      }
60✔
46

2,820✔
47
      if(tagObj[":@"]){
2,820✔
48
        assignAttributes( val, tagObj[":@"], newJpath, options);
600✔
49
      }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
2,820✔
50
        val = val[options.textNodeName];
1,185✔
51
      }else if(Object.keys(val).length === 0){
2,220✔
52
        if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
350✔
53
        else val = "";
335✔
54
      }
350✔
55

2,820✔
56
      if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
2,820✔
57
        if(!Array.isArray(compressedObj[property])) {
315✔
58
            compressedObj[property] = [ compressedObj[property] ];
240✔
59
        }
240✔
60
        compressedObj[property].push(val);
315✔
61
      }else{
2,820✔
62
        //TODO: if a node is not an array, then check if it should be an array
2,505✔
63
        //also determine if it is a leaf node
2,505✔
64
        if (options.isArray(property, newJpath, isLeaf )) {
2,505✔
65
          compressedObj[property] = [val];
100✔
66
        }else{
2,505✔
67
          compressedObj[property] = val;
2,405✔
68
        }
2,405✔
69
      }
2,505✔
70
    }
2,820✔
71
    
4,610✔
72
  }
4,610✔
73
  // if(text && text.length > 0) compressedObj[options.textNodeName] = text;
3,490✔
74
  if(typeof text === "string"){
3,490✔
75
    if(text.length > 0) compressedObj[options.textNodeName] = text;
1,200✔
76
  }else if(text !== undefined) compressedObj[options.textNodeName] = text;
3,490✔
77
  return compressedObj;
3,490✔
78
}
3,490✔
79

5✔
80
function propName(obj){
4,610✔
81
  const keys = Object.keys(obj);
4,610✔
82
  for (let i = 0; i < keys.length; i++) {
4,610✔
83
    const key = keys[i];
4,610✔
84
    if(key !== ":@") return key;
4,610✔
85
  }
4,610✔
86
}
4,610!
87

5✔
88
function assignAttributes(obj, attrMap, jpath, options){
600✔
89
  if (attrMap) {
600✔
90
    const keys = Object.keys(attrMap);
600✔
91
    const len = keys.length; //don't make it inline
600✔
92
    for (let i = 0; i < len; i++) {
600✔
93
      const atrrName = keys[i];
890✔
94
      if (options.isArray(atrrName, jpath + "." + atrrName, true, true)) {
890✔
95
        obj[atrrName] = [ attrMap[atrrName] ];
20✔
96
      } else {
890✔
97
        obj[atrrName] = attrMap[atrrName];
870✔
98
      }
870✔
99
    }
890✔
100
  }
600✔
101
}
600✔
102

5✔
103
function isLeafTag(obj, options){
2,820✔
104
  const { textNodeName } = options;
2,820✔
105
  const propCount = Object.keys(obj).length;
2,820✔
106
  
2,820✔
107
  if (propCount === 0) {
2,820✔
108
    return true;
585✔
109
  }
585✔
110

2,235✔
111
  if (
2,235✔
112
    propCount === 1 &&
2,820✔
113
    (obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
1,710✔
114
  ) {
2,820✔
115
    return true;
1,400✔
116
  }
1,400✔
117

835✔
118
  return false;
835✔
119
}
2,820✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc