diff --git a/index.js b/index.js index 6e9dcaf..acca685 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,10 @@ var index = require('./package.json'); +function isFunction(functionToCheck) { + return typeof functionToCheck === 'function'; +} + if(global[index.name] && global[index.name].version === index.version) { module.exports = global[index.name]; } else { @@ -18,16 +22,14 @@ if(global[index.name] && global[index.name].version === index.version) { //detector.find = detection.find; detector.find = function(vid, pid, callback) { // Suss out the optional parameters - if(!pid && !callback) { + if(isFunction(vid) && !pid && !callback) { callback = vid; vid = undefined; - } - else if(!callback) { + } else if(isFunction(pid) && !callback) { callback = pid; pid = undefined; } - return new Promise(function(resolve, reject) { // Assemble the optional args into something we can use with `apply` var args = []; diff --git a/test/test.js b/test/test.js index 8a1ad6d..faf45f3 100644 --- a/test/test.js +++ b/test/test.js @@ -63,6 +63,33 @@ describe('usb-detection', function() { }); }); + it('should return a promise when vid and pid are given', async function(done) { + const devices = await usbDetect.find(); + usbDetect.find(devices[0].vendorId, devices[0].productId) + .then(function(devicesFromTestedFunction) { + testArrayOfDevicesShape(devicesFromTestedFunction); + expect(devicesFromTestedFunction.length).to.be.greaterThan(0); + // Should find a subset of USB devices. We assume you have many USB devices connected + expect(devicesFromTestedFunction.length).to.be.lessThan(devices.length); + }) + .then(done) + .catch(done.fail); + }); + + it('should return a promise when vid is given', async function(done) { + const devices = await usbDetect.find(); + usbDetect.find(devices[0].vendorId) + .then(function(devicesFromTestedFunction) { + testArrayOfDevicesShape(devicesFromTestedFunction); + expect(devicesFromTestedFunction.length).to.be.greaterThan(0); + // Should find a subset of USB devices. We assume you have many USB devices connected + expect(devicesFromTestedFunction.length).to.be.lessThan(devices.length); + }) + .then(done) + .catch(done.fail); + }); + + it('should return a promise', function(done) { usbDetect.find() .then(function(devices) {