From 28206602649a0dd09561b91e622c6a77517bf2e4 Mon Sep 17 00:00:00 2001 From: Klaxon Date: Tue, 28 Aug 2018 20:15:59 +1000 Subject: [PATCH] fix regex issues and ESlint errors and warnings fix comment fix ESlint errors and warnings fix regex add author --- src/core/lib/IP.mjs | 53 +++++++++++++++----------- src/core/operations/ParseIPRange.mjs | 4 +- test/tests/operations/ParseIPRange.mjs | 24 ++++++------ 3 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/core/lib/IP.mjs b/src/core/lib/IP.mjs index bd1baa68..cfefbd11 100644 --- a/src/core/lib/IP.mjs +++ b/src/core/lib/IP.mjs @@ -3,6 +3,7 @@ * * @author picapi * @author n1474335 [n1474335@gmail.com] + * @author Klaxon [klaxon@veyr.com] * @copyright Crown Copyright 2016 * @license Apache-2.0 */ @@ -110,7 +111,7 @@ export function ipv6CidrRange(cidr, includeNetworkInfo) { */ export function ipv4HyphenatedRange(range, includeNetworkInfo, enumerateAddresses, allowLargeList) { const ip1 = strToIpv4(range[0].split("-")[0].trim()), - ip2 = strToIpv4(range[0].split("-")[1].trim()); + ip2 = strToIpv4(range[0].split("-")[1].trim()); let output = ""; @@ -200,27 +201,29 @@ export function ipv6HyphenatedRange(range, includeNetworkInfo) { */ export function ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList) { - var ipv4List = match[0].split("\n"); + let ipv4List = match[0].split("\n"); ipv4List = ipv4List.filter(Boolean); - var ipv4CidrList = ipv4List.filter( function(a) { return a.includes("/")}); + const ipv4CidrList = ipv4List.filter(function(a) { + return a.includes("/"); + }); for (let i = 0; i < ipv4CidrList.length; i++) { - let network = strToIpv4(ipv4CidrList[i].split("/")[0]); - let cidrRange = parseInt(ipv4CidrList[i].split("/")[1]); + const network = strToIpv4(ipv4CidrList[i].split("/")[0]); + const cidrRange = parseInt(ipv4CidrList[i].split("/")[1], 10); if (cidrRange < 0 || cidrRange > 31) { return "IPv4 CIDR must be less than 32"; } - let mask = ~(0xFFFFFFFF >>> cidrRange), + const mask = ~(0xFFFFFFFF >>> cidrRange), cidrIp1 = network & mask, cidrIp2 = cidrIp1 | ~mask; - ipv4List.splice(ipv4List.indexOf(ipv4CidrList[i]),1); + ipv4List.splice(ipv4List.indexOf(ipv4CidrList[i]), 1); ipv4List.push(ipv4ToStr(cidrIp1), ipv4ToStr(cidrIp2)); } ipv4List = ipv4List.sort(ipv4Compare); - let ip1 = ipv4List[0]; - let ip2 = ipv4List[ipv4List.length - 1]; - let range = [ip1 + " - " + ip2] + const ip1 = ipv4List[0]; + const ip2 = ipv4List[ipv4List.length - 1]; + const range = [ip1 + " - " + ip2]; return ipv4HyphenatedRange(range, includeNetworkInfo, enumerateAddresses, allowLargeList); } @@ -234,38 +237,42 @@ export function ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, a */ export function ipv6ListedRange(match, includeNetworkInfo) { - var ipv6List = match[0].split("\n"); - ipv6List = ipv6List.filter(function(str) {return str.trim();}); + let ipv6List = match[0].split("\n"); + ipv6List = ipv6List.filter(function(str) { + return str.trim(); + }); for (let i =0; i < ipv6List.length; i++){ ipv6List[i] = ipv6List[i].trim(); } - var ipv6CidrList = ipv6List.filter( function(a) { return a.includes("/")}); + const ipv6CidrList = ipv6List.filter(function(a) { + return a.includes("/"); + }); for (let i = 0; i < ipv6CidrList.length; i++) { - let network = strToIpv6(ipv6CidrList[i].split("/")[0]); - let cidrRange = parseInt(ipv6CidrList[i].split("/")[1]); + const network = strToIpv6(ipv6CidrList[i].split("/")[0]); + const cidrRange = parseInt(ipv6CidrList[i].split("/")[1], 10); if (cidrRange < 0 || cidrRange > 127) { return "IPv6 CIDR must be less than 128"; } - let cidrIp1 = new Array(8), + const cidrIp1 = new Array(8), cidrIp2 = new Array(8); - let mask = genIpv6Mask(cidrRange); + const mask = genIpv6Mask(cidrRange); for (let j = 0; j < 8; j++) { cidrIp1[j] = network[j] & mask[j]; cidrIp2[j] = cidrIp1[j] | (~mask[j] & 0x0000FFFF); } - ipv6List.splice(ipv6List.indexOf(ipv6CidrList[i]),1); + ipv6List.splice(ipv6List.indexOf(ipv6CidrList[i]), 1); ipv6List.push(ipv6ToStr(cidrIp1), ipv6ToStr(cidrIp2)); } ipv6List = ipv6List.sort(ipv6Compare); - let ip1 = ipv6List[0]; - let ip2 = ipv6List[ipv6List.length - 1]; - let range = [ip1 + " - " + ip2] + const ip1 = ipv6List[0]; + const ip2 = ipv6List[ipv6List.length - 1]; + const range = [ip1 + " - " + ip2]; return ipv6HyphenatedRange(range, includeNetworkInfo); } @@ -492,11 +499,11 @@ export function ipv4Compare(a, b) { */ export function ipv6Compare(a, b) { - let a_ = strToIpv6(a), + const a_ = strToIpv6(a), b_ = strToIpv6(b); for (let i = 0; i < a_.length; i++){ - if (a_[i] != b_[i]){ + if (a_[i] !== b_[i]){ return a_[i] - b_[i]; } } diff --git a/src/core/operations/ParseIPRange.mjs b/src/core/operations/ParseIPRange.mjs index 7a15c29f..84d4dd63 100644 --- a/src/core/operations/ParseIPRange.mjs +++ b/src/core/operations/ParseIPRange.mjs @@ -60,10 +60,10 @@ class ParseIPRange extends Operation { // Check what type of input we are looking at const ipv4CidrRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\/(\d\d?)\s*$/, ipv4RangeRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\s*-\s*((?:\d{1,3}\.){3}\d{1,3})\s*$/, - ipv4ListRegex = /^\s*(((?:\d{1,3}\.){3}\d{1,3})(\/(\d\d?))?(\n|$)(\n*))*$/, + ipv4ListRegex = /^\s*(((?:\d{1,3}\.){3}\d{1,3})(\/(\d\d?))?(\n|$)(\n*))+\s*$/, ipv6CidrRegex = /^\s*(((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))\/(\d\d?\d?)\s*$/i, ipv6RangeRegex = /^\s*(((?=.*::)(?!.*::[^-]+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))\s*-\s*(((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\17)::|:\b|(?![\dA-F])))|(?!\16\17)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))\s*$/i, - ipv6ListRegex = /^((((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))(\/(\d\d?\d?))?(\n|$)(\n*))*$/ig; + ipv6ListRegex = /^\s*((((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\4)::|:\b|(?![\dA-F])))|(?!\3\4)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4}))(\/(\d\d?\d?))?(\n|$)(\n*))+\s*$/i; let match; if ((match = ipv4CidrRegex.exec(input))) { diff --git a/test/tests/operations/ParseIPRange.mjs b/test/tests/operations/ParseIPRange.mjs index 41ca5a43..1b81cbc0 100644 --- a/test/tests/operations/ParseIPRange.mjs +++ b/test/tests/operations/ParseIPRange.mjs @@ -1,5 +1,5 @@ /** - * MS tests. + * Parse IP Range tests. * * @author Klaxon [klaxon@veyr.com] * @copyright Crown Copyright 2017 @@ -15,7 +15,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -26,7 +26,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -37,7 +37,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -48,7 +48,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -59,7 +59,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -70,7 +70,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -81,7 +81,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -92,7 +92,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -103,7 +103,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -114,7 +114,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], }, @@ -125,7 +125,7 @@ TestRegister.addTests([ recipeConfig: [ { "op": "Parse IP range", - "args": [true,true,false] + "args": [true, true, false] }, ], },