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

NaturalIntelligence / fast-xml-parser / 12317922166

13 Dec 2024 02:55PM UTC coverage: 98.212%. Remained the same
12317922166

Pull #699

github

web-flow
Merge ae43748a5 into 408290231
Pull Request #699: Fixes entity parsing when used in strict mode

804 of 863 branches covered (93.16%)

2746 of 2796 relevant lines covered (98.21%)

682647.78 hits per line

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

98.15
/src/xmlparser/node2json.js
1
'use strict';
2

3
/**
4
 * 
5
 * @param {array} node 
6
 * @param {any} options 
7
 * @returns 
8
 */
9
function prettify(node, options){
10
  return compress( node, options);
524✔
11
}
12

13
/**
14
 * 
15
 * @param {array} arr 
16
 * @param {object} options 
17
 * @param {string} jPath 
18
 * @returns object
19
 */
20
function compress(arr, options, jPath){
21
  let text;
22
  const compressedObj = {};
2,724✔
23
  for (let i = 0; i < arr.length; i++) {
2,724✔
24
    const tagObj = arr[i];
3,612✔
25
    const property = propName(tagObj);
3,612✔
26
    let newJpath = "";
3,612✔
27
    if(jPath === undefined) newJpath = property;
3,612✔
28
    else newJpath = jPath + "." + property;
3,000✔
29

30
    if(property === options.textNodeName){
3,612✔
31
      if(text === undefined) text = tagObj[property];
1,412✔
32
      else text += "" + tagObj[property];
128✔
33
    }else if(property === undefined){
2,200!
34
      continue;
×
35
    }else if(tagObj[property]){
2,200!
36
      
37
      let val = compress(tagObj[property], options, newJpath);
2,200✔
38
      const isLeaf = isLeafTag(val, options);
2,200✔
39

40
      if(tagObj[":@"]){
2,200✔
41
        assignAttributes( val, tagObj[":@"], newJpath, options);
452✔
42
      }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
1,748✔
43
        val = val[options.textNodeName];
944✔
44
      }else if(Object.keys(val).length === 0){
804✔
45
        if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
268✔
46
        else val = "";
256✔
47
      }
48

49
      if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
2,200✔
50
        if(!Array.isArray(compressedObj[property])) {
244✔
51
            compressedObj[property] = [ compressedObj[property] ];
184✔
52
        }
53
        compressedObj[property].push(val);
244✔
54
      }else{
55
        //TODO: if a node is not an array, then check if it should be an array
56
        //also determine if it is a leaf node
57
        if (options.isArray(property, newJpath, isLeaf )) {
1,956✔
58
          compressedObj[property] = [val];
76✔
59
        }else{
60
          compressedObj[property] = val;
1,880✔
61
        }
62
      }
63
    }
64
    
65
  }
66
  // if(text && text.length > 0) compressedObj[options.textNodeName] = text;
67
  if(typeof text === "string"){
2,724✔
68
    if(text.length > 0) compressedObj[options.textNodeName] = text;
944✔
69
  }else if(text !== undefined) compressedObj[options.textNodeName] = text;
1,780✔
70
  return compressedObj;
2,724✔
71
}
72

73
function propName(obj){
74
  const keys = Object.keys(obj);
3,612✔
75
  for (let i = 0; i < keys.length; i++) {
3,612✔
76
    const key = keys[i];
3,612✔
77
    if(key !== ":@") return key;
3,612!
78
  }
79
}
80

81
function assignAttributes(obj, attrMap, jpath, options){
82
  if (attrMap) {
452!
83
    const keys = Object.keys(attrMap);
452✔
84
    const len = keys.length; //don't make it inline
452✔
85
    for (let i = 0; i < len; i++) {
452✔
86
      const atrrName = keys[i];
680✔
87
      if (options.isArray(atrrName, jpath + "." + atrrName, true, true)) {
680✔
88
        obj[atrrName] = [ attrMap[atrrName] ];
16✔
89
      } else {
90
        obj[atrrName] = attrMap[atrrName];
664✔
91
      }
92
    }
93
  }
94
}
95

96
function isLeafTag(obj, options){
97
  const { textNodeName } = options;
2,200✔
98
  const propCount = Object.keys(obj).length;
2,200✔
99
  
100
  if (propCount === 0) {
2,200✔
101
    return true;
436✔
102
  }
103

104
  if (
1,764✔
105
    propCount === 1 &&
3,660✔
106
    (obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
107
  ) {
108
    return true;
1,108✔
109
  }
110

111
  return false;
656✔
112
}
113
exports.prettify = prettify;
4✔
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