• 项目经常使用的common.js 方法


    // 生成随机的uuid
    export const generateUUID = function () {
      let d = new Date().getTime()
      const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        const r = (d + Math.random() * 16) % 16 | 0
        d = Math.floor(d / 16)
        return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16)
      })
      return uuid
    }
    // localstroge
    export const storage = {
      get: function (key) {
        var value = localStorage.getItem(key)
        if (value) {
          try {
            var value_json = JSON.parse(value)
            if (typeof value_json === 'object') {
              return value_json
            } else if (typeof value_json === 'number') {
              return value_json
            }
          } catch (e) {
            return value
          }
        } else {
          return false
        }
      },
      set: function (key, value) {
        localStorage.setItem(key, value)
      },
      remove: function (key) {
        localStorage.removeItem(key)
      },
      clear: function () {
        localStorage.clear()
      }
    }
    //判断类型
    //  isType([], 'Array') // true
    // isType(/d/, 'RegExp') // true
    // isType(new Date(), 'Date') // true
    // isType(function(){}, 'Function') // true
    // isType(Symbol(1), 'Symbol') // true
    export const  isType = function(target, type) {
      let targetType = Object.prototype.toString.call(target).slice(8, -1).toLowerCase()
      return targetType === type.toLowerCase()
    }
    //对象属性剔除   lodash有
    // let data = {
    //   id: 1,
    //   title: 'xxx',
    //   comment: []
    // }
    // omit(data, ['id'])  {title: 'xxx', comment: []}
    export const  omit = function(object, props=[]){
      let res = {}
      Object.keys(object).forEach(key=>{
        if(!props.includes(key)){
          res[key] = typeof object[key] === 'object' && object[key] !== null ?
            JSON.parse(JSON.stringify(object[key])):
            object[key]
        }
      })
      return res
    }
    //对象属性选取方法
    export const  pick = function(object, props=[]){
      let res = {}
      Object.keys(object).forEach(key=>{
        if(props.includes(key)){
          res[key] = typeof object[key] === 'object' && object[key] !== null ?
            JSON.parse(JSON.stringify(object[key])):
            object[key]
        }
      })
      return res
    }
    //性能分析  谷歌非标准插件
    window.onload = function(){
      setTimeout(()=>{
        let t = performance.timing,
            m = performance.memory
        console.table({
          'DNS查询耗时': (t.domainLookupEnd - t.domainLookupStart).toFixed(0),
          'TCP链接耗时': (t.connectEnd - t.connectStart).toFixed(0),
          'request请求耗时': (t.responseEnd - t.responseStart).toFixed(0),
          '解析dom树耗时': (t.domComplete - t.domInteractive).toFixed(0),
          '白屏时间': (t.responseStart - t.navigationStart).toFixed(0),
          'domready时间': (t.domContentLoadedEventEnd - t.navigationStart).toFixed(0),
          'onload时间': (t.loadEventEnd - t.navigationStart).toFixed(0),
          'js内存使用占比': m ? (m.usedJSHeapSize / m.totalJSHeapSize * 100).toFixed(2) + '%' : undefined
        })
      })
    }
    //检测是否为PC端浏览器
    export const  isPCBroswer = function() {  
      let e = window.navigator.userAgent.toLowerCase()  
        , t = "ipad" == e.match(/ipad/i)  
        , i = "iphone" == e.match(/iphone/i)  
        , r = "midp" == e.match(/midp/i)  
        , n = "rv:1.2.3.4" == e.match(/rv:1.2.3.4/i)  
        , a = "ucweb" == e.match(/ucweb/i)  
        , o = "android" == e.match(/android/i)  
        , s = "windows ce" == e.match(/windows ce/i)  
        , l = "windows mobile" == e.match(/windows mobile/i);
      return !(t || i || r || n || a || o || s || l)  
    }
    //对象数组根据params去重
    export const deduplicationByparams = (arr, params)=> {
        let set = new Set()
        return  arr.reduce((total, item) => {
            if (!set.has(item[params])) {
                set.add(item[params])
                total.push(item)
            }
            return total
        }, [])
    }
    //合并对象
    export const deepMerge =(target, source)=> {
        Object.keys(source).forEach(key => {
            target[key] = target[key] && typeof target[key] === 'object' ? deepMerge(target[key], source[key]) : source[key]
        })
        return target
    }
    //小数相加
    export const addNum = (num1, num2) => {
      let sq1, sq2, m;
      try {
        sq1 = num1.toString().split('.')[1].length;
      } catch (e) {
        sq1 = 0;
      }
      try {
        sq2 = num2.toString().split('.')[1].length;
      } catch (e) {
        sq2 = 0;
      }
      m = Math.pow(10, Math.max(sq1, sq2));
      return (num1 * m + num2 * m) / m;
    }
  • 相关阅读:
    关于Python装饰器内层函数为什么要return目标函数的一些个人见解
    多项式拟合与线性回归
    numpy基本方法总结 --good
    numpy中的convolve的理解
    最容易理解的对卷积(convolution)的解释
    Python之numpy基本指令
    线性回归原理小结
    矩阵的导数与迹
    【MyBatis学习14】MyBatis和Spring整合
    【MyBatis学习13】MyBatis中的二级缓存
  • 原文地址:https://www.cnblogs.com/binglove/p/13424524.html
Copyright © 2020-2023  润新知