xml-toolkit 0.0.8 → 0.0.9

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/index.js CHANGED
@@ -3,5 +3,6 @@ const XMLReader = require ('./lib/XMLReader')
3
3
  const SAXEvent = require ('./lib/SAXEvent')
4
4
  const AttributesMap = require ('./lib/AttributesMap')
5
5
  const MoxyLikeJsonEncoder = require ('./lib/MoxyLikeJsonEncoder')
6
+ const XMLNode = require ('./lib/XMLNode')
6
7
 
7
- module.exports = {XMLLexer, XMLReader, SAXEvent, AttributesMap, MoxyLikeJsonEncoder}
8
+ module.exports = {XMLLexer, XMLReader, SAXEvent, AttributesMap, MoxyLikeJsonEncoder, XMLNode}
@@ -1,45 +1,63 @@
1
1
  const SAXEvent = require ('./SAXEvent.js'), {CHARACTERS, END_ELEMENT} = SAXEvent.TYPES
2
2
 
3
- const set = (o, k, nv) => {
3
+ const GET_LOCAL_NAME = (localName, namespaceURI) => localName
4
4
 
5
- if (!(k in o)) return o [k] = nv
5
+ const MoxyLikeJsonEncoder = function (options = {}) {
6
6
 
7
- const ov = o [k]; if (!Array.isArray (ov)) o [k] = [ov]
8
-
9
- o [k].push (nv)
7
+ let {getName} = options; if (!getName) getName = GET_LOCAL_NAME
10
8
 
11
- }
9
+ const xform = ({children, attributes}) => {
12
10
 
13
- const xform = ({children, attributes}) => {
11
+ let o = null
14
12
 
15
- let o = null
13
+ const set = (nv, localName, namespaceURI) => {
14
+
15
+ const k = getName (localName, namespaceURI)
16
16
 
17
- if (attributes != null && attributes.size !== 0) o = Object.fromEntries (attributes.entries ())
17
+ if (o === null) return o = {[k]: nv}
18
18
 
19
- if (children != null) for (const child of children) switch (child.type) {
19
+ if (!(k in o)) return o [k] = nv
20
20
 
21
- case CHARACTERS: return child.text
21
+ const ov = o [k]; if (!Array.isArray (ov)) return o [k] = [ov, nv]
22
22
 
23
- case END_ELEMENT:
24
-
25
- if (o === null) o = {}
23
+ ov.push (nv)
24
+
25
+ }
26
+
27
+ if (attributes != null)
28
+
29
+ for (const [name, value] of attributes.entries ())
26
30
 
27
- set (o, child.localName, xform (child))
31
+ set (value, attributes.getLocalName (name), attributes.getNamespaceURI (name))
28
32
 
29
- }
30
-
31
- return o
33
+ if (children != null) for (const child of children) switch (child.type) {
32
34
 
33
- }
35
+ case CHARACTERS:
36
+
37
+ return child.text
34
38
 
35
- const MoxyLikeJsonEncoder = function (options = {}) {
39
+ case END_ELEMENT:
40
+
41
+ set (xform (child), child.localName, child.namespaceURI)
42
+
43
+ }
44
+
45
+ return o
46
+
47
+ }
36
48
 
37
49
  return function (node) {
38
50
 
39
51
  let result = xform (node)
40
-
41
- return options.wrap ? {[node.localName]: result} : result
42
-
52
+
53
+ const {wrap, map} = options
54
+
55
+ if (wrap) result = {[getName (node.localName, node.namespaceURI)]: result}
56
+
57
+ if (map) result = map (result)
58
+
59
+ return result
60
+
43
61
  }
44
62
 
45
63
  }
package/lib/XMLNode.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const SAXEvent = require ('./SAXEvent.js')
2
2
  const AttributesMap = require ('./AttributesMap')
3
3
  const NamespacesMap = require ('./NamespacesMap')
4
+ const MoxyLikeJsonEncoder = require ('./MoxyLikeJsonEncoder')
4
5
 
5
6
  const XML_READER = Symbol ('_xmlReader')
6
7
  const ATTRIBUTES = Symbol ('_attributes')
@@ -31,8 +32,9 @@ const XMLNode = class extends SAXEvent {
31
32
 
32
33
  e [PARENT] = this [PARENT]
33
34
  e [LEVEL] = this [LEVEL]
34
- e [ATTRIBUTES] = this [ATTRIBUTES]
35
- e [NS_MAP] = this [NS_MAP]
35
+
36
+ if (ATTRIBUTES in this) e [ATTRIBUTES] = this [ATTRIBUTES]
37
+ if (NS_MAP in this) e [NS_MAP] = this [NS_MAP]
36
38
 
37
39
  return e
38
40
 
@@ -96,7 +98,9 @@ const XMLNode = class extends SAXEvent {
96
98
 
97
99
  get namespaceURI () {
98
100
 
99
- return this.namespacesMap.getNamespaceURI (this.name, true)
101
+ const {namespacesMap} = this; if (namespacesMap == null) return null
102
+
103
+ return namespacesMap.getNamespaceURI (this.name, true)
100
104
 
101
105
  }
102
106
 
@@ -138,4 +142,6 @@ XMLNode.getLocalName = name => {
138
142
 
139
143
  }
140
144
 
145
+ XMLNode.toObject = MoxyLikeJsonEncoder
146
+
141
147
  module.exports = XMLNode
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Collection of classes for dealing with XML",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const fs = require ('fs')
2
2
  const assert = require ('assert')
3
- const {XMLReader, SAXEvent, XMLLexer, AttributesMap, MoxyLikeJsonEncoder} = require ('../')
3
+ const {XMLReader, SAXEvent, XMLLexer, AttributesMap, XMLNode} = require ('../')
4
4
 
5
5
  async function test_001_lexer_sync (fn) {
6
6
 
@@ -60,7 +60,11 @@ console.log (xml)
60
60
  const sax = new XMLReader ({
61
61
  // stripSpace: true,
62
62
  filterElements: 'SendRequestRequest',
63
- map: MoxyLikeJsonEncoder ({wrap: 1})
63
+ map: XMLNode.toObject ({
64
+ // wrap: 1,
65
+ getName: (localName, namespaceURI) => (!namespaceURI ? '' : '{' + namespaceURI + '}') + localName,
66
+ map: o => Object.fromEntries (Object.entries (o).map (([k, v]) => [k + '111', v]))
67
+ })
64
68
  })
65
69
 
66
70
  /*