Skip to content

Commit a114e80

Browse files
committed
Another round of cleanup/refactor. Tests should pass now
1 parent 20ea870 commit a114e80

3 files changed

Lines changed: 33 additions & 49 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
3-
- "4.1"
4-
- "0.10"
3+
- "5.5"
4+
- "4.2"

lib/lirc_node.js

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,70 +29,55 @@ exports.init = function() {
2929
});
3030
};
3131

32-
// Private
33-
exports._populateRemotes = function (result) {
34-
console.log("_populateRemotes", result);
32+
// Parse the list of remotes that irsend knows about
33+
exports._populateRemotes = function (irsendResult) {
3534
var remotes = {};
3635

37-
// list comes back with newlines, so split into an array
38-
var results = result[1].split('\n');
39-
40-
results.forEach(function (remote) {
36+
irsendResult[1].split('\n').forEach(function (remote) {
4137
var remoteName = remote.match(/\s(.*)$/);
4238
if (remoteName) remotes[remoteName[1]] = [];
4339
});
4440

45-
console.log("_populateRemotes", remotes);
4641
return remotes;
4742
};
4843

49-
exports._populateCommands = function (remotes) {
50-
console.log("_populateCommands", remotes);
51-
44+
// Given object whose keys represent remotes, get commands for each remote
45+
// Returns promise that will resolve when all irsend invocations complete
46+
exports._populateCommands = function (remotesObject) {
5247
var commandPromises = [];
53-
var remotes = Object.keys(remotes);
48+
var remoteNames = Object.keys(remotesObject);
5449

55-
// Get the list of all commands for each remote
56-
remotes.forEach(function (remote) {
50+
remoteNames.forEach(function (remote) {
5751
commandPromises.push(exports.irsend.list(remote, '')
58-
.then(function (result) {
59-
var ret = {};
60-
ret[remote] = exports._parseCommands(result);
61-
return ret;
52+
.then(function (irsendResult) {
53+
return { [remote]: exports._parseCommands(irsendResult) }
6254
})
6355
);
6456
})
6557

66-
// commandPromises should look like this:
67-
// [{ TV: ['Power', 'VolUp', 'VolDown'] }, { Yamaha: ['Power', 'Xbox360'] }]
68-
//
69-
// Final response should look like this:
70-
// { TV: ['Power', 'VolUp', 'VolDown'], Yamaha: ['Power', 'Xbox360'] }
7158
return Promise.all(commandPromises)
72-
.then(function(commands) {
73-
var ret = {};
59+
.then(exports._joinRemoteCommands);
60+
};
7461

75-
commands.forEach(function(command) {
76-
ret = Object.assign(ret, command);
77-
});
62+
// Merges an array of remoteCommands into a single object
63+
exports._joinRemoteCommands = function(remoteCommands) {
64+
var remotes = {};
7865

79-
console.log("_populateCommands", ret);
80-
return ret;
66+
remoteCommands.forEach(function(remote) {
67+
remotes = Object.assign(remotes, remote);
8168
});
69+
70+
return remotes;
8271
};
8372

84-
exports._parseCommands = function (result) {
85-
console.log("_parseCommands", result);
73+
// Parses the results of irsend for a specifif remote
74+
exports._parseCommands = function (irsendResult) {
8675
var commands = [];
8776

88-
// Change multiline string into array to parse
89-
var results = result[1].split('\n');
90-
91-
results.forEach(function (command) {
77+
irsendResult[1].split('\n').forEach(function (command) {
9278
var commandName = command.match(/\s.*\s(.*)$/);
9379
if (commandName && commandName[1]) commands.push(commandName[1]);
9480
});
9581

96-
console.log("_parseCommands", commands);
9782
return commands;
9883
};

test/lirc_node.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
var lirc_node = require('../'),
1+
var lircNode = require('../'),
22
assert = require('assert'),
33
sinon = require('sinon'),
44
q = require('q');
55

6-
describe('lirc_node', function() {
6+
describe('lircNode', function() {
77
describe('init', function() {
88
it('should exist', function() {
9-
assert(lirc_node.init instanceof Function);
9+
assert(lircNode.init instanceof Function);
1010
});
1111
});
1212

1313
describe("Setting an internal list of Remotes", function() {
1414
describe('#_populateRemotes', function() {
1515
beforeEach(function() {
16-
lirc_node.remotes = {};
16+
lircNode.remotes = {};
1717
});
1818

1919
it('should exist', function() {
20-
assert(lirc_node._populateRemotes instanceof Function);
20+
assert(lircNode._populateRemotes instanceof Function);
2121
});
2222

2323
it('should return an object of remote names', function () {
2424
var remotes = lircNode._populateRemotes(['', '']);
25-
console.log("remotes:", remotes);
2625
assert(remotes instanceof Object);
2726
});
2827

@@ -47,16 +46,16 @@ describe('lirc_node', function() {
4746

4847
describe('#_populateCommands', function() {
4948
beforeEach(function() {
50-
sinon.stub(lirc_node.irsend, 'list', function() {});
49+
sinon.stub(lircNode.irsend, 'list', function() {});
5150
});
5251

5352
afterEach(function() {
54-
lirc_node.remotes = {};
55-
lirc_node.irsend.list.restore();
53+
lircNode.remotes = {};
54+
lircNode.irsend.list.restore();
5655
});
5756

5857
it('should exist', function() {
59-
assert(lirc_node._populateCommands instanceof Function);
58+
assert(lircNode._populateCommands instanceof Function);
6059
});
6160
});
6261

0 commit comments

Comments
 (0)