Skip to content

Commit

Permalink
Merge pull request felixge#82 from janpieper/jshint
Browse files Browse the repository at this point in the history
Use jshint to check code for problems and errors
  • Loading branch information
rmehner committed Oct 24, 2013
2 parents e9857de + b8e1996 commit a9f56a9
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 423 deletions.
7 changes: 7 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"node": true,
"laxbreak": true,
"indent": 2,
"curly": true,
"trailing": true
}
2 changes: 1 addition & 1 deletion examples/save-video-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// or streaming to a ffserver

var arDrone = require('..');
var PaVEParser = require('../lib/video/PaVEParser');
var PaVEParser = require('../lib/video/PaVEParser');
var output = require('fs').createWriteStream('./vid.h264');

var video = arDrone.createClient().getVideoStream();
Expand Down
28 changes: 14 additions & 14 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Client(options) {
this._udpControl = options.udpControl || new Client.UdpControl(options);
this._udpNavdatasStream = options.udpNavdataStream || new Client.UdpNavdataStream(options);
this._pngStream = null;
this._tcpStream = null;
this._tcpVideoStream = null;
this._interval = null;
this._ref = {};
this._pcmd = {};
Expand Down Expand Up @@ -49,46 +49,46 @@ Client.prototype.createPngStream = function() {
};

Client.prototype.getPngStream = function() {
if (this._pngStream == null) {
this._pngStream = this._newPngStream();
if (this._pngStream === null) {
this._pngStream = this._newPngStream();
}
return this._pngStream;
}
};

Client.prototype.getVideoStream = function() {
if (this._tcpVideoStream == null) {
if (this._tcpVideoStream === null) {
this._tcpVideoStream = this._newTcpVideoStream();
}
return this._tcpVideoStream;
}
};

Client.prototype._newTcpVideoStream = function() {
var stream = new Client.TcpVideoStream(this._options);
var callback = function(err) {
if (err !== null) {
console.log('TcpVideoStream error: %s', err.message);
setTimeout(function () {
console.log('Attempting to reconnect to TcpVideoStream...');
stream.connect(callback);
console.log('Attempting to reconnect to TcpVideoStream...');
stream.connect(callback);
}, 1000);
}
}
};

stream.connect(callback);
stream.on('error', callback);
return stream;
}
};

Client.prototype._newPngStream = function() {
var videoStream = this.getVideoStream();
var pngEncoder = new Client.PngEncoder(this._options);

videoStream.on('data', function(data) {
pngEncoder.write(data);
pngEncoder.write(data);
});

return pngEncoder;
}
};

