Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Update vid/pid check in usbDetect.find(...) to check for function callback #74

Merged
merged 6 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = [];
Expand Down
27 changes: 25 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ var getSetTimeoutPromise = require('./lib/set-timeout-promise-helper');
var usbDetect = require('../');

const MANUAL_INTERACTION_TIMEOUT = 10000;
const USB_TEST_VID = process.env.TEST_VID || 5824; // VID of the tevice used to test
const USB_TEST_PID = process.env.TEST_PID || 1155;

// We just look at the keys of this device object
var DEVICE_OBJECT_FIXTURE = {
locationId: 0,
vendorId: 5824,
productId: 1155,
vendorId: USB_TEST_VID,
productId: USB_TEST_PID,
deviceName: 'Teensy USB Serial (COM3)',
manufacturer: 'PJRC.COM, LLC.',
serialNumber: '',
Expand Down Expand Up @@ -63,6 +65,27 @@ 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(devices) {
testArrayOfDevicesShape(devices);
})
.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(devices) {
testArrayOfDevicesShape(devices);
})
.then(done)
.catch(done.fail);
});


it('should return a promise', function(done) {
usbDetect.find()
.then(function(devices) {
Expand Down