fast-xml-parser 4.5.2 → 5.0.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.
Files changed (42) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +9 -9
  3. package/package.json +29 -16
  4. package/src/cli/cli.js +7 -6
  5. package/src/cli/man.js +4 -1
  6. package/src/cli/read.js +30 -79
  7. package/src/fxp.js +11 -8
  8. package/src/ignoreAttributes.js +2 -4
  9. package/src/util.js +14 -18
  10. package/src/{v5 → v6}/CharsSymbol.js +1 -1
  11. package/src/{v5/valueParsers → v6}/EntitiesParser.js +1 -4
  12. package/src/{v5 → v6}/OptionsBuilder.js +3 -6
  13. package/src/{v5 → v6}/OutputBuilders/BaseOutputBuilder.js +1 -3
  14. package/src/{v5 → v6}/OutputBuilders/JsArrBuilder.js +3 -3
  15. package/src/{v5 → v6}/OutputBuilders/JsMinArrBuilder.js +3 -5
  16. package/src/{v5 → v6}/OutputBuilders/JsObjBuilder.js +3 -5
  17. package/src/{v5 → v6}/OutputBuilders/ParserOptionsBuilder.js +6 -11
  18. package/src/{v5 → v6}/TagPath.js +1 -1
  19. package/src/{v5 → v6}/TagPathMatcher.js +3 -5
  20. package/src/{v5 → v6}/XMLParser.js +3 -5
  21. package/src/{v5 → v6}/Xml2JsParser.js +8 -10
  22. package/src/{v5 → v6}/XmlPartReader.js +4 -10
  23. package/src/{v5 → v6}/XmlSpecialTagsReader.js +5 -12
  24. package/src/{v5 → v6}/inputSource/BufferSource.js +1 -3
  25. package/src/{v5 → v6}/inputSource/StringSource.js +1 -3
  26. package/src/{v5 → v6/valueParsers}/EntitiesParser.js +1 -3
  27. package/src/{v5 → v6}/valueParsers/booleanParser.js +1 -2
  28. package/src/{v5 → v6}/valueParsers/booleanParserExt.js +1 -2
  29. package/src/{v5 → v6}/valueParsers/currency.js +1 -3
  30. package/src/{v5 → v6}/valueParsers/join.js +1 -2
  31. package/src/{v5 → v6}/valueParsers/number.js +2 -4
  32. package/src/{v5 → v6}/valueParsers/trim.js +1 -3
  33. package/src/validator.js +5 -5
  34. package/src/xmlbuilder/json2xml.js +3 -4
  35. package/src/xmlbuilder/orderedJs2Xml.js +1 -2
  36. package/src/xmlparser/DocTypeReader.js +3 -5
  37. package/src/xmlparser/OptionsBuilder.js +2 -5
  38. package/src/xmlparser/OrderedObjParser.js +8 -11
  39. package/src/xmlparser/XMLParser.js +7 -9
  40. package/src/xmlparser/node2json.js +1 -2
  41. package/src/xmlparser/xmlNode.js +3 -6
  42. /package/src/{v5 → v6}/Report.js +0 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  <small>Note: If you find missing information about particular minor version, that version must have been changed without any functional change in this library.</small>
2
2
 
3
+ **5.0.0 / 2025-02-19**
4
+ - ESM support
5
+ - no change in the functionality, syntax, APIs, options, or documentation.
6
+
3
7
  **4.5.2 / 2025-02-18**