Client.prototype.resume = function() {
// Reset config ACK.
Expand Down Expand Up @@ -231,7 +231,7 @@ Client.prototype.config = function(key, value, callback) {
};

Client.prototype.ctrl = function(controlMode, otherMode) {
this._udpControl.ctrl(controlMode, otherMode);
this._udpControl.ctrl(controlMode, otherMode);
};

Client.prototype.animate = function(animation, duration) {
Expand Down Expand Up @@ -265,7 +265,7 @@ var pcmdOptions = [
['left', 'right'],
['front', 'back'],
['clockwise', 'counterClockwise'],
]
];

pcmdOptions.forEach(function(pair) {
Client.prototype[pair[0]] = function(speed) {
Expand Down
1 change: 1 addition & 0 deletions lib/Repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Repl.prototype.resume = function() {
prompt : 'drone> ',
});

/* jshint evil:true */
this._nodeEval = this._repl.eval;

// @TODO This does not seem to work for animate('yawShake', 2000), need
Expand Down
6 changes: 3 additions & 3 deletions lib/control/AtCommandCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ AtCommandCreator.prototype.raw = function(type, args, blocks, options, callback)
};

AtCommandCreator.prototype.ctrl = function(controlMode, otherMode) {
var args = [controlMode, otherMode];
return this.raw('CTRL', args);
var args = [controlMode, otherMode];
return this.raw('CTRL', args);
};

// Used for fly/land as well as emergency trigger/recover
Expand Down Expand Up @@ -87,7 +87,7 @@ AtCommandCreator.prototype.ctrl = function(a, b) {
// TMP: Used to send FTRIM
AtCommandCreator.prototype.ftrim = function() {
return this.raw('FTRIM');
}
};

AtCommandCreator.prototype.animateLeds = function(name, hz, duration) {
// Default animation
Expand Down
2 changes: 1 addition & 1 deletion lib/control/UdpControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ UdpControl.prototype._sendCommands = function(commands) {
debug(serialized.replace('\r', '|'));
var buffer = new Buffer(serialized);
this._socket.send(buffer, 0, buffer.length, this._port, this._ip);
}
};

UdpControl.prototype._concat = function(commands) {
var self = this;
Expand Down
4 changes: 3 additions & 1 deletion lib/navdata/UdpNavdataStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ UdpNavdataStream.prototype._setTimeout = function() {
};

UdpNavdataStream.prototype._handleMessage = function(buffer) {
var navdata = {};

try {
var navdata = this._parseNavdata(buffer);
navdata = this._parseNavdata(buffer);
} catch (err) {
// avoid 'error' causing an exception when nobody is listening
if (this.listeners('error').length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/navdata/maskingFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports.NAVDATA_NUM_TAGS = Object.keys(constants.options).length;
exports.NAVDATA_OPTION_FULL_MASK = (1<<exports.NAVDATA_NUM_TAGS) - 1;

exports.TAG_TYPE_MASK = function (tagtype) {
return (tagtype == 0) ? 0 : 1 << (tagtype - 1);
return (tagtype === 0) ? 0 : 1 << (tagtype - 1);
};

exports.NAVDATA_OPTION_MASK = function (option) {
Expand Down
4 changes: 3 additions & 1 deletion lib/navdata/parseNavdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var NavdataReader = require('./NavdataReader');
// call `iterator` `n` times, returning an array of the results
var timesMap = function(n, iterator, context) {
var results = [];
for (var i = 0; i < n; i++) results[i] = iterator.call(context, i);
for (var i = 0; i < n; i++) {
results[i] = iterator.call(context, i);
}
return results;
};

Expand Down
4 changes: 2 additions & 2 deletions lib/video/PngSplitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ PngSplitter.prototype.write = function(buffer) {

this._state = 'chunk.data';
break;
case 'chunk.data':
case 'chunk.data':
var chunkSize = this._chunk.length + 4; // 4 bytes = CRC
if (this.remainingBytes() < chunkSize) {
return;
Expand All @@ -60,7 +60,7 @@ PngSplitter.prototype.write = function(buffer) {
break;
}

this._state = 'chunk.header'
this._state = 'chunk.header';
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/video/TcpVideoStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function TcpVideoStream(options) {
options = options || {};

this.readable = true;
this._socket = options.socket || new net.Socket;
this._socket = options.socket || new net.Socket();
this._port = options.port || constants.ports.VIDEO;
this._ip = options.ip || constants.DEFAULT_DRONE_IP;
this._timeout = options.timeout || 1 * 1000;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"main": "./index",
"scripts": {
"pretest": "node_modules/jshint/bin/jshint -c .jshintrc examples/ lib/ test/ index.js",
"test": "node test/run.js"
},
"dependencies": {
Expand All @@ -19,7 +20,8 @@
"devDependencies": {
"utest": "0.0.6",
"urun": "0.0.6",
"sinon": "1.4.2"
"sinon": "1.4.2",
"jshint": "2.3.0"
},
"optionalDependencies": {},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ server.listen(common.TCP_PORT, function() {
var video = new TcpVideoStream({ip: 'localhost', port: common.TCP_PORT, timeout: 100});

video.connect(function(err) {
if (err) throw err;
if (err) { throw err; }

events.push('connectCb');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ server.listen(common.TCP_PORT, function() {
var video = new TcpVideoStream({ip: 'localhost', port: common.TCP_PORT});

video.connect(function(err) {
if (err) throw err;
if (err) { throw err; }

events.push('connectCb');
});
Expand Down
22 changes: 11 additions & 11 deletions test/unit/control/test-AtCommandCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ test('AtCommandCreator', {
assert(!cmd.callback);

var options = { timeout: 10 };
var cmd = this.creator.raw('FOO', [1, 2], true, options);
cmd = this.creator.raw('FOO', [1, 2], true, options);
assert.equal(cmd.type, 'FOO');
assert.deepEqual(cmd.args, [1, 2]);
assert.equal(cmd.blocks, true);
assert.deepEqual(cmd.options, options);
assert(!cmd.callback);

var options = { timeout: 10 };
options = { timeout: 10 };
var callback = function() {};
var cmd = this.creator.raw('FOO', [1, 2], true, options, callback);
cmd = this.creator.raw('FOO', [1, 2], true, options, callback);
assert.equal(cmd.type, 'FOO');
assert.deepEqual(cmd.args, [1, 2]);
assert.equal(cmd.blocks, true);
Expand All @@ -54,12 +54,12 @@ test('AtCommandCreator', {
assert.equal(cmd.blocks, false);
assert.deepEqual(cmd.options, {});

var cmd = this.creator.ref({fly: true});
cmd = this.creator.ref({fly: true});
assert.ok(cmd.args[0] & AtCommandCreator.REF_FLAGS.takeoff);
assert.equal(cmd.blocks, false);
assert.deepEqual(cmd.options, {});

var cmd = this.creator.ref({emergency: true});
cmd = this.creator.ref({emergency: true});
assert.ok(cmd.args[0] & AtCommandCreator.REF_FLAGS.emergency);
assert.equal(cmd.blocks, false);
assert.deepEqual(cmd.options, {});
Expand All @@ -85,16 +85,16 @@ test('AtCommandCreator', {
assert.equal(this.creator.pcmd({counterClockwise: val}).args[4], at.floatString(-val));

// test multiple aliases togeter
var cmd = this.creator.pcmd({left: 0.1, clockwise: 0.3});
cmd = this.creator.pcmd({left: 0.1, clockwise: 0.3});
assert.equal(cmd.args[1], at.floatString(-0.1));
assert.equal(cmd.args[4], at.floatString(0.3));

// test progressive bit being unset when no aliases are provided
var cmd = this.creator.pcmd();
cmd = this.creator.pcmd();
assert.equal(cmd.args[0] & AtCommandCreator.PCMD_FLAGS.progressive, false);

// test progressive bit being set automatically
var cmd = this.creator.pcmd({left: 0.1});
cmd = this.creator.pcmd({left: 0.1});
assert.ok(cmd.args[0] & (1 << 0));
},

Expand Down Expand Up @@ -122,7 +122,7 @@ test('AtCommandCreator', {
assert.deepEqual(cmd.options, {});

var callback = function() {};
var cmd = this.creator.config('foo', 'bar', callback);
cmd = this.creator.config('foo', 'bar', callback);
assert.equal(cmd.type, 'CONFIG');
assert.equal(cmd.args.length, 2);
assert.equal(cmd.args[0], '"foo"');
Expand All @@ -131,14 +131,14 @@ test('AtCommandCreator', {
assert.deepEqual(cmd.options, {});
assert.equal(cmd.callback, callback);

var cmd = this.creator.config({key: 'foo', value: 'bar'});
cmd = this.creator.config({key: 'foo', value: 'bar'});
assert.equal(cmd.type, 'CONFIG');
assert.equal(cmd.args.length, 2);
assert.equal(cmd.args[0], '"foo"');
assert.equal(cmd.args[1], '"bar"');
assert.equal(cmd.blocks, true);

var cmd = this.creator.config({key: 'foo', value: 'bar', timeout: 1}, callback);
cmd = this.creator.config({key: 'foo', value: 'bar', timeout: 1}, callback);
assert.equal(cmd.type, 'CONFIG');
assert.equal(cmd.args.length, 2);
assert.equal(cmd.args[0], '"foo"');
Expand Down
40 changes: 20 additions & 20 deletions test/unit/control/test-UdpControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ test('UdpControl', {


// Should have sent a single non-blocking command second.
var sendArgs = fakeSocket.send.getCall(1).args;
var buffer = sendArgs.shift();
var offset = sendArgs.shift();
var length = sendArgs.shift();
var port = sendArgs.shift();
var ip = sendArgs.shift();
sendArgs = fakeSocket.send.getCall(1).args;
buffer = sendArgs.shift();
offset = sendArgs.shift();
length = sendArgs.shift();
port = sendArgs.shift();
ip = sendArgs.shift();

assert.equal(Buffer.isBuffer(buffer), true);
assert.deepEqual(buffer.toString(), config1.serialize(2));
Expand All @@ -158,13 +158,13 @@ test('UdpControl', {
control.ack();
control.flush();

var ctrl = new AtCommand('CTRL', [5, 0]);
var sendArgs = fakeSocket.send.getCall(2).args;
var buffer = sendArgs.shift();
var offset = sendArgs.shift();
var length = sendArgs.shift();
var port = sendArgs.shift();
var ip = sendArgs.shift();
ctrl = new AtCommand('CTRL', [5, 0]);
sendArgs = fakeSocket.send.getCall(2).args;
buffer = sendArgs.shift();
offset = sendArgs.shift();
length = sendArgs.shift();
port = sendArgs.shift();
ip = sendArgs.shift();

assert.equal(Buffer.isBuffer(buffer), true);
assert.deepEqual(buffer.toString(), ctrl.serialize(3));
Expand All @@ -178,12 +178,12 @@ test('UdpControl', {
control.ackReset();
control.flush();

var sendArgs = fakeSocket.send.getCall(3).args;
var buffer = sendArgs.shift();
var offset = sendArgs.shift();
var length = sendArgs.shift();
var port = sendArgs.shift();
var ip = sendArgs.shift();
sendArgs = fakeSocket.send.getCall(3).args;
buffer = sendArgs.shift();
offset = sendArgs.shift();
length = sendArgs.shift();
port = sendArgs.shift();
ip = sendArgs.shift();

assert.equal(Buffer.isBuffer(buffer), true);
assert.deepEqual(buffer.toString(), config2.serialize(4));
Expand All @@ -208,5 +208,5 @@ test('UdpControl', {

control.close();
assert.equal(fakeSocket.close.callCount, 1);
},
}
});
Loading

0 comments on commit a9f56a9

Please sign in to comment.