IP端口解析校验:

1、判断是否在1-65535之间、判断是否符合端口规范,解析开始、结束。例如: 12,12-23(现这样,后期优化)


// 校验端口
export const portGather = (data) => {
  if (data != '') {
    // 匹配非数字、非英文横线、非英文逗号的字符
    const pattern = /[^0-9,-]/g;
    if (!pattern.test(data)) {
      // 判断数据 开头,末尾 只能是数字
      const head = /^[1-9]/g
      const end = /[0-9]$/g
      if (head.test(data) && end.test(data)) {
        // 将 - 替换成 ,将数据拆分成数组
        const newStr = data.replace(/-/g, ',');
        const dataArray = newStr.split(',');

        function hasSpecialElement (arr) {
          // 判断数组中是否存在 ''或 '0' 开头的元素
          let hasEmptyOrZero = arr.some(function (element) {
            return element === '' || element.startsWith('0');
          });
          // 根据判断结果返回相应的字符串
          return hasEmptyOrZero ? false : true;//存在false     不存在true
        }
        if (hasSpecialElement(dataArray)) {
          // 判断 存在大于 65535 的数字
          const result = dataArray.some(str => {
            const num = parseInt(str);
            return num > 65535;
          });
          if (!result) {
            const eligibleData = data.split(',')
            // 去重公共方法
            const findDuplicateWithCount = arr => {
              const map = new Map();
              arr.forEach(item => map.set(item, map.has(item) ? map.get(item) + 1 : 1));
              return Array.from(map.entries()).filter(([key, value]) => value > 1).map(([key, value]) => `${key}:出现${value}次`);
            }
            // 判断 是否存在  - 
            if (data.indexOf('-') == -1) {
              // 字符串中不含有英文短横线
              const result = eligibleData.map(str => str + '-' + str);

              // 去重
              const duplicateArrWithCount = findDuplicateWithCount(result)

              if (duplicateArrWithCount.length == 0) {
                return '输入合规'
              } else {
                return duplicateArrWithCount
              }

            } else {
              //  字符串中含有英文短横线
              //  这里 需要判断  xxxx-cccc  cccc>xxxx
              const scopePortGather = eligibleData.filter(str => str.includes('-'));
              const csinglePortGather = eligibleData.filter(str => !str.includes('-'));
              const scope = scopePortGather.every(str => {
                if (str.split('-').length !== 2) {
                  return false;
                }
                const [left, right] = str.split('-');
                return parseInt(left) < parseInt(right);
              });
              if (scope) {
                // 去重 
                const csingleScopePortGather = csinglePortGather.map(str => str + '-' + str);
                const allStandardPortGather = [...csingleScopePortGather, ...scopePortGather]
                // 去重
                const repetitionArr = findDuplicateWithCount(allStandardPortGather)

                if (repetitionArr.length == 0) {
                  return '输入合规'
                } else {
                  return repetitionArr
                }
              } else {
                return '数据错误,请修改(可能存在两个-连用,或者 存在:源端口 > 目的端口 )'
              }
            }
          } else {
            return '数据错误,请修改(可能存在大于65535 的数字 )'
          }
        } else {
          return '数据格式错误,请修改(可能存在 -,或 ,- 字符 或者 0开头的数字)'
        }
      } else {
        return '数据格式错误,请修改(首尾只能是数字,并且第一个数字大于0)'
      }
    } else {
      return '数据格式错误,请修改(字符串中存在:非数字、非英文横线、非英文逗号的字符)'
    }
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值