Skip to content

Commit

Permalink
use standard JavaScript style and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Oct 25, 2015
1 parent 1569ddf commit 27d078c
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 494 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
@@ -0,0 +1,11 @@
# EditorConfig is awesome: http://EditorConfig.org

root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
12 changes: 7 additions & 5 deletions .gitignore
@@ -1,5 +1,7 @@
.lock-wscript
node_modules
build
*.swp
npm-debug.log
build/
node_modules/

!.editorconfig
!.gitignore
!.npmignore
!.travis.yml
8 changes: 8 additions & 0 deletions .npmignore
@@ -0,0 +1,8 @@
test/

.editorconfig
.gitignore
.npmignore
.travis.yml

bench.js
11 changes: 4 additions & 7 deletions .travis.yml
Expand Up @@ -6,13 +6,12 @@ env:
- CXX="g++-4.8"

node_js:
- '0.8'
- '0.10'
- '0.12'
- 'iojs'
- 'iojs-v1'
- 'iojs-v2'
- '4.0'
- '4.1'
- '4.2'
- 'stable'

addons:
apt:
Expand All @@ -22,6 +21,4 @@ addons:
- g++-4.8
- gcc-4.8

before_install:
# npm shipped with Node.js 0.8 doesn't support carret so let's update it
- if [ "$TRAVIS_NODE_VERSION" == "0.8" ]; then npm install -g npm; fi
before_script: npm install -g standard
9 changes: 4 additions & 5 deletions README.md
@@ -1,10 +1,11 @@
# node-expat

