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

NaturalIntelligence / fast-xml-parser / 4156981534

pending completion
4156981534

push

github

amit kumar gupta
Update package detail

692 of 741 branches covered (93.39%)

3 of 3 new or added lines in 2 files covered. (100.0%)

2376 of 2418 relevant lines covered (98.26%)

196233.44 hits per line

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

94.79
/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);
110✔
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 = {};
585✔
23
  for (let i = 0; i < arr.length; i++) {
585✔
24
    const tagObj = arr[i];
804✔
25
    const property = propName(tagObj);
804✔
26
    let newJpath = "";
804✔
27
    if(jPath === undefined) newJpath = property;
804✔
28
    else newJpath = jPath + "." + property;
675✔
29

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

40
      if(tagObj[":@"]){
475✔
41
        assignAttributes( val, tagObj[":@"], newJpath, options);
95✔
42
      }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
380✔
43
        val = val[options.textNodeName];
223✔
44
      }else if(Object.keys(val).length === 0){
157✔
45
        if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
41✔
46
        else val = "";
38✔
47
      }
48

49
      if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
475✔
50
        if(!Array.isArray(compressedObj[property])) {
48✔
51
            compressedObj[property] = [ compressedObj[property] ];
38✔
52
        }
53
        compressedObj[property].push(val);
48✔
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 )) {
427✔
58
          compressedObj[property] = [val];
18✔
59
        }else{
60
          compressedObj[property] = val;
409✔
61
        }
62
      }
63
    }
64
    
65
  }
66
  // if(text && text.length > 0) compressedObj[options.textNodeName] = text;
67
  if(typeof text === "string"){
585✔
68
    if(text.length > 0) compressedObj[options.textNodeName] = text;
215✔
69
  }else if(text !== undefined) compressedObj[options.textNodeName] = text;
370✔
70
  return compressedObj;
585✔
71
}
72

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

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

96
function isLeafTag(obj, options){
97
  const propCount = Object.keys(obj).length;
475✔
98
  if( propCount === 0 || (propCount === 1 && obj[options.textNodeName]) ) return true;
475✔
99
  return false;
149✔
100
}
101
exports.prettify = prettify;
1✔
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