fast-xml-parser 3.21.0 → 4.0.0-beta.3

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.
@@ -0,0 +1,486 @@
1
+ 'use strict';
2
+
3
+ const util = require('../util');
4
+ const xmlNode = require('./xmlNode');
5
+ const readDocType = require("./DocTypeReader");
6
+ const toNumber = require("strnum");
7
+
8
+ const regx =
9
+ '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
10
+ .replace(/NAME/g, util.nameRegexp);
11
+
12
+ //const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
13
+ //const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
14
+
15
+ class OrderedObjParser{
16
+ constructor(options){
17
+ this.options = options;
18
+ this.currentNode = null;
19
+ this.tagsNodeStack = [];
20
+ this.docTypeEntities = {};
21
+ this.lastEntities = {
22
+ "amp" : { regex: /&(amp|#38|#x26);/g, val : "&"},
23
+ "apos" : { regex: /&(apos|#39|#x27);/g, val : "'"},
24
+ "gt" : { regex: /&(gt|#62|#x3E);/g, val : ">"},
25
+ "lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
26
+ "quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
27
+ };
28
+ this.htmlEntities = {
29
+ "space": { regex: /&(nbsp|#160);/g, val: " " },
30
+ // "lt" : { regex: /&(lt|#60);/g, val: "<" },
31
+ // "gt" : { regex: /&(gt|#62);/g, val: ">" },
32
+ // "amp" : { regex: /&(amp|#38);/g, val: "&" },
33
+ // "quot" : { regex: /&(quot|#34);/g, val: "\"" },
34
+ // "apos" : { regex: /&(apos|#39);/g, val: "'" },
35
+ "cent" : { regex: /&(cent|#162);/g, val: "¢" },
36
+ "pound" : { regex: /&(pound|#163);/g, val: "£" },
37
+ "yen" : { regex: /&(yen|#165);/g, val: "¥" },
38
+ "euro" : { regex: /&(euro|#8364);/g, val: "€" },
39
+ "copyright" : { regex: /&(copy|#169);/g, val: "©" },
40
+ "reg" : { regex: /&(reg|#174);/g, val: "®" },
41
+ "inr" : { regex: /&(inr|#8377);/g, val: "₹" },
42
+ };
43
+ this.parseXml = parseXml;
44
+ this.parseTextData = parseTextData;
45
+ this.resolveNameSpace = resolveNameSpace;
46
+ this.buildAttributesMap = buildAttributesMap;
47
+ this.isItStopNode = isItStopNode;
48
+ this.replaceEntitiesValue = replaceEntitiesValue;
49
+ this.readTagExp = readTagExp;
50
+ this.readStopNodeData = readStopNodeData;
51
+ }
52
+
53
+ }
54
+
55
+ /**
56
+ * @param {string} val
57
+ * @param {string} tagName
58
+ * @param {string} jPath
59
+ * @param {boolean} dontTrim
60
+ * @param {boolean} hasAttributes
61
+ * @param {boolean} isLeafNode
62
+ */
63
+ function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode) {
64
+ if (val !== undefined) {
65
+ if (this.options.trimValues && !dontTrim) {
66
+ val = val.trim();
67
+ }
68
+ if(val.length > 0){
69
+ val = this.replaceEntitiesValue(val);
70
+
71
+ const newval = this.options.tagValueProcessor(tagName, val, jPath, hasAttributes, isLeafNode);
72
+ if(newval === null || newval === undefined){
73
+ //don't parse
74
+ return val;
75
+ }else if(typeof newval !== typeof val || newval !== val){
76
+ //overwrite
77
+ return newval;
78
+ }else if(this.options.trimValues){
79
+ return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
80
+ }else{
81
+ const trimmedVal = val.trim();
82
+ if(trimmedVal === val){
83
+ return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
84
+ }else{
85
+ return val;
86
+ }
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ function resolveNameSpace(tagname) {
93
+ if (this.options.removeNSPrefix) {
94
+ const tags = tagname.split(':');
95
+ const prefix = tagname.charAt(0) === '/' ? '/' : '';
96
+ if (tags[0] === 'xmlns') {
97
+ return '';
98
+ }
99
+ if (tags.length === 2) {
100
+ tagname = prefix + tags[1];
101
+ }
102
+ }
103
+ return tagname;
104
+ }
105
+
106
+ //TODO: change regex to capture NS
107
+ //const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
108
+ const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
109
+
110
+ function buildAttributesMap(attrStr, jPath) {
111
+ if (!this.options.ignoreAttributes && typeof attrStr === 'string') {
112
+ // attrStr = attrStr.replace(/\r?\n/g, ' ');
113
+ //attrStr = attrStr || attrStr.trim();
114
+
115
+ const matches = util.getAllMatches(attrStr, attrsRegx);
116
+ const len = matches.length; //don't make it inline
117
+ const attrs = {};
118
+ for (let i = 0; i < len; i++) {
119
+ const attrName = this.resolveNameSpace(matches[i][1]);
120
+ let oldVal = matches[i][4];
121
+ const aName = this.options.attributeNamePrefix + attrName;
122
+ if (attrName.length) {
123
+ if (oldVal !== undefined) {
124
+ if (this.options.trimValues) {
125
+ oldVal = oldVal.trim();
126
+ }
127
+ oldVal = this.replaceEntitiesValue(oldVal);
128
+ const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPath);
129
+ if(newVal === null || newVal === undefined){
130
+ //don't parse
131
+ attrs[aName] = oldVal;
132
+ }else if(typeof newVal !== typeof oldVal || newVal !== oldVal){
133
+ //overwrite
134
+ attrs[aName] = newVal;
135
+ }else{
136
+ //parse
137
+ attrs[aName] = parseValue(
138
+ oldVal,
139
+ this.options.parseAttributeValue,
140
+ this.options.numberParseOptions
141
+ );
142
+ }
143
+ } else if (this.options.allowBooleanAttributes) {
144
+ attrs[aName] = true;
145
+ }
146
+ }
147
+ }
148
+ if (!Object.keys(attrs).length) {
149
+ return;
150
+ }
151
+ if (this.options.attributesGroupName) {
152
+ const attrCollection = {};
153
+ attrCollection[this.options.attributesGroupName] = attrs;
154
+ return attrCollection;
155
+ }
156
+ return attrs;
157
+ }
158
+ }
159
+
160
+ const parseXml = function(xmlData) {
161
+ xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
162
+ const xmlObj = new xmlNode('!xml');
163
+ let currentNode = xmlObj;
164
+ let textData = "";
165
+ let jPath = "";
166
+ for(let i=0; i< xmlData.length; i++){//for each char in XML data
167
+ const ch = xmlData[i];
168
+ if(ch === '<'){
169
+ // const nextIndex = i+1;
170
+ // const _2ndChar = xmlData[nextIndex];
171
+ if( xmlData[i+1] === '/') {//Closing Tag
172
+ const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
173
+ let tagName = xmlData.substring(i+2,closeIndex).trim();
174
+
175
+ if(this.options.removeNSPrefix){
176
+ const colonIndex = tagName.indexOf(":");
177
+ if(colonIndex !== -1){
178
+ tagName = tagName.substr(colonIndex+1);
179
+ }
180
+ }
181
+
182
+ if(currentNode){
183
+ textData = this.parseTextData(textData
184
+ , currentNode.tagname
185
+ , jPath
186
+ ,false
187
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
188
+ , Object.keys(currentNode.child).length === 0);
189
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
190
+ textData = "";
191
+ }
192
+
193
+ jPath = jPath.substr(0, jPath.lastIndexOf("."));
194
+
195
+ currentNode = this.tagsNodeStack.pop();//avoid recurssion, set the parent tag scope
196
+ textData = "";
197
+ i = closeIndex;
198
+ } else if( xmlData[i+1] === '?') {
199
+ i = findClosingIndex(xmlData, "?>", i, "Pi Tag is not closed.")
200
+ } else if(xmlData.substr(i + 1, 3) === '!--') {
201
+ const endIndex = findClosingIndex(xmlData, "-->", i, "Comment is not closed.")
202
+ if(this.options.commentPropName){
203
+ const comment = xmlData.substring(i + 4, endIndex - 2);
204
+
205
+ //TODO: remove repeated code
206
+ if(textData){ //store previously collected data as textNode
207
+ textData = this.parseTextData(textData
208
+ , currentNode.tagname
209
+ , jPath
210
+ ,false
211
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
212
+ , Object.keys(currentNode.child).length === 0);
213
+
214
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
215
+ textData = "";
216
+ }
217
+ currentNode.add(this.options.commentPropName, [ { [this.options.textNodeName] : comment } ]);
218
+ }
219
+ i = endIndex;
220
+ } else if( xmlData.substr(i + 1, 2) === '!D') {
221
+ const result = readDocType(xmlData, i);
222
+ this.docTypeEntities = result.entities;
223
+ i = result.i;
224
+ }else if(xmlData.substr(i + 1, 2) === '![') {
225
+ const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
226
+ const tagExp = xmlData.substring(i + 9,closeIndex);
227
+
228
+ if(textData){ //store previously collected data as textNode
229
+ textData = this.parseTextData(textData
230
+ , currentNode.tagname
231
+ , jPath
232
+ ,false
233
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
234
+ , Object.keys(currentNode.child).length === 0);
235
+
236
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
237
+ textData = "";
238
+ }
239
+
240
+ //cdata should be set even if it is 0 length string
241
+ if(this.options.cdataPropName){
242
+ // let val = this.parseTextData(tagExp, this.options.cdataPropName, jPath + "." + this.options.cdataPropName, true, false, true);
243
+ // if(!val) val = "";
244
+ currentNode.add(this.options.cdataPropName, [ { [this.options.textNodeName] : tagExp } ]);
245
+ }else{
246
+ let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true);
247
+ if(!val) val = "";
248
+ currentNode.add(this.options.textNodeName, val);
249
+ }
250
+
251
+ i = closeIndex + 2;
252
+ }else {//Opening tag
253
+
254
+ let result = this.readTagExp(xmlData,i);
255
+ let tagName= result.tagName;
256
+ let tagExp = result.tagExp;
257
+ let attrExpPresent = result.attrExpPresent;
258
+ let closeIndex = result.closeIndex;
259
+
260
+ //save text as child node
261
+ if (currentNode && textData) {
262
+ if(currentNode.tagname !== '!xml'){
263
+ //when nested tag is found
264
+ textData = this.parseTextData(textData
265
+ , currentNode.tagname
266
+ , jPath
267
+ , false
268
+ , currentNode.attributes ? Object.keys(currentNode.attributes).length !== 0 : false
269
+ , false);
270
+ if(textData !== undefined && textData !== "") currentNode.add(this.options.textNodeName, textData);
271
+ textData = "";
272
+ }
273
+ }
274
+
275
+ if(tagName !== xmlObj.tagname){
276
+ jPath += jPath ? "." + tagName : tagName;
277
+ }
278
+
279
+ //check if last tag was unpaired tag
280
+ const lastTag = currentNode;
281
+ if(lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1 ){
282
+ currentNode = this.tagsNodeStack.pop();
283
+ }
284
+
285
+ if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
286
+ let tagContent = "";
287
+ //self-closing tag
288
+ if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){}
289
+ //boolean tag
290
+ else if(this.options.unpairedTags.indexOf(tagName) !== -1){}
291
+ //normal tag
292
+ else{
293
+ //read until closing tag is found
294
+ const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1);
295
+ if(!result) throw new Error(`Unexpected end of ${tagName}`);
296
+ i = result.i;
297
+ tagContent = result.tagContent;
298
+ }
299
+
300
+ const childNode = new xmlNode(tagName);
301
+ if(tagName !== tagExp && attrExpPresent){
302
+ childNode.attributes = this.buildAttributesMap(tagExp, jPath);
303
+ }
304
+ jPath = jPath.substr(0, jPath.lastIndexOf("."));
305
+ childNode.add(this.options.textNodeName, tagContent);
306
+
307
+ currentNode.addChild(childNode);
308
+ }else{
309
+ //selfClosing tag
310
+ if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
311
+
312
+ if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
313
+ tagName = tagName.substr(0, tagName.length - 1);
314
+ tagExp = tagName;
315
+ }else{
316
+ tagExp = tagExp.substr(0, tagExp.length - 1);
317
+ }
318
+
319
+ const childNode = new xmlNode(tagName);
320
+ if(tagName !== tagExp && attrExpPresent){
321
+ childNode.attributes = this.buildAttributesMap(tagExp, jPath);
322
+ }
323
+ jPath = jPath.substr(0, jPath.lastIndexOf("."));
324
+ currentNode.addChild(childNode);
325
+ }
326
+ //opening tag
327
+ else{
328
+ const childNode = new xmlNode( tagName);
329
+ this.tagsNodeStack.push(currentNode);
330
+
331
+ if(tagName !== tagExp && attrExpPresent){
332
+ childNode.attributes = this.buildAttributesMap(tagExp, jPath);
333
+ }
334
+ currentNode.addChild(childNode);
335
+ currentNode = childNode;
336
+ }
337
+ textData = "";
338
+ i = closeIndex;
339
+ }
340
+ }
341
+ }else{
342
+ textData += xmlData[i];
343
+ }
344
+ }
345
+ return xmlObj.child;
346
+ }
347
+
348
+ const replaceEntitiesValue = function(val){
349
+ if(this.options.processEntities){
350
+ for(let entityName in this.docTypeEntities){
351
+ const entity = this.docTypeEntities[entityName];
352
+ val = val.replace( entity.regx, entity.val);
353
+ }
354
+ for(let entityName in this.lastEntities){
355
+ const entity = this.lastEntities[entityName];
356
+ val = val.replace( entity.regex, entity.val);
357
+ }
358
+ if(this.options.htmlEntities){
359
+ for(let entityName in this.htmlEntities){
360
+ const entity = this.htmlEntities[entityName];
361
+ val = val.replace( entity.regex, entity.val);
362
+ }
363
+ }
364
+ }
365
+ return val;
366
+ }
367
+ //TODO: use jPath to simplify the logic
368
+ /**
369
+ *
370
+ * @param {string[]} stopNodes
371
+ * @param {string} jPath
372
+ * @param {string} currentTagName
373
+ */
374
+ function isItStopNode(stopNodes, jPath, currentTagName){
375
+ const allNodesExp = "*." + currentTagName;
376
+ for (const stopNodePath in stopNodes) {
377
+ const stopNodeExp = stopNodes[stopNodePath];
378
+ if( allNodesExp === stopNodeExp || jPath === stopNodeExp ) return true;
379
+ }
380
+ return false;
381
+ }
382
+
383
+ /**
384
+ * Returns the tag Expression and where it is ending handling single-dobule quotes situation
385
+ * @param {string} xmlData
386
+ * @param {number} i starting index
387
+ * @returns
388
+ */
389
+ function tagExpWithClosingIndex(xmlData, i){
390
+ let attrBoundary;
391
+ let tagExp = "";
392
+ for (let index = i; index < xmlData.length; index++) {
393
+ let ch = xmlData[index];
394
+ if (attrBoundary) {
395
+ if (ch === attrBoundary) attrBoundary = "";//reset
396
+ } else if (ch === '"' || ch === "'") {
397
+ attrBoundary = ch;
398
+ } else if (ch === '>') {
399
+ return {
400
+ data: tagExp,
401
+ index: index
402
+ }
403
+ } else if (ch === '\t') {
404
+ ch = " "
405
+ }
406
+ tagExp += ch;
407
+ }
408
+ }
409
+
410
+ function findClosingIndex(xmlData, str, i, errMsg){
411
+ const closingIndex = xmlData.indexOf(str, i);
412
+ if(closingIndex === -1){
413
+ throw new Error(errMsg)
414
+ }else{
415
+ return closingIndex + str.length - 1;
416
+ }
417
+ }
418
+
419
+ function readTagExp(xmlData,i){
420
+ const result = tagExpWithClosingIndex(xmlData, i+1);
421
+ let tagExp = result.data;
422
+ const closeIndex = result.index;
423
+ const separatorIndex = tagExp.search(/\s/);
424
+ let tagName = tagExp;
425
+ let attrExpPresent = true;
426
+ if(separatorIndex !== -1){//separate tag name and attributes expression
427
+ tagName = tagExp.substr(0, separatorIndex).replace(/\s\s*$/, '');
428
+ tagExp = tagExp.substr(separatorIndex + 1);
429
+ }
430
+
431
+ if(this. options.removeNSPrefix){
432
+ const colonIndex = tagName.indexOf(":");
433
+ if(colonIndex !== -1){
434
+ tagName = tagName.substr(colonIndex+1);
435
+ attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
436
+ }
437
+ }
438
+
439
+ return {
440
+ tagName: tagName,
441
+ tagExp: tagExp,
442
+ closeIndex: closeIndex,
443
+ attrExpPresent: attrExpPresent,
444
+ }
445
+ }
446
+ /**
447
+ * find paired tag for a stop node
448
+ * @param {string} xmlData
449
+ * @param {string} tagName
450
+ * @param {number} i
451
+ */
452
+ function readStopNodeData(xmlData, tagName, i){
453
+ const startIndex = i;
454
+ for (; i < xmlData.length; i++) {
455
+ if( xmlData[i] === "<" && xmlData[i+1] === "/"){
456
+ const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
457
+ let closeTagName = xmlData.substring(i+2,closeIndex).trim();
458
+ if(closeTagName === tagName){
459
+ return {
460
+ tagContent: xmlData.substring(startIndex, i),
461
+ i : closeIndex
462
+ }
463
+ }
464
+ i=closeIndex;
465
+ }
466
+ }//end for loop
467
+ }
468
+
469
+ function parseValue(val, shouldParse, options) {
470
+ if (shouldParse && typeof val === 'string') {
471
+ //console.log(options)
472
+ const newval = val.trim();
473
+ if(newval === 'true' ) return true;
474
+ else if(newval === 'false' ) return false;
475
+ else return toNumber(val, options);
476
+ } else {
477
+ if (util.isExist(val)) {
478
+ return val;
479
+ } else {
480
+ return '';
481
+ }
482
+ }
483
+ }
484
+
485
+
486
+ module.exports = OrderedObjParser;
@@ -0,0 +1,37 @@
1
+ const { buildOptions} = require("./OptionsBuilder");
2
+ const OrderedObjParser = require("./OrderedObjParser");
3
+ const { prettify} = require("./node2json");
4
+ const validator = require('../validator');
5
+
6
+ class XMLParser{
7
+ constructor(options){
8
+ this.options = buildOptions(options);
9
+ }
10
+ /**
11
+ * Parse XML dats to JS object
12
+ * @param {string|Buffer} xmlData
13
+ * @param {boolean|Object} validationOption
14
+ */
15
+ parse(xmlData,validationOption){
16
+ if(typeof xmlData === "string"){
17
+ }else if( xmlData.toString){
18
+ xmlData = xmlData.toString();
19
+ }else{
20
+ throw new Error("XML data is accepted in String or Bytes[] form.")
21
+ }
22
+ if( validationOption){
23
+ if(validationOption === true) validationOption = {}; //validate with default options
24
+
25
+ const result = validator.validate(xmlData, validationOption);
26
+ if (result !== true) {
27
+ throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` )
28
+ }
29
+ }
30
+ const orderedObjParser = new OrderedObjParser(this.options);
31
+ const orderedResult = orderedObjParser.parseXml(xmlData);
32
+ if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
33
+ else return prettify(orderedResult, this.options);
34
+ }
35
+ }
36
+
37
+ module.exports = XMLParser;
@@ -0,0 +1,101 @@
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);
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 = {};
23
+ for (let i = 0; i < arr.length; i++) {
24
+ const tagObj = arr[i];
25
+ const property = propName(tagObj);
26
+ let newJpath = "";
27
+ if(jPath === undefined) newJpath = property;
28
+ else newJpath = jPath + "." + property;
29
+
30
+ if(property === options.textNodeName){
31
+ if(text === undefined) text = tagObj[property];
32
+ else text += "" + tagObj[property];
33
+ }else if(property === undefined){
34
+ continue;
35
+ }else if(tagObj[property]){
36
+
37
+ let val = compress(tagObj[property], options, newJpath);
38
+ const isLeaf = isLeafTag(val, options);
39
+
40
+ if(tagObj.attributes){
41
+ assignAttributes( val, tagObj.attributes, newJpath, options);
42
+ }else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
43
+ val = val[options.textNodeName];
44
+ }else if(Object.keys(val).length === 0){
45
+ if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
46
+ else val = "";
47
+ }
48
+
49
+ if(compressedObj[property] !== undefined) {
50
+ if(!Array.isArray(compressedObj[property])) {
51
+ compressedObj[property] = [ compressedObj[property] ];
52
+ }
53
+ compressedObj[property].push(val);
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 )) {
58
+ compressedObj[property] = [val];
59
+ }else{
60
+ compressedObj[property] = val;
61
+ }
62
+ }
63
+ }
64
+
65
+ }
66
+ // if(text && text.length > 0) compressedObj[options.textNodeName] = text;
67
+ if(typeof text === "string"){
68
+ if(text.length > 0) compressedObj[options.textNodeName] = text;
69
+ }else if(text !== undefined) compressedObj[options.textNodeName] = text;
70
+ return compressedObj;
71
+ }
72
+
73
+ function propName(obj){
74
+ const keys = Object.keys(obj);
75
+ for (let i = 0; i < keys.length; i++) {
76
+ const key = keys[i];
77
+ if(key !== "attributes") return key;
78
+ }
79
+ }
80
+
81
+ function assignAttributes(obj, attrMap, jpath, options){
82
+ if (attrMap) {
83
+ const keys = Object.keys(attrMap);
84
+ const len = keys.length; //don't make it inline
85
+ for (let i = 0; i < len; i++) {
86
+ const atrrName = keys[i];
87
+ if (options.isArray(atrrName, jpath + "." + atrrName, true, true)) {
88
+ obj[atrrName] = [ attrMap[atrrName] ];
89
+ } else {
90
+ obj[atrrName] = attrMap[atrrName];
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ function isLeafTag(obj, options){
97
+ const propCount = Object.keys(obj).length;
98
+ if( propCount === 0 || (propCount === 1 && obj[options.textNodeName]) ) return true;
99
+ return false;
100
+ }
101
+ exports.prettify = prettify;
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ class XmlNode{
4
+ constructor(tagname) {
5
+ this.tagname = tagname;
6
+ this.child = []; //nested tags, text, cdata, comments in order
7
+ this.attributes = {}; //attributes map
8
+ }
9
+ add(key,val){
10
+ // this.child.push( {name : key, val: val, isCdata: isCdata });
11
+ this.child.push( {[key]: val });
12
+ }
13
+ addChild(node) {
14
+ if(node.attributes && Object.keys(node.attributes).length > 0){
15
+ this.child.push( { [node.tagname]: node.child, attributes: node.attributes });
16
+ }else{
17
+ this.child.push( { [node.tagname]: node.child });
18
+ }
19
+ };
20
+ };
21
+
22
+
23
+ module.exports = XmlNode;