Skip to content

Commit

Permalink
Add test case for promises and fix one() definition
Browse files Browse the repository at this point in the history
  • Loading branch information
scravy committed May 4, 2020
1 parent 4393acc commit 935e881
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
43 changes: 32 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ lib.networkInterfaces = function () {
};

var _getMacAddress;
var _validIfaceRegExp = '^[a-z0-9]+$';
switch (os.platform()) {

case 'win32':
// windows has long interface names which may contain spaces and dashes
_validIfaceRegExp = '^[a-z0-9 -]+$';
_getMacAddress = require('./lib/windows.js');
break;

Expand All @@ -92,12 +95,14 @@ switch (os.platform()) {

}

var validIfaceRegExp = '^[a-z0-9]+$';
var validIfaceRegExpObj = new RegExp(validIfaceRegExp, 'i');
var validIfaceRegExp = new RegExp(_validIfaceRegExp, 'i');

function getMacAddress(iface, callback) {

if (!validIfaceRegExpObj.test(iface)) {
// some platform specific ways of resolving the mac address pass the name
// of the interface down to some command processor, so check for a well
// formed string here.
if (!validIfaceRegExp.test(iface)) {
callback(new Error([
'invalid iface: \'', iface,
'\' (must conform to reg exp /',
Expand All @@ -124,16 +129,35 @@ function promisify(func) {
});
}

lib.one = function (iface, callback) {
if (!callback && typeof iface !== 'function') {
lib.one = function () {
// one() can be invoked in several ways:
// one() -> Promise<string>
// one(iface: string) -> Promise<string>
// one(iface: string, callback) -> async, yields a string
// one(callback) -> async, yields a string
var iface = null;
var callback = null;
if (arguments.length >= 1) {
if (typeof arguments[0] === 'function') {
callback = arguments[0];
} else if (typeof arguments[0] === 'string') {
iface = arguments[0];
}
if (arguments.length >= 2) {
if (typeof arguments[1] === 'function') {
callback = arguments[1];
}
}
}
if (!callback) {
return promisify(function (callback) {
lib.one(iface, callback);
});
}

if (typeof iface === 'function') {
callback = iface;

if (iface) {
getMacAddress(iface, callback);
} else {
var ifaces = lib.networkInterfaces();
var alleged = [ 'eth0', 'eth1', 'en0', 'en1', 'en2', 'en3', 'en4' ];
iface = Object.keys(ifaces)[0];
Expand All @@ -160,9 +184,6 @@ lib.one = function (iface, callback) {
return ifaces[iface].mac;
}
}
if (typeof callback === 'function') {
getMacAddress(iface, callback);
}
return null;
};

Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ macaddress.all(function (err, all) {
});

console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2));

if (typeof Promise !== 'undefined') {
macaddress.one().then(function (result) {
console.log("Mac address for this host using Promises: %s", result);
});
macaddress.all().then(function (result) {
console.log("all() using promises");
console.log(JSON.stringify(result, null, 2));
});
}

0 comments on commit 935e881

Please sign in to comment.