fast-xml-parser 3.19.0 → 3.20.0

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.
package/README.md CHANGED
@@ -138,6 +138,11 @@ var options = {
138
138
  cdataTagName: "__cdata", //default is 'false'
139
139
  cdataPositionChar: "\\c",
140
140
  parseTrueNumberOnly: false,
141
+ numParseOptions:{
142
+ hex: true,
143
+ leadingZeros: true,
144
+ //skipLike: /\+[0-9]{10}/
145
+ }
141
146
  arrayMode: false, //"strict"
142
147
  attrValueProcessor: (val, attrName) => he.decode(val, {isAttributeValue: true}),//default is a=>a
143
148
  tagValueProcessor : (val, tagName) => he.decode(val), //default is a=>a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-xml-parser",
3
- "version": "3.19.0",
3
+ "version": "3.20.0",
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": {
@@ -89,5 +89,8 @@
89
89
  "funding": {
90
90
  "type": "paypal",
91
91
  "url": "https://paypal.me/naturalintelligence"
92
+ },
93
+ "dependencies": {
94
+ "strnum": "^1.0.3"
92
95
  }
93
96
  }
package/src/parser.d.ts CHANGED
@@ -12,10 +12,16 @@ type X2jOptions = {
12
12
  cdataTagName: false | string;
13
13
  cdataPositionChar: string;
14
14
  parseTrueNumberOnly: boolean;
15
+ numParseOptions: strnumOptions;
15
16
  tagValueProcessor: (tagValue: string, tagName: string) => string;
16
17
  attrValueProcessor: (attrValue: string, attrName: string) => string;
17
18
  stopNodes: string[];
18
19
  };
20
+ type strnumOptions = {
21
+ hex: boolean;
22
+ leadingZeros: boolean,
23
+ skipLike: RegExp
24
+ }
19
25
  type X2jOptionsOptional = Partial<X2jOptions>;
20
26
  type validationOptions = {
21
27
  allowBooleanAttributes: boolean;
package/src/parser.js CHANGED
@@ -6,7 +6,7 @@ const x2xmlnode = require('./xmlstr2xmlnode');
6
6
  const buildOptions = require('./util').buildOptions;
7
7
  const validator = require('./validator');
8
8
 
9
- exports.parse = function(xmlData, options, validationOption) {
9
+ exports.parse = function(xmlData, givenOptions = {}, validationOption) {
10
10
  if( validationOption){
11
11
  if(validationOption === true) validationOption = {}
12
12
 
@@ -15,7 +15,16 @@ exports.parse = function(xmlData, options, validationOption) {
15
15
  throw Error( result.err.msg)
16
16
  }
17
17
  }
18
- options = buildOptions(options, x2xmlnode.defaultOptions, x2xmlnode.props);
18
+ if(givenOptions.parseTrueNumberOnly
19
+ && givenOptions.parseNodeValue !== false
20
+ && !givenOptions.numParseOptions){
21
+
22
+ givenOptions.numParseOptions = {
23
+ leadingZeros: false,
24
+ }
25
+ }
26
+ let options = buildOptions(givenOptions, x2xmlnode.defaultOptions, x2xmlnode.props);
27
+
19
28
  const traversableObj = xmlToNodeobj.getTraversalObj(xmlData, options)
20
29
  //print(traversableObj, " ");
21
30
  return nodeToJson.convertToJson(traversableObj, options);
@@ -3,6 +3,8 @@
3
3
  const util = require('./util');
4
4
  const buildOptions = require('./util').buildOptions;
5
5
  const xmlNode = require('./xmlNode');
6
+ const toNumber = require("strnum");
7
+
6
8
  const regx =
7
9
  '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
8
10
  .replace(/NAME/g, util.nameRegexp);
@@ -32,6 +34,10 @@ const defaultOptions = {
32
34
  trimValues: true, //Trim string values of tag and attributes
33
35
  cdataTagName: false,
34
36
  cdataPositionChar: '\\c',
37
+ numParseOptions: {
38
+ hex: true,
39
+ leadingZeros: true
40
+ },
35
41
  tagValueProcessor: function(a, tagName) {
36
42
  return a;
37
43
  },
@@ -60,6 +66,7 @@ const props = [
60
66
  'tagValueProcessor',
61
67
  'attrValueProcessor',
62
68
  'parseTrueNumberOnly',
69
+ 'numParseOptions',
63
70
  'stopNodes'
64
71
  ];
65
72
  exports.props = props;
@@ -76,7 +83,7 @@ function processTagValue(tagName, val, options) {
76
83
  val = val.trim();
77
84
  }
78
85
  val = options.tagValueProcessor(val, tagName);
79
- val = parseValue(val, options.parseNodeValue, options.parseTrueNumberOnly);
86
+ val = parseValue(val, options.parseNodeValue, options.numParseOptions);
80
87
  }
81
88
 
82
89
  return val;
@@ -96,26 +103,13 @@ function resolveNameSpace(tagname, options) {
96
103
  return tagname;
97
104
  }
98
105
 
99
- function parseValue(val, shouldParse, parseTrueNumberOnly) {
106
+ function parseValue(val, shouldParse, options) {
100
107
  if (shouldParse && typeof val === 'string') {
101
- let parsed;
102
- if (val.trim() === '' || isNaN(val)) {
103
- parsed = val === 'true' ? true : val === 'false' ? false : val;
104
- } else {
105
- if (val.indexOf('0x') !== -1) {
106
- //support hexa decimal
107
- parsed = Number.parseInt(val, 16);
108
- } else if (val.indexOf('.') !== -1) {
109
- parsed = Number.parseFloat(val);
110
- val = val.replace(/\.?0+$/, "");
111
- } else {
112
- parsed = Number.parseInt(val, 10);
113
- }
114
- if (parseTrueNumberOnly) {
115
- parsed = String(parsed) === val ? parsed : val;
116
- }
117
- }
118
- return parsed;
108
+ //console.log(options)
109
+ const newval = val.trim();
110
+ if(newval === 'true' ) return true;
111
+ else if(newval === 'false' ) return false;
112
+ else return toNumber(val, options);
119
113
  } else {
120
114
  if (util.isExist(val)) {
121
115
  return val;
@@ -148,7 +142,7 @@ function buildAttributesMap(attrStr, options) {
148
142
  attrs[options.attributeNamePrefix + attrName] = parseValue(
149
143
  matches[i][4],
150
144
  options.parseAttributeValue,
151
- options.parseTrueNumberOnly
145
+ options.numParseOptions
152
146
  );
153
147
  } else if (options.allowBooleanAttributes) {
154
148
  attrs[options.attributeNamePrefix + attrName] = true;