Skip to content

Commit 7fa2b22

Browse files
committed
Create a promise-specific version of parseString
Examples: ```js // Via root API xml2js.parseStringPromise('< ... >', options) .then(function (result) { /* ... */ }) .catch(function (err) { /* ... */ }) // Via parser var parser = new xml2js.Parser(options); parser.parseStringPromise('< ... >') .then(function (result) { /* ... */ }) .catch(function (err) { /* ... */ }) ```
1 parent 847e0c3 commit 7fa2b22

File tree

5 files changed

+95
-1
lines changed

5 files changed

+95
-1
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@ not, we got you covered! Starting with 0.2.8 you can also leave it out, in
104104
which case `xml2js` will helpfully add it for you, no bad surprises and
105105
inexplicable bugs!
106106

107+
Promise usage
108+
-------------
109+
110+
```javascript
111+
var xml2js = require('xml2js');
112+
var xml = '<foo></foo>';
113+
114+
// With parser
115+
var parser = new xml2js.Parser(/* options */);
116+
parser.parseStringPromise(data).then(function (result) {
117+
console.dir(result);
118+
console.log('Done');
119+
})
120+
.catch(function (err) {
121+
// Failed
122+
});
123+
124+
// Without parser
125+
xml2js.parseStringPromise(data /*, options */).then(function (result) {
126+
console.dir(result);
127+
console.log('Done');
128+
})
129+
.catch(function (err) {
130+
// Failed
131+
});
132+
```
133+
107134
Parsing multiple files
108135
----------------------
109136

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,19 @@
6363
"lib": "./lib"
6464
},
6565
"scripts": {
66+
"build": "cake build",
6667
"test": "zap",
6768
"coverage": "nyc npm test && nyc report",
68-
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
69+
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
70+
"doc": "cake doc"
6971
},
7072
"repository": {
7173
"type": "git",
7274
"url": "https://github.com/Leonidas-from-XIV/node-xml2js.git"
7375
},
7476
"dependencies": {
7577
"sax": ">=0.6.0",
78+
"util.promisify": "~1.0.0",
7679
"xmlbuilder": "~9.0.1"
7780
},
7881
"devDependencies": {

src/parser.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
sax = require 'sax'
44
events = require 'events'
5+
promisify = require 'util.promisify'
56
bom = require './bom'
67
processors = require './processors'
78
setImmediate = require('timers').setImmediate
@@ -253,6 +254,9 @@ class exports.Parser extends events.EventEmitter
253254
else if @saxParser.ended
254255
throw err
255256

257+
parseStringPromise: (str) =>
258+
promisify(@parseString) str
259+
256260
exports.parseString = (str, a, b) ->
257261
# let's determine what we got as arguments
258262
if b?
@@ -270,3 +274,10 @@ exports.parseString = (str, a, b) ->
270274
# the rest is super-easy
271275
parser = new exports.Parser options
272276
parser.parseString str, cb
277+
278+
exports.parseStringPromise = (str, a) ->
279+
if typeof a == 'object'
280+
options = a
281+
282+
parser = new exports.Parser options
283+
parser.parseStringPromise str

src/xml2js.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ exports.Builder = builder.Builder
1818
exports.Parser = parser.Parser
1919

2020
exports.parseString = parser.parseString
21+
exports.parseStringPromise = parser.parseStringPromise

test/parser.test.coffee

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ util = require 'util'
55
assert = require 'assert'
66
path = require 'path'
77
os = require 'os'
8+
promisify = require 'util.promisify'
89

910
fileName = path.join __dirname, '/fixtures/sample.xml'
1011

12+
readFilePromise = promisify fs.readFile
13+
1114
skeleton = (options, checks) ->
1215
(test) ->
1316
xmlString = options?.__xmlString
@@ -574,3 +577,52 @@ module.exports =
574577
console.log 'Result object: ' + util.inspect r, false, 10
575578
equ r.hasOwnProperty('SAMP'), true
576579
equ r.SAMP.hasOwnProperty('TAGN'), true)
580+
581+
582+
'test parseStringPromise parsing': (test) ->
583+
x2js = new xml2js.Parser()
584+
readFilePromise(fileName).then (data) ->
585+
x2js.parseStringPromise data
586+
.then (r) ->
587+
# just a single test to check whether we parsed anything
588+
equ r.sample.chartest[0]._, 'Character data here!'
589+
test.finish()
590+
.catch (err) ->
591+
test.fail('Should not error')
592+
593+
'test parseStringPromise with bad input': (test) ->
594+
x2js = new xml2js.Parser()
595+
x2js.parseStringPromise("< a moose bit my sister>").then (r) ->
596+
test.fail('Should fail')
597+
.catch (err) ->
598+
assert.notEqual err, null
599+
test.finish()
600+
601+
'test global parseStringPromise parsing': (test) ->
602+
readFilePromise(fileName).then (data) ->
603+
xml2js.parseStringPromise data
604+
.then (r) ->
605+
assert.notEqual r, null
606+
equ r.sample.listtest[0].item[0].subitem[0], 'Foo(1)'
607+
test.finish()
608+
.catch (err) ->
609+
test.fail('Should not error')
610+
611+
'test global parseStringPromise with options': (test) ->
612+
readFilePromise(fileName).then (data) ->
613+
xml2js.parseStringPromise data,
614+
trim: true
615+
normalize: true
616+
.then (r) ->
617+
assert.notEqual r, null
618+
equ r.sample.whitespacetest[0]._, 'Line One Line Two'
619+
test.finish()
620+
.catch (err) ->
621+
test.fail('Should not error')
622+
623+
'test global parseStringPromise with bad input': (test) ->
624+
xml2js.parseStringPromise("< a moose bit my sister>").then (r) ->
625+
test.fail('Should fail')
626+
.catch (err) ->
627+
assert.notEqual err, null
628+
test.finish()

0 commit comments

Comments
 (0)