fast-xml-parser 3.21.0 → 3.21.1

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/json2xml.js +15 -9
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "fast-xml-parser",
3
- "version": "3.21.0",
3
+ "version": "3.21.1",
4
4
  "description": "Validate XML or Parse XML to JS/JSON very fast without C/C++ based libraries",
5
5
  "main": "./src/parser.js",
6
6
  "scripts": {
7
- "test": "npx nyc jasmine spec/*spec.js",
7
+ "test": "nyc --reporter=lcov --reporter=text jasmine spec/*spec.js",
8
8
  "unit": "jasmine",
9
- "coverage": "npx nyc report --reporter html --reporter text -t .nyc_output --report-dir .nyc_output/summary",
9
+ "coverage": "nyc report --reporter html --reporter text -t .nyc_output --report-dir .nyc_output/summary",
10
10
  "perf": "node ./benchmark/perfTest3.js",
11
11
  "lint": "eslint src/*.js spec/*.js",
12
12
  "bundle": "webpack && webpack --config webpack-prod.config.js",
@@ -77,6 +77,7 @@
77
77
  "he": "^1.2.0",
78
78
  "jasmine": "^3.6.4",
79
79
  "nimnjs": "^1.3.2",
80
+ "nyc": "^15.1.0",
80
81
  "prettier": "^1.19.1",
81
82
  "publish-please": "^5.5.2",
82
83
  "webpack": "^4.46.0",
@@ -88,7 +89,6 @@
88
89
  "url": "https://paypal.me/naturalintelligence"
89
90
  },
90
91
  "dependencies": {
91
- "nyc": "^15.1.0",
92
92
  "strnum": "^1.0.4"
93
93
  }
94
94
  }
package/src/json2xml.js CHANGED
@@ -55,6 +55,8 @@ function Parser(options) {
55
55
  this.replaceCDATAstr = replaceCDATAstr;
56
56
  this.replaceCDATAarr = replaceCDATAarr;
57
57
 
58
+ this.processTextOrObjNode = processTextOrObjNode
59
+
58
60
  if (this.options.format) {
59
61
  this.indentate = indentate;
60
62
  this.tagEndChar = '>\n';
@@ -91,10 +93,7 @@ Parser.prototype.parse = function(jObj) {
91
93
  Parser.prototype.j2x = function(jObj, level) {
92
94
  let attrStr = '';
93
95
  let val = '';
94
- const keys = Object.keys(jObj);
95
- const len = keys.length;
96
- for (let i = 0; i < len; i++) {
97
- const key = keys[i];
96
+ for (let key in jObj) {
98
97
  if (typeof jObj[key] === 'undefined') {
99
98
  // supress undefined node
100
99
  } else if (jObj[key] === null) {
@@ -143,8 +142,7 @@ Parser.prototype.j2x = function(jObj, level) {
143
142
  } else if (item === null) {
144
143
  val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
145
144
  } else if (typeof item === 'object') {
146
- const result = this.j2x(item, level + 1);
147
- val += this.buildObjNode(result.val, key, result.attrStr, level);
145
+ val += this.processTextOrObjNode(item, key, level)
148
146
  } else {
149
147
  val += this.buildTextNode(item, key, '', level);
150
148
  }
@@ -159,14 +157,22 @@ Parser.prototype.j2x = function(jObj, level) {
159
157
  attrStr += ' ' + Ks[j] + '="' + this.options.attrValueProcessor('' + jObj[key][Ks[j]]) + '"';
160
158
  }
161
159
  } else {
162
- const result = this.j2x(jObj[key], level + 1);
163
- val += this.buildObjNode(result.val, key, result.attrStr, level);
160
+ val += this.processTextOrObjNode(jObj[key], key, level)
164
161
  }
165
162
  }
166
163
  }
167
164
  return {attrStr: attrStr, val: val};
168
165
  };
169
166
 
167
+ function processTextOrObjNode (object, key, level) {
168
+ const result = this.j2x(object, level + 1);
169
+ if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
170
+ return this.buildTextNode(result.val, key, result.attrStr, level);
171
+ } else {
172
+ return this.buildObjNode(result.val, key, result.attrStr, level);
173
+ }
174
+ }
175
+
170
176
  function replaceCDATAstr(str, cdata) {
171
177
  str = this.options.tagValueProcessor('' + str);
172
178
  if (this.options.cdataPositionChar === '' || str === '') {
@@ -189,7 +195,7 @@ function replaceCDATAarr(str, cdata) {
189
195
  }
190
196
 
191
197
  function buildObjectNode(val, key, attrStr, level) {
192
- if (attrStr && !val.includes('<')) {
198
+ if (attrStr && val.indexOf('<') === -1) {
193
199
  return (
194
200
  this.indentate(level) +
195
201
  '<' +