Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding parseStringSync + simple tests #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jtdevos
Copy link

@jtdevos jtdevos commented Oct 2, 2015

(note: the history of this PR goes into more detail here: #159 (comment))

Though the async parseString() method is likely the preferred way to parse an xml file, I had need for an async version so that I could use it inside a karma.config.js file (which cannot use async calls).

My understanding is that sax.js uses event-emitter, which is a blocking api. If that's the case(and let me know if I'm wrong), I believe that it's safe to assume that sax events fire events synchronously when you call write() with a string argument instead of a buffer.

Based on this understanding I wrote parseStringSync() which is little more than a wrapper around parseString. Instead of utilizing a callback it accepts a string argument and returns the parsed result. If the underlying call results in an error, parseStringSync throws that error. I also wrote a couple of (simple) tests to assert the sync behavior works as I hoped.

Unfortunately it is my first attempt at writing coffeescript, so I'm sure there's a more elegant way to pull this off than my patch. Feel free to hack it to pieces or reject it outright :-)

@@ -156,11 +156,30 @@ module.exports =
equ r.sample.$$[10].$$[2]._, '3'
equ r.sample.$$[10].$$[3]['#name'], 'one'
equ r.sample.$$[10].$$[3]._, '4'
equ r.sample.$$[10].$$[4]['#name'], 'two'
equ r.sample.$$[10].$$[4]['#name'], 'two'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bonus space?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. I must have fat-fingered that line when I was copy-pasting it into a new test. I cut my finger earlier this week, so I'm going to blame the annoying bandage on my finger.

My apologies.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries, can always pass in a quick --amend commit

@Leonidas-from-XIV
Copy link
Owner

Thanks for the PR! Hmm, good point, though it's quite disappointing that event-emitter is blocking. I plan to review it soon.

@Leonidas-from-XIV Leonidas-from-XIV self-assigned this Oct 4, 2015
@@ -402,6 +402,23 @@ class exports.Parser extends events.EventEmitter
else if @saxParser.ended
throw err

parseStringSync: (str) =>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the API adhere to the one of the regular parseString and also add the shorthand alias exports.parseStringSync?

@Leonidas-from-XIV
Copy link
Owner

Unfortunately it is my first attempt at writing coffeescript, so I'm sure there's a more elegant way to pull this off than my patch.

No worries, looks mostly alright to me 😃

@mrparkers
Copy link

Is there anything in particular that's preventing this from being accepted? I'd be happy to help where it's needed - I'd really like this functionality for a project that I'm working on.

@Leonidas-from-XIV
Copy link
Owner

@mrparkers None of my comments were addressed, so that's the most blocking issue at the moment.

@mrparkers
Copy link

Okay. I've never used CoffeeScript before but I can try to take a stab at your comments.

@Leonidas-from-XIV
Copy link
Owner

Overall I do plan to migrate away from CoffeeScript but while at it I'd like to do some serious re-engineering so that might take quite a while still.

mckramer added a commit to mckramer/node-xml2js that referenced this pull request Feb 13, 2018
Create a sync version of parseString to API to permit
calling without a callback.

The current implemenation is sync due to underlying
implementation of SAX parser.

Examples:

```js
// Via root API
var result = xml2js.parseStringSync('< ... >', options)

// Via parser
var parser = new xml2js.Parser(options);
var result = parser.parseStringSync('< ... >');
```

See Leonidas-from-XIV#241 by @nobodyname
See Leonidas-from-XIV#319 by @mrparkers
mckramer added a commit to mckramer/node-xml2js that referenced this pull request Feb 13, 2018
Create a sync version of parseString to API to permit
calling without a callback.

The current implemenation is sync due to underlying
implementation of SAX parser.

Examples:

```js
// Via root API
var result = xml2js.parseStringSync('< ... >', options)

// Via parser
var parser = new xml2js.Parser(options);
var result = parser.parseStringSync('< ... >');
```

See Leonidas-from-XIV#241 by @nobodyman
See Leonidas-from-XIV#319 by @mrparkers
@mckramer mckramer mentioned this pull request Feb 13, 2018
mckramer added a commit to mckramer/node-xml2js that referenced this pull request Mar 12, 2018
Create a sync version of parseString to API to permit
calling without a callback.

The current implemenation is sync due to underlying
implementation of SAX parser.

Examples:

```js
// Via root API
var result = xml2js.parseStringSync('< ... >', options)

// Via parser
var parser = new xml2js.Parser(options);
var result = parser.parseStringSync('< ... >');
```

See Leonidas-from-XIV#241 by @nobodyman
See Leonidas-from-XIV#319 by @mrparkers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants