• 工具函数


    // 连字符转驼峰
    String.prototype.hyphenToHump = function () {
      return this.replace(/-(w)/g, (...args) => {
        return args[1].toUpperCase()
      })
    }
    
    // 驼峰转连字符
    String.prototype.humpToHyphen = function () {
      return this.replace(/([A-Z])/g, '-$1').toLowerCase()
    }
    
    // 日期格式化
    Date.prototype.format = function (format) {
      const o = {
        'M+': this.getMonth() + 1,
        'd+': this.getDate(),
        'h+': this.getHours(),
        'H+': this.getHours(),
        'm+': this.getMinutes(),
        's+': this.getSeconds(),
        'q+': Math.floor((this.getMonth() + 3) / 3),
        S: this.getMilliseconds(),
      }
      if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, `${this.getFullYear()}`.substr(4 - RegExp.$1.length))
      }
      for (let k in o) {
        if (new RegExp(`(${k})`).test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr(`${o[k]}`.length))
        }
      }
      return format
    }
    
    /**
     *获取前天、昨天、今天、明天、后天的时间
     *0:今天,以此类推昨天和明天、后天
     */
    function GetDateStr(AddDayCount) {
      // var dd = new Date();
      // dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
      // var y = dd.getFullYear();
      // var m = dd.getMonth() + 1;//获取当前月份的日期
      // var d = dd.getDate();
      // console.log(y + "-" + m + "-" + d,'y + "-" + m + "-" + d')
      // return y + "-" + m + "-" + d;
      return moment().add(AddDayCount, 'day').format('YYYY-MM-DD');
    }
    
    
    /**
     *获取前天、昨天、今天、明天、后天的时间
     *0:今天,以此类推昨天和明天、后天
     */
    function GetDateStr2(AddDayCount) {
      var dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1;//获取当前月份的日期
      var d = dd.getDate();
      return y + "-" + m + "-" + d;
      // return moment().add(AddDayCount, 'day').format('YYYY-MM-DD');
    }
    
    //获取两个时间节点间的日期
    function getDateSection(startTime, endTime) {
      let list = []
      for (let i = Date.parse(startTime); i <= Date.parse(endTime); i += 86400000) {
        list.push(moment(i).format('YYYYMMDD'))
      }
      return list
    }
    
    
    // 时间处理  秒数转化为天、时、分、秒
    function timeDeal(time) {
      let day,hours,min,second;
      day = Math.floor(time / 86400);
      hours = Math.floor((time - day * 86400) / 60 / 60);
      min = Math.floor((time - day * 86400 - hours * 60 * 60) / 60);
      second = Math.floor(time % 60);
      hours = hours < 10 ? '0' + hours : hours;
      min = min < 10 ? '0' + min : min;
      second = second < 10 ? '0' + second : second;
      if(day > 0) {
        return `${day}天 ${hours}:${min}:${second}`
      }
      return `${hours}:${min}:${second}`
    }
    
    // 转义  防止js注入攻击
    let htmlEncodeJQ = (str) => {
      return $('<span/>').text(str).html();
    }
    
    // 休眠 延迟
    let sleep = (s) => {
      s = s || 0;
      s = parseInt(s) * 1000;
      let now = +new Date();
      let timer = null;
      return new Promise((resolve, reject) => {
        timer = setInterval(() => {
          if (now + s < +new Date()) {
            clearInterval(timer);
            resolve(true);
          }
        }, 10)
      })
    }
    
    // 获取url字段
    let GetQueryString = (name) => {
      var reg = new RegExp("(^|/?/?)"+ name +"=([^/?/?]*)(/?/?|$)");
      var r = window.location.search.substr(1).match(reg);
      if(r!=null)return  decodeURI(r[2]); return null; 
    }
    
     // 千位分割
    const thousandBitSeparator = (num) => {
     return (num || 0).toString().replace(/(d)(?=(?:d{3})+$)/g, '$1,');
    }
    // 获取本地ip
    function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
        //compatibility for firefox and chrome
        var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
        var pc = new myPeerConnection({
          iceServers: []
        }),
          noop = function () { },
          localIPs = {},
          ipRegex = /([0-9]{1,3}(.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
          key;
      
        function iterateIP(ip) {
          if (!localIPs[ip]) onNewIP(ip);
          localIPs[ip] = true;
        }
      
        //create a bogus data channel
        pc.createDataChannel("");
      
        // create offer and set local description
        pc.createOffer().then(function (sdp) {
          sdp.sdp.split('
    ').forEach(function (line) {
            if (line.indexOf('candidate') < 0) return;
            line.match(ipRegex).forEach(iterateIP);
          });
      
          pc.setLocalDescription(sdp, noop, noop);
        }).catch(function (reason) {
          // An error occurred, so handle the failure to connect
        });
      
        //sten for candidate events
        pc.onicecandidate = function (ice) {
          if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
          ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
        };
      }
    
    getUserIP(function (ip) {
       console.log(ip);
    });
  • 相关阅读:
    跟踪创建类的个数
    动手动脑3
    动手动脑:随机数发生器和函数重载
    统计英语文章中单词
    动手动脑(1)
    原码、反码、补码
    java测试ATM自助操作系统
    深入浅出 TCP/IP 协议栈
    十大经典排序算法(动图演示)
    深入浅出 Viewport 设计原理
  • 原文地址:https://www.cnblogs.com/kewenxin/p/11857926.html
Copyright © 2020-2023  润新知