[![Build Status](https://travis-ci.org/node-xmpp/node-expat.png)](https://travis-ci.org/node-xmpp/node-expat)
[![build status](https://img.shields.io/travis/node-xmpp/node-expat/master.svg?style=flat-square)](https://travis-ci.org/node-xmpp/node-expat/branches)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com/)

## Motivation

You use [node.js](http://github.com/ry/node) for speed? You process
You use [Node.js](https://nodejs.org) for speed? You process
XML streams? Then you want the fastest XML parser: [libexpat](http://expat.sourceforge.net/)!

## Manual
Expand All @@ -13,10 +14,8 @@ Please see the [node-expat manual](http://node-xmpp.org/doc/expat.html)

## Install

Install node-expat:

```
npm i node-expat
npm install node-expat
```

## Testing
Expand Down
101 changes: 50 additions & 51 deletions bench.js
@@ -1,62 +1,61 @@
'use strict';
'use strict'

var util = require('util');
var node_xml = require("node-xml");
var libxml = require("libxmljs");
var expat = require('./');
var sax = require('sax');
var node_xml = require('node-xml')
var libxml = require('libxmljs')
var expat = require('./')
var sax = require('sax')

function NodeXmlParser() {
var parser = new node_xml.SaxParser(function(cb) { });
this.parse = function(s) {
parser.parseString(s);
};
function NodeXmlParser () { // eslint-disable-line
var parser = new node_xml.SaxParser(function (cb) {})
this.parse = function (s) {
parser.parseString(s)
}
}
function LibXmlJsParser() {
var parser = new libxml.SaxPushParser(function(cb) { });
this.parse = function(s) {
parser.push(s, false);
};
function LibXmlJsParser () { // eslint-disable-line
var parser = new libxml.SaxPushParser(function (cb) {})
this.parse = function (s) {
parser.push(s, false)
}
}
function SaxParser() {
var parser = sax.parser();
this.parse = function(s) {
parser.write(s).close();
}
function SaxParser () { // eslint-disable-line
var parser = sax.parser()
this.parse = function (s) {
parser.write(s).close()
}
}
function ExpatParser() {
var parser = new expat.Parser();
this.parse = function(s) {
parser.parse(s, false);
};
function ExpatParser () {
var parser = new expat.Parser()
this.parse = function (s) {
parser.parse(s, false)
}
}

// var p = new NodeXmlParser();
// var p = new LibXmlJsParser();
// var p = new SaxParser();
var p = new ExpatParser();
p.parse("<r>");
var nEl = 0;
function d() {
p.parse("<foo bar='baz'>quux</foo>");
nEl++;
setTimeout(d, 0);
// var p = new NodeXmlParser()
// var p = new LibXmlJsParser()
// var p = new SaxParser()
var p = new ExpatParser()
p.parse('<r>')
var nEl = 0
function d () {
p.parse("<foo bar='baz'>quux</foo>")
nEl++
setTimeout(d, 0)
}
d();
d()

var its =[];
setInterval(function() {
console.log(nEl + " el/s");
its.push(nEl);
nEl = 0;
}, 1000);
var its = []
setInterval(function () {
console.log(nEl + ' el/s')
its.push(nEl)
nEl = 0
}, 1000)

process.on('SIGINT', function () {
var average = 0;
its.forEach(function (v){
average += v;
});
average /= its.length;
console.log("Average: " + average + " el/s");
process.exit(0);
});
var average = 0
its.forEach(function (v) {
average += v
})
average /= its.length
console.log('Average: ' + average + ' el/s')
process.exit(0)
})
99 changes: 50 additions & 49 deletions lib/node-expat.js
@@ -1,11 +1,10 @@
'use strict'

var EventEmitter = require('events').EventEmitter
var util = require('util')
var expat = require('bindings')('node_expat')
var Stream = require('stream').Stream

var Parser = function(encoding) {
var Parser = function (encoding) {
this.encoding = encoding
this._getNewParser()
this.parser.emit = this.emit.bind(this)
Expand All @@ -16,98 +15,100 @@ var Parser = function(encoding) {
}
util.inherits(Parser, Stream)

Parser.prototype._getNewParser = function() {
this.parser = new expat.Parser(this.encoding)
Parser.prototype._getNewParser = function () {
this.parser = new expat.Parser(this.encoding)
}

Parser.prototype.parse = function(buf, isFinal) {
Parser.prototype.parse = function (buf, isFinal) {
return this.parser.parse(buf, isFinal)
}

Parser.prototype.setEncoding = function(encoding) {
Parser.prototype.setEncoding = function (encoding) {
this.encoding = encoding
return this.parser.setEncoding(this.encoding)
}

Parser.prototype.setUnknownEncoding = function(map, convert) {
Parser.prototype.setUnknownEncoding = function (map, convert) {
return this.parser.setUnknownEncoding(map, convert)
}

Parser.prototype.getError = function() {
Parser.prototype.getError = function () {
return this.parser.getError()
}
Parser.prototype.stop = function() {
Parser.prototype.stop = function () {
return this.parser.stop()
}
Parser.prototype.pause = function() {
Parser.prototype.pause = function () {
return this.stop()
}
Parser.prototype.resume = function() {
Parser.prototype.resume = function () {
return this.parser.resume()
}

Parser.prototype.destroy = function() {
Parser.prototype.destroy = function () {
this.parser.stop()
this.end()
}

Parser.prototype.destroySoon = function() {
Parser.prototype.destroySoon = function () {
this.destroy()
}

Parser.prototype.write = function(data) {
var error, result
try {
result = this.parse(data)
if (!result)
error = this.getError()
} catch (e) {
error = e
}
if (error) {
this.emit('error', error)
this.emit('close')
Parser.prototype.write = function (data) {
var error, result
try {
result = this.parse(data)
if (!result) {
error = this.getError()
}
return result
} catch (e) {
error = e
}
if (error) {
this.emit('error', error)
this.emit('close')
}
return result
}

Parser.prototype.end = function(data) {
var error, result
try {
result = this.parse(data || '', true)
if (!result)
Parser.prototype.end = function (data) {
var error, result
try {
result = this.parse(data || '', true)
if (!result) {
error = this.getError()
} catch (e) {
error = e
}
} catch (e) {
error = e
}

if (!error) {
this.emit('end')
} else {
this.emit('error', error)
}
this.emit('close')
if (!error) {
this.emit('end')
} else {
this.emit('error', error)
}
this.emit('close')
}

Parser.prototype.reset = function() {
return this.parser.reset()
Parser.prototype.reset = function () {
return this.parser.reset()
}
Parser.prototype.getCurrentLineNumber = function() {
return this.parser.getCurrentLineNumber()
Parser.prototype.getCurrentLineNumber = function () {
return this.parser.getCurrentLineNumber()
}
Parser.prototype.getCurrentColumnNumber = function() {
return this.parser.getCurrentColumnNumber()
Parser.prototype.getCurrentColumnNumber = function () {
return this.parser.getCurrentColumnNumber()
}
Parser.prototype.getCurrentByteIndex = function() {
return this.parser.getCurrentByteIndex()
Parser.prototype.getCurrentByteIndex = function () {
return this.parser.getCurrentByteIndex()
}

exports.Parser = Parser

exports.createParser = function(cb) {
exports.createParser = function (cb) {
var parser = new Parser()
if (cb) {
parser.on('startElement', cb)
parser.on('startElement', cb)
}
return parser
}
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -12,7 +12,10 @@
"parsing"
],
"scripts": {
"test": "vows --spec ./test/**/*.js"
"preversion": "npm test",
"lint": "standard",
"unit": "vows --spec ./test/**/*.js",
"test": "npm run unit && npm run lint"
},
"dependencies": {
"bindings": "^1.2.1",
Expand Down

0 comments on commit 27d078c

Please sign in to comment.