Browse Source

refactored isIP method

pull/15/head
Prabhanjan 6 years ago
parent
commit
0221be02ec
  1. 21
      utils/isIP.js

21
utils/isIP.js

@ -3,25 +3,8 @@
// Parses a string, and returns true if it is an IP Address.
function isIP(str) {
const segments = str
.split(".")
.map(Number);
if (!segments.length === 4) {
return false;
}
for(let i = 0; i < segments.length; i++) {
const segment = segments[i];
if (Number.isNaN(segment)) {
return false;
}
if (segment < 1 || segment > 255) {
return false;
}
}
if (segments[3] > 254) {
return false;
}
return true;
const IPv4 = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
return IPv4.test(str);
}
module.exports = isIP;

Loading…
Cancel
Save