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

NaturalIntelligence / fast-xml-parser / 13817442089

12 Mar 2025 05:13PM UTC coverage: 98.946%. Remained the same
13817442089

Pull #734

github

web-flow
Merge 4a6d4de32 into af4f1d2d4
Pull Request #734: [WIP] ci: build with Node 18.x

1087 of 1120 branches covered (97.05%)

8640 of 8732 relevant lines covered (98.95%)

510597.48 hits per line

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

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

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

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

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

2,750✔
40
      if(tagObj[":@"]){
2,750✔
41
        assignAttributes( val, tagObj[":@"], newJpath, options);
565✔
42
      }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
2,750✔
43
        val = val[options.textNodeName];
1,180✔
44
      }else if(Object.keys(val).length === 0){
2,185✔
45
        if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
335✔
46
        else val = "";
320✔
47
      }
335✔
48

2,750✔
49
      if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
2,750✔
50
        if(!Array.isArray(compressedObj[property])) {
305✔
51
            compressedObj[property] = [ compressedObj[property] ];
230✔
52
        }
230✔
53
        compressedObj[property].push(val);
305✔
54
      }else{
2,750✔
55
        //TODO: if a node is not an array, then check if it should be an array
2,445✔
56
        //also determine if it is a leaf node
2,445✔
57
        if (options.isArray(property, newJpath, isLeaf )) {
2,445✔
58
          compressedObj[property] = [val];
95✔
59
        }else{
2,445✔
60
          compressedObj[property] = val;
2,350✔
61
        }
2,350✔
62
      }
2,445✔
63
    }
2,750✔
64
    
4,515✔
65
  }
4,515✔
66
  // if(text && text.length > 0) compressedObj[options.textNodeName] = text;
3,405✔
67
  if(typeof text === "string"){
3,405✔
68
    if(text.length > 0) compressedObj[options.textNodeName] = text;
1,180✔
69
  }else if(text !== undefined) compressedObj[options.textNodeName] = text;
3,405✔
70
  return compressedObj;
3,405✔
71
}
3,405✔
72

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

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

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

2,205✔
104
  if (
2,205✔
105
    propCount === 1 &&
2,750✔
106
    (obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
1,695✔
107
  ) {
2,750✔
108
    return true;
1,385✔
109
  }
1,385✔
110

820✔
111
  return false;
820✔
112
}
2,750✔
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