fix regex issues and ESlint errors and warnings

fix comment

fix ESlint errors and warnings

fix regex

add author
This commit is contained in:
Klaxon 2018-08-28 20:15:59 +10:00
parent 86145dbf67
commit 2820660264
3 changed files with 44 additions and 37 deletions

View File

@ -3,6 +3,7 @@
* *
* @author picapi * @author picapi
* @author n1474335 [n1474335@gmail.com] * @author n1474335 [n1474335@gmail.com]
* @author Klaxon [klaxon@veyr.com]
* @copyright Crown Copyright 2016 * @copyright Crown Copyright 2016
* @license Apache-2.0 * @license Apache-2.0
*/ */
@ -200,17 +201,19 @@ export function ipv6HyphenatedRange(range, includeNetworkInfo) {
*/ */
export function ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList) { export function ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, allowLargeList) {
var ipv4List = match[0].split("\n"); let ipv4List = match[0].split("\n");
ipv4List = ipv4List.filter(Boolean); 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++) { for (let i = 0; i < ipv4CidrList.length; i++) {
let network = strToIpv4(ipv4CidrList[i].split("/")[0]); const network = strToIpv4(ipv4CidrList[i].split("/")[0]);
let cidrRange = parseInt(ipv4CidrList[i].split("/")[1]); const cidrRange = parseInt(ipv4CidrList[i].split("/")[1], 10);
if (cidrRange < 0 || cidrRange > 31) { if (cidrRange < 0 || cidrRange > 31) {
return "IPv4 CIDR must be less than 32"; return "IPv4 CIDR must be less than 32";
} }
let mask = ~(0xFFFFFFFF >>> cidrRange), const mask = ~(0xFFFFFFFF >>> cidrRange),
cidrIp1 = network & mask, cidrIp1 = network & mask,
cidrIp2 = cidrIp1 | ~mask; cidrIp2 = cidrIp1 | ~mask;
ipv4List.splice(ipv4List.indexOf(ipv4CidrList[i]), 1); ipv4List.splice(ipv4List.indexOf(ipv4CidrList[i]), 1);
@ -218,9 +221,9 @@ export function ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, a
} }
ipv4List = ipv4List.sort(ipv4Compare); ipv4List = ipv4List.sort(ipv4Compare);
let ip1 = ipv4List[0]; const ip1 = ipv4List[0];
let ip2 = ipv4List[ipv4List.length - 1]; const ip2 = ipv4List[ipv4List.length - 1];
let range = [ip1 + " - " + ip2] const range = [ip1 + " - " + ip2];
return ipv4HyphenatedRange(range, includeNetworkInfo, enumerateAddresses, allowLargeList); return ipv4HyphenatedRange(range, includeNetworkInfo, enumerateAddresses, allowLargeList);
} }
@ -234,26 +237,30 @@ export function ipv4ListedRange(match, includeNetworkInfo, enumerateAddresses, a
*/ */
export function ipv6ListedRange(match, includeNetworkInfo) { export function ipv6ListedRange(match, includeNetworkInfo) {
var ipv6List = match[0].split("\n"); let ipv6List = match[0].split("\n");
ipv6List = ipv6List.filter(function(str) {return str.trim();}); ipv6List = ipv6List.filter(function(str) {
return str.trim();
});
for (let i =0; i < ipv6List.length; i++){ for (let i =0; i < ipv6List.length; i++){
ipv6List[i] = ipv6List[i].trim(); 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++) { for (let i = 0; i < ipv6CidrList.length; i++) {
let network = strToIpv6(ipv6CidrList[i].split("/")[0]); const network = strToIpv6(ipv6CidrList[i].split("/")[0]);
let cidrRange = parseInt(ipv6CidrList[i].split("/")[1]); const cidrRange = parseInt(ipv6CidrList[i].split("/")[1], 10);
if (cidrRange < 0 || cidrRange > 127) { if (cidrRange < 0 || cidrRange > 127) {
return "IPv6 CIDR must be less than 128"; return "IPv6 CIDR must be less than 128";
} }
let cidrIp1 = new Array(8), const cidrIp1 = new Array(8),
cidrIp2 = new Array(8); cidrIp2 = new Array(8);
let mask = genIpv6Mask(cidrRange); const mask = genIpv6Mask(cidrRange);
for (let j = 0; j < 8; j++) { for (let j = 0; j < 8; j++) {
cidrIp1[j] = network[j] & mask[j]; cidrIp1[j] = network[j] & mask[j];
@ -263,9 +270,9 @@ export function ipv6ListedRange(match, includeNetworkInfo) {
ipv6List.push(ipv6ToStr(cidrIp1), ipv6ToStr(cidrIp2)); ipv6List.push(ipv6ToStr(cidrIp1), ipv6ToStr(cidrIp2));
} }
ipv6List = ipv6List.sort(ipv6Compare); ipv6List = ipv6List.sort(ipv6Compare);
let ip1 = ipv6List[0]; const ip1 = ipv6List[0];
let ip2 = ipv6List[ipv6List.length - 1]; const ip2 = ipv6List[ipv6List.length - 1];
let range = [ip1 + " - " + ip2] const range = [ip1 + " - " + ip2];
return ipv6HyphenatedRange(range, includeNetworkInfo); return ipv6HyphenatedRange(range, includeNetworkInfo);
} }
@ -492,11 +499,11 @@ export function ipv4Compare(a, b) {
*/ */
export function ipv6Compare(a, b) { export function ipv6Compare(a, b) {
let a_ = strToIpv6(a), const a_ = strToIpv6(a),
b_ = strToIpv6(b); b_ = strToIpv6(b);
for (let i = 0; i < a_.length; i++){ for (let i = 0; i < a_.length; i++){
if (a_[i] != b_[i]){ if (a_[i] !== b_[i]){
return a_[i] - b_[i]; return a_[i] - b_[i];
} }
} }

View File

@ -60,10 +60,10 @@ class ParseIPRange extends Operation {
// Check what type of input we are looking at // Check what type of input we are looking at
const ipv4CidrRegex = /^\s*((?:\d{1,3}\.){3}\d{1,3})\/(\d\d?)\s*$/, 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*$/, 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, 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, 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; let match;
if ((match = ipv4CidrRegex.exec(input))) { if ((match = ipv4CidrRegex.exec(input))) {

View File

@ -1,5 +1,5 @@
/** /**
* MS tests. * Parse IP Range tests.
* *
* @author Klaxon [klaxon@veyr.com] * @author Klaxon [klaxon@veyr.com]
* @copyright Crown Copyright 2017 * @copyright Crown Copyright 2017