4
8
  - Fix null CDATA to comply with undefined behavior (#701) (By [Matthieu BOHEAS](https://github.com/Kelgors))
5
9
  - Fix(performance): Update check for leaf node in saveTextToParentTag function in OrderedObjParser.js (#707) (By [...](https://github.com/tomingtoming))
package/README.md CHANGED
@@ -99,10 +99,10 @@ Through OpenCollective
99
99
  * Supports parsing of PI (Processing Instruction) tags with XML declaration tags
100
100
  * And many more other features.
101
101
 
102
- ## v5
103
- I developed v5 in Apr 2023. And I didn't get the chance to complete all the features. I've ensured that new features don't impact performance. With v5, you have more control on parsing output. Check [docs](./docs/v5) for syntax help and basic understanding.
102
+ ## v6
103
+ I developed v6 in Apr 2023. And I didn't get the chance to complete all the features. I've ensured that new features don't impact performance. With v6, you have more control on parsing output. Check [docs](./docs/v6) for syntax help and basic understanding.
104
104
 
105
- Please leave a comment in discussion forum for your suggestions and if you really need v5.
105
+ Please leave a comment in discussion forum for your suggestions and if you really need v6.
106
106
 
107
107
  ## How to use
108
108
 
@@ -155,7 +155,7 @@ Bundle size
155
155
 
156
156
  ### Documents
157
157
  <table>
158
- <tr><td>v3</td><td>v4</td><td>v5</td></tr>
158
+ <tr><td>v3</td><td>v4</td><td>v6</td></tr>
159
159
  <tr>
160
160
  <td>
161
161
  <a href="./docs/v3/docs.md">documents</a>
@@ -170,11 +170,11 @@ Bundle size
170
170
  <li><a href="./docs/v4/7.PITags.md">PI Tag processing</a></li>
171
171
  </ol></td>
172
172
  <td><ol>
173
- <li></li><a href="./docs/v5/1.GettingStarted.md">Getting Started</a></li>
174
- <li><a href="./docs/v5/2.Features.md">Features</a></li>
175
- <li><a href="./docs/v5/3.Options.md">Options</a></li>
176
- <li><a href="./docs/v5/4.OutputBuilders.md">Output Builders</a></li>
177
- <li><a href="./docs/v5/5.ValueParsers.md">Value Parsers</a></li>
173
+ <li></li><a href="./docs/v6/1.GettingStarted.md">Getting Started</a></li>
174
+ <li><a href="./docs/v6/2.Features.md">Features</a></li>
175
+ <li><a href="./docs/v6/3.Options.md">Options</a></li>
176
+ <li><a href="./docs/v6/4.OutputBuilders.md">Output Builders</a></li>
177
+ <li><a href="./docs/v6/5.ValueParsers.md">Value Parsers</a></li>
178
178
  </ol></td>
179
179
  </tr>
180
180
  </table>
package/package.json CHANGED
@@ -1,18 +1,28 @@
1
1
  {
2
2
  "name": "fast-xml-parser",
3
- "version": "4.5.2",
3
+ "version": "5.0.0",
4
4
  "description": "Validate XML, Parse XML, Build XML without C/C++ based libraries",
5
- "main": "./src/fxp.js",
5
+ "main": "./lib/fxp.cjs",
6
+ "type": "module",
7
+ "module": "./src/fxp.js",
8
+ "types": "./src/fxp.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./src/fxp.js",
12
+ "require": "./lib/fxp.cjs",
13
+ "types": "./src/fxp.d.ts",
14
+ "default": "./src/fxp.js"
15
+ }
16
+ },
6
17
  "scripts": {
7
- "test": "nyc --reporter=lcov --reporter=text jasmine spec/*spec.js",
18
+ "test": "c8 --reporter=lcov --reporter=text jasmine spec/*spec.js",
8
19
  "test-types": "tsc --noEmit spec/typings/typings-test.ts",
9
20
  "unit": "jasmine",
10
21
  "coverage": "nyc report --reporter html --reporter text -t .nyc_output --report-dir .nyc_output/summary",
11
22
  "perf": "node ./benchmark/perfTest3.js",
12
- "lint": "eslint src/*.js spec/*.js",
13
- "bundle": "webpack --config webpack-prod.config.js",
23
+ "lint": "eslint src/**/*.js spec/**/*.js benchmark/**/*.js",
24
+ "bundle": "webpack --config webpack.esm.config.js && webpack --config webpack.cjs.config.js",
14
25
  "prettier": "prettier --write src/**/*.js",
15
- "publish-please": "publish-please",
16
26
  "checkReadiness": "publish-please --dry-run"
17
27
  },
18
28
  "bin": {
@@ -42,28 +52,31 @@
42
52
  ],
43
53
  "author": "Amit Gupta (https://solothought.com)",
44
54
  "license": "MIT",
45
- "devDependencies": {
55
+ "optionalDependencies": {
46
56
  "@babel/core": "^7.13.10",
47
57
  "@babel/plugin-transform-runtime": "^7.13.10",
48
58
  "@babel/preset-env": "^7.13.10",
49
59
  "@babel/register": "^7.13.8",
60
+ "babel-loader": "^8.2.2"
61
+ },
62
+ "devDependencies": {
50
63
  "@types/node": "20",
51
- "babel-loader": "^8.2.2",
64
+ "c8": "^10.1.3",
52
65
  "eslint": "^8.3.0",
53
66
  "he": "^1.2.0",
54
- "jasmine": "^3.6.4",
55
- "nyc": "^15.1.0",
56
- "prettier": "^1.19.1",
67
+ "jasmine": "^5.6.0",
68
+ "prettier": "^3.5.1",
57
69
  "publish-please": "^5.5.2",
58
70
  "typescript": "5",
59
71
  "webpack": "^5.64.4",
60
72
  "webpack-cli": "^4.9.1"
61
73
  },
62
- "typings": "src/fxp.d.ts",
63
- "funding": [{
64
- "type": "github",
65
- "url": "https://github.com/sponsors/NaturalIntelligence"
66
- }],
74
+ "funding": [
75
+ {
76
+ "type": "github",
77
+ "url": "https://github.com/sponsors/NaturalIntelligence"
78
+ }
79
+ ],
67
80
  "dependencies": {
68
81
  "strnum": "^1.0.5"
69
82
  }
package/src/cli/cli.js CHANGED
@@ -1,14 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
  /*eslint-disable no-console*/
4
- const fs = require('fs');
5
- const path = require('path');
6
- const {XMLParser, XMLValidator} = require("../fxp");
7
- const readToEnd = require('./read').readToEnd;
4
+ import fs from 'fs';
5
+ import {XMLParser, XMLValidator} from "../fxp.js";
6
+ import {readToEnd} from './read.js';
7
+ import cmdDetail from "./man.js"
8
+ import pkg from '../../package.json';
9
+ const version = pkg.version;
8
10
 
9
- const version = require('./../../package.json').version;
10
11
  if (process.argv[2] === '--help' || process.argv[2] === '-h') {
11
- console.log(require("./man"));
12
+ console.log(cmdDetail);
12
13
  } else if (process.argv[2] === '--version') {
13
14
  console.log(version);
14
15
  } else {
package/src/cli/man.js CHANGED
@@ -1,4 +1,7 @@
1
- module.exports = `Fast XML Parser 4.0.0
1
+ import pkg from '../../package.json';
2
+ const version = pkg.version;
3
+
4
+ export default `Fast XML Parser ${version}
2
5
  ----------------
3
6
  $ fxparser [-ns|-a|-c|-v|-V] <filename> [-o outputfile.json]
4
7
  $ cat xmlfile.xml | fxparser [-ns|-a|-c|-v|-V] [-o outputfile.json]
package/src/cli/read.js CHANGED
@@ -1,92 +1,43 @@
1
1
  'use strict';
2
2
 
3
- // Copyright 2013 Timothy J Fontaine <tjfontaine@gmail.com>
4
- //
5
- // Permission is hereby granted, free of charge, to any person obtaining a copy
6
- // of this software and associated documentation files (the 'Software'), to deal
7
- // in the Software without restriction, including without limitation the rights
8
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- // copies of the Software, and to permit persons to whom the Software is
10
- // furnished to do so, subject to the following conditions:
11
- //
12
- // The above copyright notice and this permission notice shall be included in
13
- // all copies or substantial portions of the Software.
14
- //
15
- // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- // THE SOFTWARE
3
+ import { Transform } from 'stream';
22
4
 
23
- /*
24
-
25
- Read any stream all the way to the end and trigger a single cb
26
-
27
- const http = require('http');
28
-
29
- const rte = require('readtoend');
30
-
31
- http.get('http://nodejs.org', function(response) {
32
- rte.readToEnd(response, function(err, body) {
33
- console.log(body);
34
- });
35
- });
36
-
37
- */
38
-
39
- let stream = require('stream');
40
- const util = require('util');
41
-
42
- if (!stream.Transform) {
43
- stream = require('readable-stream');
44
- }
45
-
46
- function ReadToEnd(opts) {
47
- if (!(this instanceof ReadToEnd)) {
48
- return new ReadToEnd(opts);
5
+ export default class ReadToEnd extends Transform {
6
+ constructor(options = {}) {
7
+ super(options);
8
+ this._encoding = options.encoding || 'utf8';
9
+ this._buffer = '';
49
10
  }
50
11
 
51
- stream.Transform.call(this, opts);
52
-
53
- this._rte_encoding = opts.encoding || 'utf8';
54
-
55
- this._buff = '';
56
- }
57
-
58
- module.exports = ReadToEnd;
59
- util.inherits(ReadToEnd, stream.Transform);
60
-
61
- ReadToEnd.prototype._transform = function(chunk, encoding, done) {
62
- this._buff += chunk.toString(this._rte_encoding);
63
- this.push(chunk);
64
- done();
65
- };
66
-
67
- ReadToEnd.prototype._flush = function(done) {
68
- this.emit('complete', undefined, this._buff);
69
- done();
70
- };
12
+ _transform(chunk, encoding, done) {
13
+ this._buffer += chunk.toString(this._encoding);
14
+ this.push(chunk);
15
+ done();
16
+ }
71
17
 
72
- ReadToEnd.readToEnd = function(stream, options, cb) {
73
- if (!cb) {
74
- cb = options;
75
- options = {};
18
+ _flush(done) {
19
+ this.emit('complete', null, this._buffer);
20
+ done();
76
21
  }
77
22
 
78
- const dest = new ReadToEnd(options);
23
+ static readToEnd(stream, options, callback) {
24
+ if (typeof options === 'function') {
25
+ callback = options;
26
+ options = {};
27
+ }
79
28
 
80
- stream.pipe(dest);
29
+ const dest = new ReadToEnd(options);
81
30
 
82
- stream.on('error', function(err) {
83
- stream.unpipe(dest);
84
- cb(err);
85
- });
31
+ stream.pipe(dest);
86
32
 
87
- dest.on('complete', cb);
33
+ stream.on('error', (err) => {
34
+ stream.unpipe(dest);
35
+ callback(err);
36
+ });
88
37
 
89
- dest.resume();
38
+ dest.on('complete', callback);
39
+ dest.resume();
90
40
 
91
- return dest;
92
- };
41
+ return dest;
42
+ }
43
+ }
package/src/fxp.js CHANGED
@@ -1,11 +1,14 @@
1
1
  'use strict';
2
2
 
3
- const validator = require('./validator');
4
- const XMLParser = require('./xmlparser/XMLParser');
5
- const XMLBuilder = require('./xmlbuilder/json2xml');
3
+ import {validate} from './validator.js';
4
+ import XMLParser from './xmlparser/XMLParser.js';
5
+ import XMLBuilder from './xmlbuilder/json2xml.js';
6
6
 
7
- module.exports = {
8
- XMLParser: XMLParser,
9
- XMLValidator: validator,
10
- XMLBuilder: XMLBuilder
11
- }
7
+ const XMLValidator = {
8
+ validate: validate
9
+ }
10
+ export {
11
+ XMLParser,
12
+ XMLValidator,
13
+ XMLBuilder
14
+ };
@@ -1,4 +1,4 @@
1
- function getIgnoreAttributesFn(ignoreAttributes) {
1
+ export default function getIgnoreAttributesFn(ignoreAttributes) {
2
2
  if (typeof ignoreAttributes === 'function') {
3
3
  return ignoreAttributes
4
4
  }
@@ -15,6 +15,4 @@ function getIgnoreAttributesFn(ignoreAttributes) {
15
15
  }
16
16
  }
17
17
  return () => false
18
- }
19
-
20
- module.exports = getIgnoreAttributesFn
18
+ }
package/src/util.js CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  const nameStartChar = ':A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD';
4
4
  const nameChar = nameStartChar + '\\-.\\d\\u00B7\\u0300-\\u036F\\u203F-\\u2040';
5
- const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*'
5
+ export const nameRegexp = '[' + nameStartChar + '][' + nameChar + ']*';
6
6
  const regexName = new RegExp('^' + nameRegexp + '$');
7
7
 
8
- const getAllMatches = function(string, regex) {
8
+ export function getAllMatches(string, regex) {
9
9
  const matches = [];
10
10
  let match = regex.exec(string);
11
11
  while (match) {
@@ -19,27 +19,27 @@ const getAllMatches = function(string, regex) {
19
19
  match = regex.exec(string);
20
20
  }
21
21
  return matches;
22
- };
22
+ }
23
23
 
24
- const isName = function(string) {
24
+ export const isName = function(string) {
25
25
  const match = regexName.exec(string);
26
26
  return !(match === null || typeof match === 'undefined');
27
- };
27
+ }
28
28
 
29
- exports.isExist = function(v) {
29
+ export function isExist(v) {
30
30
  return typeof v !== 'undefined';
31
- };
31
+ }
32
32
 
33
- exports.isEmptyObject = function(obj) {
33
+ export function isEmptyObject(obj) {
34
34
  return Object.keys(obj).length === 0;
35
- };
35
+ }
36
36
 
37
37
  /**
38
38
  * Copy all the properties of a into b.
39
39
  * @param {*} target
40
40
  * @param {*} a
41
41
  */
42
- exports.merge = function(target, a, arrayMode) {
42
+ export function merge(target, a, arrayMode) {
43
43
  if (a) {
44
44
  const keys = Object.keys(a); // will return an array of own properties
45
45
  const len = keys.length; //don't make it inline
@@ -51,22 +51,18 @@ exports.merge = function(target, a, arrayMode) {
51
51
  }
52
52
  }
53
53
  }
54
- };
54
+ }
55
55
  /* exports.merge =function (b,a){
56
56
  return Object.assign(b,a);
57
57
  } */
58
58
 
59
- exports.getValue = function(v) {
59
+ export function getValue(v) {
60
60
  if (exports.isExist(v)) {
61
61
  return v;
62
62
  } else {
63
63
  return '';
64
64
  }
65
- };
65
+ }
66
66
 
67
67
  // const fakeCall = function(a) {return a;};
68
- // const fakeCallNoReturn = function() {};
69
-
70
- exports.isName = isName;
71
- exports.getAllMatches = getAllMatches;
72
- exports.nameRegexp = nameRegexp;
68
+ // const fakeCallNoReturn = function() {};
@@ -1,4 +1,4 @@
1
- modules.export = {
1
+ export default {
2
2
  "<" : "<", //tag start
3
3
  ">" : ">", //tag end
4
4
  "/" : "/", //close tag
@@ -16,8 +16,7 @@ const htmlEntities = {
16
16
  "num_dec": { regex: /&#([0-9]{1,7});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 10)) },
17
17
  "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
18
18
  };
19
-
20
- class EntitiesParser{
19
+ export default class EntitiesParser{
21
20
  constructor(replaceHtmlEntities) {
22
21
  this.replaceHtmlEntities = replaceHtmlEntities;
23
22
  this.docTypeEntities = {};
@@ -103,5 +102,3 @@ function validateEntityName(name){
103
102
  }
104
103
  return name;
105
104
  }
106
-
107
- module.exports = EntitiesParser;
@@ -1,7 +1,7 @@
1
1
 
2
- const JsObjOutputBuilder = require("./OutputBuilders/JsObjBuilder");
2
+ import {JsObjOutputBuilder} from './OutputBuilders/JsObjBuilder.js';
3
3
 
4
- const defaultOptions = {
4
+ export const defaultOptions = {
5
5
  preserveOrder: false,
6
6
  removeNSPrefix: false, // remove NS from tag name or attribute name if true
7
7
  //ignoreRootElement : false,
@@ -35,7 +35,7 @@ const defaultOptions = {
35
35
  OutputBuilder: new JsObjOutputBuilder(),
36
36
  };
37
37
 
38
- const buildOptions = function(options) {
38
+ export const buildOptions = function(options) {
39
39
  const finalOptions = { ... defaultOptions};
40
40
  copyProperties(finalOptions,options)
41
41
  return finalOptions;
@@ -59,6 +59,3 @@ function copyProperties(target, source) {
59
59
  }
60
60
  }
61
61
  }
62
-
63
- exports.buildOptions = buildOptions;
64
- exports.defaultOptions = defaultOptions;
@@ -1,4 +1,4 @@
1
- class BaseOutputBuilder{
1
+ export default class BaseOutputBuilder{
2
2
  constructor(){
3
3
  // this.attributes = {};
4
4
  }
@@ -67,5 +67,3 @@ class BaseOutputBuilder{
67
67
  this.attributes = {}
68
68
  }
69
69
  }
70
-
71
- module.exports = BaseOutputBuilder;
@@ -1,6 +1,6 @@
1
- const {buildOptions,registerCommonValueParsers} = require("./ParserOptionsBuilder");
1
+ import {buildOptions,registerCommonValueParsers} from './ParserOptionsBuilder.js';
2
2
 
3
- class OutputBuilder{
3
+ export default class OutputBuilder{
4
4
  constructor(options){
5
5
  this.options = buildOptions(options);
6
6
  this.registeredParsers = registerCommonValueParsers(this.options);
@@ -16,7 +16,7 @@ class OutputBuilder{
16
16
  }
17
17
 
18
18
  const rootName = '!js_arr';
19
- const BaseOutputBuilder = require("./BaseOutputBuilder");
19
+ import BaseOutputBuilder from './BaseOutputBuilder.js';
20
20
 
21
21
  class JsArrBuilder extends BaseOutputBuilder{
22
22
 
@@ -1,6 +1,6 @@
1
- const {buildOptions,registerCommonValueParsers} = require("./ParserOptionsBuilder");
1
+ import {buildOptions,registerCommonValueParsers} from"./ParserOptionsBuilder";
2
2
 
3
- class OutputBuilder{
3
+ export default class OutputBuilder{
4
4
  constructor(options){
5
5
  this.options = buildOptions(options);
6
6
  this.registeredParsers = registerCommonValueParsers(this.options);
@@ -15,7 +15,7 @@ class OutputBuilder{
15
15
  }
16
16
  }
17
17
 
18
- const BaseOutputBuilder = require("./BaseOutputBuilder");
18
+ import BaseOutputBuilder from "./BaseOutputBuilder.js";
19
19
  const rootName = '^';
20
20
 
21
21
  class JsMinArrBuilder extends BaseOutputBuilder{
@@ -98,5 +98,3 @@ class JsMinArrBuilder extends BaseOutputBuilder{
98
98
  return this.root[rootName];
99
99
  }
100
100
  }
101
-
102
- module.exports = OutputBuilder;
@@ -1,8 +1,8 @@
1
1
 
2
2
 
3
- const {buildOptions,registerCommonValueParsers} = require("./ParserOptionsBuilder");
3
+ import {buildOptions,registerCommonValueParsers} from './ParserOptionsBuilder.js';
4
4
 
5
- class OutputBuilder{
5
+ export default class OutputBuilder{
6
6
  constructor(builderOptions){
7
7
  this.options = buildOptions(builderOptions);
8
8
  this.registeredParsers = registerCommonValueParsers(this.options);
@@ -17,7 +17,7 @@ class OutputBuilder{
17
17
  }
18
18
  }
19
19
 
20
- const BaseOutputBuilder = require("./BaseOutputBuilder");
20
+ import BaseOutputBuilder from './BaseOutputBuilder.js';
21
21
  const rootName = '^';
22
22
 
23
23
  class JsObjBuilder extends BaseOutputBuilder{
@@ -152,5 +152,3 @@ class JsObjBuilder extends BaseOutputBuilder{
152
152
  function isEmpty(obj) {
153
153
  return Object.keys(obj).length === 0;
154
154
  }
155
-
156
- module.exports = OutputBuilder;
@@ -1,7 +1,7 @@
1
- const trimParser = require("../valueParsers/trim")
2
- const booleanParser = require("../valueParsers/booleanParser")
3
- const currencyParser = require("../valueParsers/currency")
4
- const numberParser = require("../valueParsers/number")
1
+ import trimParser from "../valueParsers/trim";
2
+ import booleanParser from "../valueParsers/booleanParser";
3
+ import currencyParser from "../valueParsers/currency";
4
+ import numberParser from "../valueParsers/number";
5
5
 
6
6
  const defaultOptions={
7
7
  nameFor:{
@@ -44,7 +44,7 @@ const defaultOptions={
44
44
  const withJoin = ["trim","join", /*"entities",*/"number","boolean","currency"/*, "date"*/]
45
45
  const withoutJoin = ["trim", /*"entities",*/"number","boolean","currency"/*, "date"*/]
46
46
 
47
- function buildOptions(options){
47
+ export function buildOptions(options){
48
48
  //clone
49
49
  const finalOptions = { ... defaultOptions};
50
50
 
@@ -78,7 +78,7 @@ function copyProperties(target, source) {
78
78
  }
79
79
  }
80
80
 
81
- function registerCommonValueParsers(options){
81
+ export function registerCommonValueParsers(options){
82
82
  return {
83
83
  "trim": new trimParser(),
84
84
  // "join": this.entityParser.parse,
@@ -92,8 +92,3 @@ function registerCommonValueParsers(options){
92
92
  // "date": this.entityParser.parse,
93
93
  }
94
94
  }
95
-
96
- module.exports = {
97
- buildOptions : buildOptions,
98
- registerCommonValueParsers: registerCommonValueParsers
99
- }
@@ -1,4 +1,4 @@
1
- class TagPath{
1
+ export default class TagPath{
2
2
  constructor(pathStr){
3
3
  let text = "";
4
4
  let tName = "";
@@ -1,6 +1,6 @@
1
- const TagPath = require("./TagPath");
1
+ import {TagPath} from './TagPath.js';
2
2
 
3
- class TagPathMatcher{
3
+ export default class TagPathMatcher{
4
4
  constructor(stack,node){
5
5
  this.stack = stack;
6
6
  this.node= node;
@@ -10,6 +10,4 @@ class TagPathMatcher{
10
10
  const tagPath = new TagPath(path);
11
11
  return tagPath.match(this.stack, this.node);
12
12
  }
13
- }
14
-
15
- module.exports = TagPathMatcher;
13
+ }
@@ -1,7 +1,7 @@
1
- const { buildOptions} = require("./OptionsBuilder");
2
- const Xml2JsParser = require("./Xml2JsParser");
1
+ import { buildOptions} from './OptionsBuilder.js';
2
+ import Xml2JsParser from './Xml2JsParser.js';
3
3
 
4
- class XMLParser{
4
+ export default class XMLParser{
5
5
 
6
6
  constructor(options){
7
7
  this.externalEntities = {};
@@ -81,5 +81,3 @@ function isStream(stream){
81
81
  if(stream && typeof stream.read === "function" && typeof stream.on === "function" && typeof stream.readableEnded === "boolean") return true;
82
82
  return false;
83
83
  }
84
-
85
- module.exports = XMLParser;
@@ -1,10 +1,10 @@
1
- const StringSource = require("./inputSource/StringSource");
2
- const BufferSource = require("./inputSource/BufferSource");
3
- const {readTagExp,readClosingTagName} = require("./XmlPartReader");
4
- const {readComment, readCdata,readDocType,readPiTag} = require("./XmlSpecialTagsReader");
5
- const TagPath = require("./TagPath");
6
- const TagPathMatcher = require("./TagPathMatcher");
7
- const EntitiesParser = require('./EntitiesParser');
1
+ import StringSource from './inputSource/StringSource.js';
2
+ import BufferSource from './inputSource/BufferSource.js';
3
+ import {readTagExp,readClosingTagName} from './XmlPartReader.js';
4
+ import {readComment, readCdata,readDocType,readPiTag} from './XmlSpecialTagsReader.js';
5
+ import TagPath from './TagPath.js';
6
+ import TagPathMatcher from './TagPathMatcher.js';
7
+ import EntitiesParser from './EntitiesParser.js';
8
8
 
9
9
  //To hold the data of current tag
10
10
  //This is usually used to compare jpath expression against current tag
@@ -16,7 +16,7 @@ class TagDetail{
16
16
  }
17
17
  }
18
18
 
19
- class Xml2JsParser {
19
+ export default class Xml2JsParser {
20
20
  constructor(options) {
21
21
  this.options = options;
22
22
 
@@ -233,5 +233,3 @@ function resolveNameSpace(name, removeNSPrefix) {
233
233
  }
234
234
  return name;
235
235
  }
236
-
237
- module.exports = Xml2JsParser;
@@ -6,7 +6,7 @@
6
6
  * @param {string} tagName
7
7
  * @param {number} i : start index
8
8
  */
9
- function readStopNode(xmlDoc, tagName, i){
9
+ export function readStopNode(xmlDoc, tagName, i){
10
10
  const startIndex = i;
11
11
  // Starting at 1 since we already have an open tag
12
12
  let openTagCount = 1;
@@ -55,7 +55,7 @@ function readStopNode(xmlDoc, tagName, i){
55
55
  * @param {Source} source
56
56
  * @returns tag name
57
57
  */
58
- function readClosingTagName(source){
58
+ export function readClosingTagName(source){
59
59
  let text = ""; //temporary data
60
60
  while(source.canRead()){
61
61
  let ch = source.readCh();
@@ -77,7 +77,7 @@ function readClosingTagName(source){
77
77
  * @param {number} startIndex starting index
78
78
  * @returns tag expression includes tag name & attribute string
79
79
  */
80
- function readTagExp(parser) {
80
+ export function readTagExp(parser) {
81
81
  let inSingleQuotes = false;
82
82
  let inDoubleQuotes = false;
83
83
  let i;
@@ -107,7 +107,7 @@ function readTagExp(parser) {
107
107
  return buildTagExpObj(exp, parser)
108
108
  }
109
109
 
110
- function readPiExp(parser) {
110
+ export function readPiExp(parser) {
111
111
  let inSingleQuotes = false;
112
112
  let inDoubleQuotes = false;
113
113
  let i;
@@ -208,9 +208,3 @@ const getAllMatches = function(string, regex) {
208
208
  return matches;
209
209
  };
210
210
 
211
- module.exports = {
212
- readStopNode: readStopNode,
213
- readClosingTagName: readClosingTagName,
214
- readTagExp: readTagExp,
215
- readPiExp: readPiExp,
216
- }
@@ -1,6 +1,6 @@
1
- const {readPiExp} = require("./XmlPartReader");
1
+ import {readPiExp} from './XmlPartReader.js';
2
2
 
3
- function readCdata(parser){
3
+ export function readCdata(parser){
4
4
  //<![ are already read till this point
5
5
  let str = parser.source.readStr(6); //CDATA[
6
6
  parser.source.updateBufferBoundary(6);
@@ -10,7 +10,7 @@ function readCdata(parser){
10
10
  let text = parser.source.readUpto("]]>");
11
11
  parser.outputBuilder.addCdata(text);
12
12
  }
13
- function readPiTag(parser){
13
+ export function readPiTag(parser){
14
14
  //<? are already read till this point
15
15
  let tagExp = readPiExp(parser, "?>");
16
16
  if(!tagExp) throw new Error("Invalid Pi Tag expression.");
@@ -22,7 +22,7 @@ function readPiTag(parser){
22
22
  }
23
23
  }
24
24
 
25
- function readComment(parser){
25
+ export function readComment(parser){
26
26
  //<!- are already read till this point
27
27
  let ch = parser.source.readCh();
28
28
  if(ch !== "-") throw new Error(`Invalid comment expression at ${parser.source.line}:${parser.source.cols}`);
@@ -36,7 +36,7 @@ const DOCTYPE_tags = {
36
36
  "AT":/^TLIST\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+[^\s]+\s+$/m,
37
37
  "NO":/^TATION.+$/m
38
38
  }
39
- function readDocType(parser){
39
+ export function readDocType(parser){
40
40
  //<!D are already read till this point
41
41
  let str = parser.source.readStr(6); //OCTYPE
42
42
  parser.source.updateBufferBoundary(6);
@@ -109,10 +109,3 @@ function registerEntity(parser){
109
109
  }
110
110
  }
111
111
  }
112
-
113
- module.exports = {
114
- readCdata: readCdata,
115
- readComment:readComment,
116
- readDocType:readDocType,
117
- readPiTag:readPiTag
118
- }
@@ -2,7 +2,7 @@ const Constants = {
2
2
  space: 32,
3
3
  tab: 9
4
4
  }
5
- class BufferSource{
5
+ export default class BufferSource{
6
6
  constructor(bytesArr){
7
7
  this.line = 1;
8
8
  this.cols = 0;
@@ -114,5 +114,3 @@ readUptoCloseTag(stopStr) { //stopStr: "</tagname"
114
114
  }
115
115
 
116
116
  }
117
-
118
- module.exports = BufferSource;
@@ -1,7 +1,7 @@
1
1
  const whiteSpaces = [" ", "\n", "\t"];
2
2
 
3
3
 
4
- class StringSource{
4
+ export default class StringSource{
5
5
  constructor(str){
6
6
  this.line = 1;
7
7
  this.cols = 0;
@@ -119,5 +119,3 @@ class StringSource{
119
119
  }
120
120
 
121
121
  }
122
-
123
- module.exports = StringSource;
@@ -17,7 +17,7 @@ const htmlEntities = {
17
17
  "num_hex": { regex: /&#x([0-9a-fA-F]{1,6});/g, val : (_, str) => String.fromCharCode(Number.parseInt(str, 16)) },
18
18
  };
19
19
 
20
- class EntitiesParser{
20
+ export default class EntitiesParser{
21
21
  constructor(replaceHtmlEntities) {
22
22
  this.replaceHtmlEntities = replaceHtmlEntities;
23
23
  this.docTypeEntities = {};
@@ -103,5 +103,3 @@ function validateEntityName(name){
103
103
  }
104
104
  return name;
105
105
  }
106
-
107
- module.exports = EntitiesParser;
@@ -1,4 +1,4 @@
1
- class boolParser{
1
+ export default class boolParser{
2
2
  constructor(trueList, falseList){
3
3
  if(trueList)
4
4
  this.trueList = trueList;
@@ -20,4 +20,3 @@ class boolParser{
20
20
  return val;
21
21
  }
22
22
  }
23
- module.exports = boolParser;
@@ -1,4 +1,4 @@
1
- function boolParserExt(val){
1
+ export default function boolParserExt(val){
2
2
  if(isArray(val)){
3
3
  for (let i = 0; i < val.length; i++) {
4
4
  val[i] = parse(val[i])
@@ -17,4 +17,3 @@ function parse(val){
17
17
  }
18
18
  return val;
19
19
  }
20
- module.exports = boolParserExt;
@@ -16,7 +16,7 @@ const symbol = "(?:\$|€|¥|₹)?";
16
16
 
17
17
  const currencyCheckRegex = /^\s*(?:-|\+)?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d{1,2})?\s*(?:\$|€|¥|₹)?\s*$/u;
18
18
 
19
- class CurrencyParser{
19
+ export default class CurrencyParser{
20
20
  constructor(options){
21
21
  this.options = options || defaultOptions;
22
22
  }
@@ -36,5 +36,3 @@ class CurrencyParser{
36
36
  }
37
37
  }
38
38
  CurrencyParser.defaultOptions = defaultOptions;
39
-
40
- module.exports = CurrencyParser;
@@ -4,11 +4,10 @@
4
4
  * @param {string} by
5
5
  * @returns
6
6
  */
7
- function join(val, by=" "){
7
+ export default function join(val, by=" "){
8
8
  if(isArray(val)){
9
9
  val.join(by)
10
10
  }
11
11
  return val;
12
12
  }
13
13
 
14
- module.exports = join;
@@ -1,7 +1,7 @@
1
- const toNumber = require("strnum");
1
+ import toNumber from "strnum";
2
2
 
3
3
 
4
- class numParser{
4
+ export default class numParser{
5
5
  constructor(options){
6
6
  this.options = options;
7
7
  }
@@ -12,5 +12,3 @@ class numParser{
12
12
  return val;
13
13
  }
14
14
  }
15
-
16
- module.exports = numParser;
@@ -1,8 +1,6 @@
1
- class trimmer{
1
+ export default class trimmer{
2
2
  parse(val){
3
3
  if(typeof val === "string") return val.trim();
4
4
  else return val;
5
5
  }
6
6
  }
7
-
8
- module.exports = trimmer;
package/src/validator.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const util = require('./util');
3
+ import {getAllMatches, isName} from './util.js';
4
4
 
5
5
  const defaultOptions = {
6
6
  allowBooleanAttributes: false, //A tag can have attributes without any value
@@ -8,7 +8,7 @@ const defaultOptions = {
8
8
  };
9
9
 
10
10
  //const tagsPattern = new RegExp("<\\/?([\\w:\\-_\.]+)\\s*\/?>","g");
11
- exports.validate = function (xmlData, options) {
11
+ export function validate(xmlData, options) {
12
12
  options = Object.assign({}, defaultOptions, options);
13
13
 
14
14
  //xmlData = xmlData.replace(/(\r\n|\n|\r)/gm,"");//make it single line
@@ -321,7 +321,7 @@ function validateAttributeString(attrStr, options) {
321
321
 
322
322
  //if(attrStr.trim().length === 0) return true; //empty string
323
323
 
324
- const matches = util.getAllMatches(attrStr, validAttrStrRegxp);
324
+ const matches = getAllMatches(attrStr, validAttrStrRegxp);
325
325
  const attrNames = {};
326
326
 
327
327
  for (let i = 0; i < matches.length; i++) {
@@ -399,13 +399,13 @@ function getErrorObject(code, message, lineNumber) {
399
399
  }
400
400
 
401
401
  function validateAttrName(attrName) {
402
- return util.isName(attrName);
402
+ return isName(attrName);
403
403
  }
404
404
 
405
405
  // const startsWithXML = /^xml/i;
406
406
 
407
407
  function validateTagName(tagname) {
408
- return util.isName(tagname) /* && !tagname.match(startsWithXML) */;
408
+ return isName(tagname) /* && !tagname.match(startsWithXML) */;
409
409
  }
410
410
 
411
411
  //this function returns the line number for the character at the given index
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
  //parse Empty Node as self closing node
3
- const buildFromOrderedJs = require('./orderedJs2Xml');
4
- const getIgnoreAttributesFn = require('../ignoreAttributes')
3
+ import buildFromOrderedJs from './orderedJs2Xml.js';
4
+ import getIgnoreAttributesFn from "../ignoreAttributes.js";
5
5
 
6
6
  const defaultOptions = {
7
7
  attributeNamePrefix: '@_',
@@ -37,7 +37,7 @@ const defaultOptions = {
37
37
  oneListGroup: false
38
38
  };
39
39
 
40
- function Builder(options) {
40
+ export default function Builder(options) {
41
41
  this.options = Object.assign({}, defaultOptions, options);
42
42
  if (this.options.ignoreAttributes === true || this.options.attributesGroupName) {
43
43
  this.isAttribute = function(/*a*/) {
@@ -283,4 +283,3 @@ function isAttribute(name /*, options*/) {
283
283
  }
284
284
  }
285
285
 
286
- module.exports = Builder;
@@ -6,7 +6,7 @@ const EOL = "\n";
6
6
  * @param {any} options
7
7
  * @returns
8
8
  */
9
- function toXml(jArray, options) {
9
+ export default function toXml(jArray, options) {
10
10
  let indentation = "";
11
11
  if (options.format && options.indentBy.length > 0) {
12
12
  indentation = EOL;
@@ -132,4 +132,3 @@ function replaceEntitiesValue(textValue, options) {
132
132
  }
133
133
  return textValue;
134
134
  }
135
- module.exports = toXml;
@@ -1,7 +1,7 @@
1
- const util = require('../util');
1
+ import {isName} from '../util.js';
2
2
 
3
3
  //TODO: handle comments
4
- function readDocType(xmlData, i){
4
+ export default function readDocType(xmlData, i){
5
5
 
6
6
  const entities = {};
7
7
  if( xmlData[i + 3] === 'O' &&
@@ -144,10 +144,8 @@ function isNotation(xmlData, i){
144
144
  }
145
145
 
146
146
  function validateEntityName(name){
147
- if (util.isName(name))
147
+ if (isName(name))
148
148
  return name;
149
149
  else
150
150
  throw new Error(`Invalid entity name ${name}`);
151
151
  }
152
-
153
- module.exports = readDocType;
@@ -1,5 +1,5 @@
1
1
 
2
- const defaultOptions = {
2
+ export const defaultOptions = {
3
3
  preserveOrder: false,
4
4
  attributeNamePrefix: '@_',
5
5
  attributesGroupName: false,
@@ -40,9 +40,6 @@ const defaultOptions = {
40
40
  // skipEmptyListItem: false
41
41
  };
42
42
 
43
- const buildOptions = function(options) {
43
+ export const buildOptions = function(options) {
44
44
  return Object.assign({}, defaultOptions, options);
45
45
  };
46
-
47
- exports.buildOptions = buildOptions;
48
- exports.defaultOptions = defaultOptions;
@@ -1,11 +1,11 @@
1
1
  'use strict';
2
2
  ///@ts-check
3
3
 
4
- const util = require('../util');
5
- const xmlNode = require('./xmlNode');
6
- const readDocType = require("./DocTypeReader");
7
- const toNumber = require("strnum");
8
- const getIgnoreAttributesFn = require('../ignoreAttributes')
4
+ import {getAllMatches, isExist} from '../util.js';
5
+ import xmlNode from './xmlNode.js';
6
+ import readDocType from './DocTypeReader.js';
7
+ import toNumber from "strnum";
8
+ import getIgnoreAttributesFn from "../ignoreAttributes.js";
9
9
 
10
10
  // const regx =
11
11
  // '<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
@@ -14,7 +14,7 @@ const getIgnoreAttributesFn = require('../ignoreAttributes')
14
14
  //const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
15
15
  //const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
16
16
 
17
- class OrderedObjParser{
17
+ export default class OrderedObjParser{
18
18
  constructor(options){
19
19
  this.options = options;
20
20
  this.currentNode = null;
@@ -131,7 +131,7 @@ function buildAttributesMap(attrStr, jPath, tagName) {
131
131
  // attrStr = attrStr.replace(/\r?\n/g, ' ');
132
132
  //attrStr = attrStr || attrStr.trim();
133
133
 
134
- const matches = util.getAllMatches(attrStr, attrsRegx);
134
+ const matches = getAllMatches(attrStr, attrsRegx);
135
135
  const len = matches.length; //don't make it inline
136
136
  const attrs = {};
137
137
  for (let i = 0; i < len; i++) {
@@ -594,13 +594,10 @@ function parseValue(val, shouldParse, options) {
594
594
  else if(newval === 'false' ) return false;
595
595
  else return toNumber(val, options);
596
596
  } else {
597
- if (util.isExist(val)) {
597
+ if (isExist(val)) {
598
598
  return val;
599
599
  } else {
600
600
  return '';
601
601
  }
602
602
  }
603
603
  }
604
-
605
-
606
- module.exports = OrderedObjParser;
@@ -1,9 +1,9 @@
1
- const { buildOptions} = require("./OptionsBuilder");
2
- const OrderedObjParser = require("./OrderedObjParser");
3
- const { prettify} = require("./node2json");
4
- const validator = require('../validator');
1
+ import { buildOptions} from './OptionsBuilder.js';
2
+ import OrderedObjParser from './OrderedObjParser.js';
3
+ import prettify from './node2json.js';
4
+ import {validate} from "../validator.js";
5
5
 
6
- class XMLParser{
6
+ export default class XMLParser{
7
7
 
8
8
  constructor(options){
9
9
  this.externalEntities = {};
@@ -25,7 +25,7 @@ class XMLParser{
25
25
  if( validationOption){
26
26
  if(validationOption === true) validationOption = {}; //validate with default options
27
27
 
28
- const result = validator.validate(xmlData, validationOption);
28
+ const result = validate(xmlData, validationOption);
29
29
  if (result !== true) {
30
30
  throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` )
31
31
  }
@@ -53,6 +53,4 @@ class XMLParser{
53
53
  this.externalEntities[key] = value;
54
54
  }
55
55
  }
56
- }
57
-
58
- module.exports = XMLParser;
56
+ }
@@ -6,7 +6,7 @@
6
6
  * @param {any} options
7
7
  * @returns
8
8
  */
9
- function prettify(node, options){
9
+ export default function prettify(node, options){
10
10
  return compress( node, options);
11
11
  }
12
12
 
@@ -110,4 +110,3 @@ function isLeafTag(obj, options){
110
110
 
111
111
  return false;
112
112
  }
113
- exports.prettify = prettify;
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- class XmlNode{
3
+ export default class XmlNode{
4
4
  constructor(tagname) {
5
5
  this.tagname = tagname;
6
6
  this.child = []; //nested tags, text, cdata, comments in order
@@ -18,8 +18,5 @@ class XmlNode{
18
18
  }else{
19
19
  this.child.push( { [node.tagname]: node.child });
20
20
  }
21
- };
22
- };
23
-
24
-
25
- module.exports = XmlNode;
21
+ }
22
+ }
File without changes