Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added unit test and fix in the constructor making the test pass

  • Loading branch information...
commit 05c7f43750204de7626b9f8142624e0df62986f0 1 parent 87003ae
Marek Kubica authored
Showing with 16 additions and 0 deletions.
  1. +3 −0  lib/xml2js.js
  2. +2 −0  src/xml2js.coffee
  3. +11 −0 test/xml2js.test.coffee
3  lib/xml2js.js
View
@@ -72,6 +72,9 @@
this.parseString = __bind(this.parseString, this);
this.reset = __bind(this.reset, this);
var key, value, _ref;
+ if (!(this instanceof exports.Parser)) {
+ return new exports.Parser(opts);
+ }
this.options = {};
_ref = exports.defaults["0.2"];
for (key in _ref) {
2  src/xml2js.coffee
View
@@ -61,6 +61,8 @@ class exports.ValidationError extends Error
class exports.Parser extends events.EventEmitter
constructor: (opts) ->
+ # if this was called without 'new', create an instance with new and return
+ return new exports.Parser opts unless @ instanceof exports.Parser
# copy this versions default options
@options = {}
@options[key] = value for own key, value of exports.defaults["0.2"]
11 test/xml2js.test.coffee
View
@@ -308,3 +308,14 @@ module.exports =
xml2js.parseString html, strict: false, (err, parsed) ->
equ err, null
test.finish()
+
+ 'test construction with new and without': (test) ->
+ demo = '<xml><foo>Bar</foo></xml>'
+ withNew = new xml2js.Parser
+ withNew.parseString demo, (err, resWithNew) ->
+ equ err, undefined
+ withoutNew = xml2js.Parser()
+ withoutNew.parseString demo, (err, resWithoutNew) ->
+ equ err, undefined
+ assert.deepEqual resWithNew, resWithoutNew
+ test.done()
Please sign in to comment.
Something went wrong with that request. Please try again.