Skip to content

Commit 935e881

Browse files
committed
Add test case for promises and fix one() definition
1 parent 4393acc commit 935e881

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

index.js

+32-11
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,12 @@ lib.networkInterfaces = function () {
6969
};
7070

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

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

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

9396
}
9497

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

98100
function getMacAddress(iface, callback) {
99101

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

127-
lib.one = function (iface, callback) {
128-
if (!callback && typeof iface !== 'function') {
132+
lib.one = function () {
133+
// one() can be invoked in several ways:
134+
// one() -> Promise<string>
135+
// one(iface: string) -> Promise<string>
136+
// one(iface: string, callback) -> async, yields a string
137+
// one(callback) -> async, yields a string
138+
var iface = null;
139+
var callback = null;
140+
if (arguments.length >= 1) {
141+
if (typeof arguments[0] === 'function') {
142+
callback = arguments[0];
143+
} else if (typeof arguments[0] === 'string') {
144+
iface = arguments[0];
145+
}
146+
if (arguments.length >= 2) {
147+
if (typeof arguments[1] === 'function') {
148+
callback = arguments[1];
149+
}
150+
}
151+
}
152+
if (!callback) {
129153
return promisify(function (callback) {
130154
lib.one(iface, callback);
131155
});
132156
}
133157

134-
if (typeof iface === 'function') {
135-
callback = iface;
136-
158+
if (iface) {
159+
getMacAddress(iface, callback);
160+
} else {
137161
var ifaces = lib.networkInterfaces();
138162
var alleged = [ 'eth0', 'eth1', 'en0', 'en1', 'en2', 'en3', 'en4' ];
139163
iface = Object.keys(ifaces)[0];
@@ -160,9 +184,6 @@ lib.one = function (iface, callback) {
160184
return ifaces[iface].mac;
161185
}
162186
}
163-
if (typeof callback === 'function') {
164-
getMacAddress(iface, callback);
165-
}
166187
return null;
167188
};
168189

test.js

+10
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,13 @@ macaddress.all(function (err, all) {
1919
});
2020

2121
console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2));
22+
23+
if (typeof Promise !== 'undefined') {
24+
macaddress.one().then(function (result) {
25+
console.log("Mac address for this host using Promises: %s", result);
26+
});
27+
macaddress.all().then(function (result) {
28+
console.log("all() using promises");
29+
console.log(JSON.stringify(result, null, 2));
30+
});
31+
}

0 commit comments

Comments
 (0)