Skip to content

Commit

Permalink
Merge pull request #2 from eiriksm/use-tap-and-coverage-report
Browse files Browse the repository at this point in the history
Re-factor a bit and report coverage.
  • Loading branch information
eiriksm committed Aug 3, 2017
2 parents 0e2b0e9 + cc90f23 commit 360d943
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_js:
- 6
- 8
- node
after_script:
- npm run test-cov-report
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test:
node test/test.js
npm test

.PHONY: test
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,33 @@ Get local ip address in node.
Well, for different kinds of reasons you might want to know what your local
ip address is so you can broadcast it to different services.

# API

## localip(interface, [callback])

__interface__ should be a network interface, such as wlan0.
__callback__ is optional, and will be invoked with the signature `callback(err, res)` if specified. If no callback is specified, the value will be returned directly, or an Error will be thrown in case of an error.

# Example
```js
var localip = require('local-ip');
var interface = 'wlan0';
var iface = 'wlan0';

localip(interface, function(err, res) {
localip(iface, function(err, res) {
if (err) {
throw new Error('I have no idea what my local ip is.');
}
console.log('My local ip address on ' + interface + ' is ' + res);
console.log('My local ip address on ' + iface + ' is ' + res);
});
```

# "Docs"
Pretty self-explanatory. It is basically just a wrapper around
`require('os').networkInterfaces`
You can also use it in sync mode:

## localip(interface, callback)
Gets local ip and invokes callback with `(err, res)` as you might be used to
in other modules in node.
```
var iface = 'wlan0';
var localip = require('local-ip')(interface);
console.log('My local ip address on ' + iface + ' is ' + localip);
```

# License
MIT
53 changes: 27 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
;(function() {
'use strict';
var os = require('os');
var util = require('util');
var ifaces = os.networkInterfaces();
'use strict';
var os = require('os');
var util = require('util');
var ifaces = os.networkInterfaces();

var getLocalIp = function(iface, callback) {
var returnVal;
for (var dev in ifaces) {
if (ifaces.hasOwnProperty(dev)) {
// If the user does not specify anything, last value will be returned.
if (iface && dev !== iface) {
continue;
}
for (var i = 0, len = ifaces[dev].length; i < len; i++) {
var details = ifaces[dev][i];
if (details.family === 'IPv4') {
returnVal = details.address;
}
}
var getLocalIp = function(iface, callback) {
var returnVal;
Object.keys(ifaces).forEach(function(dev) {
// If the user does not specify anything, last value will be returned.
if (iface && dev !== iface) {
return;
}
for (var i = 0, len = ifaces[dev].length; i < len; i++) {
var details = ifaces[dev][i];
if (details.family === 'IPv4') {
returnVal = details.address;
}
}
});
var error = util.format('No address found on %s', iface);
if (!callback) {
if (!returnVal) {
callback(new Error(util.format('No address found on %s', iface)));
return;
throw new Error(error);
}
callback(null, returnVal);
};
module.exports = getLocalIp;
})();

return returnVal;
}
if (!returnVal) {
return callback(new Error(error));
}
return callback(null, returnVal);
};
module.exports = getLocalIp;
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "Get local ip address in node.",
"main": "index.js",
"scripts": {
"test": "make test"
"test": "tap test/*",
"test-cov": "tap test/* --coverage --coverage-report=text-lcov > lcov.info",
"test-cov-report": "npm run test-cov && cat lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"repository": {
"type": "git",
Expand All @@ -20,6 +22,8 @@
},
"homepage": "https://github.com/eiriksm/node-local-ip",
"devDependencies": {
"tape": "~2.14.0"
"coveralls": "^2.13.1",
"tap": "^10.7.1",
"tape": "^4.8.0"
}
}
8 changes: 5 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ var localip = require('..');
var test = require('tape');

test('Get local ip', function(t) {
t.plan(4);
t.plan(6);
t.equal(typeof(localip), 'function', 'localip is exposed as function');
localip(undefined, function(err, res) {
t.equal(typeof(res), 'string', 'localip invoked callback with value');
t.equal(typeof(res), 'string', 'localip invoked callback with value');
});
localip('garbagestringthatisnotinterface', function(err, res) {
t.equal(typeof(err), 'object', 'Callback invoked with error value on bogus interface');
t.equal(err.toString(), 'Error: No address found on garbagestringthatisnotinterface', 'Callback invoked with error value on bogus interface');
t.equal(res, undefined, 'Callback invoked with no result value on bogus interface');
});
t.equal(typeof(localip(undefined)), 'string', 'localip invoked callback with value');
t.throws(localip.bind(null, 'garbagestringthatisnotinterface'), 'No address found on garbagestringthatisnotinterface');
});

0 comments on commit 360d943

Please sign in to comment.