From 0221be02ec72fbd4b9614475cd4cc45b497eba4a Mon Sep 17 00:00:00 2001 From: Prabhanjan Date: Sun, 7 Oct 2018 15:04:32 +0530 Subject: [PATCH] refactored isIP method --- utils/isIP.js | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/utils/isIP.js b/utils/isIP.js index 864d647..bcacc64 100644 --- a/utils/isIP.js +++ b/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;