xml-toolkit 0.0.7 → 0.0.8

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.
@@ -65,7 +65,7 @@ const AttributesMap = class extends Map {
65
65
 
66
66
  }
67
67
 
68
- return super.set (k, this.fixText (v))
68
+ return super.set (k, v === '' ? null : this.fixText (v))
69
69
 
70
70
  }
71
71
 
@@ -1,4 +1,4 @@
1
- const SAXEvent = require ('./SAXEvent.js')
1
+ const SAXEvent = require ('./SAXEvent.js'), {CHARACTERS, END_ELEMENT} = SAXEvent.TYPES
2
2
 
3
3
  const set = (o, k, nv) => {
4
4
 
@@ -12,13 +12,19 @@ const set = (o, k, nv) => {
12
12
 
13
13
  const xform = ({children, attributes}) => {
14
14
 
15
- let o = attributes == null ? {} : Object.fromEntries (attributes.entries ())
15
+ let o = null
16
+
17
+ if (attributes != null && attributes.size !== 0) o = Object.fromEntries (attributes.entries ())
16
18
 
17
19
  if (children != null) for (const child of children) switch (child.type) {
18
20
 
19
- case SAXEvent.TYPES.CHARACTERS: return child.text
21
+ case CHARACTERS: return child.text
20
22
 
21
- case SAXEvent.TYPES.END_ELEMENT: set (o, child.localName, xform (child))
23
+ case END_ELEMENT:
24
+
25
+ if (o === null) o = {}
26
+
27
+ set (o, child.localName, xform (child))
22
28
 
23
29
  }
24
30
 
package/lib/XMLReader.js CHANGED
@@ -14,7 +14,7 @@ const XMLReader = class extends Transform {
14
14
  options.decodeStrings = false
15
15
  options.objectMode = true
16
16
 
17
- if (!('stripSpace' in options)) options.stripSpace = false
17
+ if (!('stripSpace' in options)) options.stripSpace = ('filterElements' in options)
18
18
  assert (options.stripSpace === true || options.stripSpace === false, 'options.stripSpace must be boolean, not ' + typeof options.stripSpace)
19
19
 
20
20
  if (!('useEntities' in options)) options.useEntities = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xml-toolkit",
3
- "version": "0.0.7",
3
+ "version": "0.0.8",
4
4
  "description": "Collection of classes for dealing with XML",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test/soap.xml CHANGED
@@ -2,7 +2,7 @@
2
2
  <SOAP-ENV:Header/>
3
3
  <SOAP-ENV:Body>
4
4
  <SendRequestRequest xmlns="urn://x-artefacts-smev-gov-ru/services/message-exchange/types/1.1" xmlns:ns0="urn://x-artefacts-smev-gov-ru/services/message-exchange/types/basic/1.1">
5
- <SenderProvidedRequestData Id="Ue7e71ce1-7ce3-4ca5-a689-1a8f2edbb1af">
5
+ <SenderProvidedRequestData Id="Ue7e71ce1-7ce3-4ca5-a689-1a8f2edbb1af" IDDQD="">
6
6
  <MessageID>3931cda8-3245-11ec-b0bc-000c293433a0</MessageID>
7
7
  <ns0:MessagePrimaryContent>
8
8
  <ExportDebtRequestsRequest xmlns="urn:dom.gosuslugi.ru/debt-responses/1.0.0" xmlns:ns0="urn:dom.gosuslugi.ru/common/1.2.0">
package/test/test.js CHANGED
@@ -59,8 +59,8 @@ console.log (xml)
59
59
 
60
60
  const sax = new XMLReader ({
61
61
  // stripSpace: true,
62
- // filterElements: 'SendRequestRequest',
63
- // map: MoxyLikeJsonEncoder ({wrap: 1})
62
+ filterElements: 'SendRequestRequest',
63
+ map: MoxyLikeJsonEncoder ({wrap: 1})
64
64
  })
65
65
 
66
66
  /*
@@ -77,26 +77,22 @@ console.log (xml)
77
77
  })
78
78
  */
79
79
 
80
-
81
- // sax.process (fs.createReadStream ('test/' + fn))
82
-
83
- sax.process (xml)
84
-
80
+
85
81
  //console.log (sax)
86
82
  //console.log (sax.isSAX)
87
83
 
88
-
84
+ /*
89
85
  let s = ''
90
86
  for await (const e of sax) {
91
- // s += xml
92
- console.log ([e.type, e.isStartElement, e.isEndElement , e.isCharacters])
87
+ s += xml
88
+ // console.log ([e.type, e.isStartElement, e.isEndElement , e.isCharacters])
93
89
  }
94
-
90
+ */
95
91
  //console.log ([xml, s])
96
92
 
97
- // const v = await sax.findFirst ()
93
+ const v = await sax.process (xml).findFirst ()
98
94
 
99
- // console.log (JSON.stringify (v, null, 2))
95
+ console.log (JSON.stringify (v, null, 2))
100
96
 
101
97
  }
102
98