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

Commit

Permalink
Browse files Browse the repository at this point in the history
net: make isIP() return 0 on empty input
  • Loading branch information
rsolomo authored and bnoordhuis committed May 14, 2012
1 parent 38542f7 commit d404159
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/net.js
Expand Up @@ -1030,7 +1030,7 @@ Server.prototype.listenFD = function(fd, type) {
// and it does not detect more than one double : in a string.
exports.isIP = function(input) {
if (!input) {
return 4;
return 0;
} else if (/^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/.test(input)) {
var parts = input.split('.');
for (var i = 0; i < parts.length; i++) {
Expand Down
7 changes: 4 additions & 3 deletions test/simple/test-net-isip.js
Expand Up @@ -36,12 +36,13 @@ assert.equal(net.isIP('::1'), 6);
assert.equal(net.isIP('::'), 6);
assert.equal(net.isIP('0000:0000:0000:0000:0000:0000:12345:0000'), 0);
assert.equal(net.isIP('0'), 0);
assert.equal(net.isIP(), 0);
assert.equal(net.isIP(""), 0);

assert.equal(net.isIPv4('127.0.0.1'), true);
assert.equal(net.isIPv4('example.com'), false);
assert.equal(net.isIPv4('2001:252:0:1::2008:6'), false);

assert.equal(net.isIPv6('127.0.0.1'), false);
assert.equal(net.isIPv4('example.com'), false);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);

assert.equal(net.isIPv6('example.com'), false);
assert.equal(net.isIPv6('2001:252:0:1::2008:6'), true);

2 comments on commit d404159

@felixge
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason why we return 0 here instead of false? (other than BC)

@bnoordhuis
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None except that it's what the docs say net.isIP("") returns.

Please sign in to comment.