工做中收集的工具類函數庫

對象、數組對象

  1. clone( data ) 深拷貝git

    clone = function (data) {
        var tmp = data.constructor === Array ? [] : {}
        for (var k in data) {
          if (!data[k]) {
            tmp[k] = data[k]
          } else if (data[k].constructor === Object || data[k].constructor === Array) {
            tmp[k] = deepClone(data[k])
          } else {
            tmp[k] = data[k]
          }
        }
        return tmp
      }
  2. formateData( ne, struct ) 數據格式化,去除struct中不存在的keyjson

    // 格式化數據
      formateData: function (ne, struct) {
        return getMess(ne, struct)
      }
  3. compareData( ol, ne, struct ) 數據比對,ol爲原數據, ne爲新數據, struct爲數據結構數組

    // json數據格式化 ol爲原數據,ne爲新數據, struct爲數據結構
      compareData: function (ol, ne, struct) {
        if (struct) {
          // 傳入了 取出數據結構的 按照取出數據結構進行比對
          var mps = getMess(ne, struct)
          return getchanged(ol, mps)
        } else {
          // 沒有傳入的
          return getMy(ol, ne)
        }
      }
    // 結構格式化
    function getMess (ne, struct) {
      var tem = {}
      if (!ne) {
        // 假值
        // null/0/''
        tem = ne
      } else if (ne.constructor === Array) {
        // 數組
        tem = []
        for (var i = 0; i < ne.length; i++) {
          tem.push(getMess(ne[i], struct[0]))
        }
      } else if (ne.constructor === Object) {
        // 對象
        tem = {}
        for (var k in struct) {
          tem[k] = getMess(ne[k], struct[k])
        }
      } else {
        // 簡單值
        tem = ne
      }
      return tem
    }
    // 數據比對
    function getMy (ol, ne, struct) {
      var tem = {}
      for (var ta in ne) {
        if (ta === 'sel') continue
        if (ta === 'id') tem['id'] = ne[ta]
        // eslint-disable-next-line eqeqeq
        if (ol[ta] != ne[ta] && struct[ta]) {
          if (ne[ta] instanceof Array) {
            tem[ta] = []
            // 先判斷b的是否都在
            for (var i = 0; i < ne[ta].length; i++) {
              // 是不是新增的 新增的沒有id
              if (ne[ta][i].id) {
                for (let j = 0; j < ol[ta].length; j++) {
                  if (ne[ta][i].id === ol[ta][j].id && ne[ta][i].id !== undefined && ol[ta][i].id !== undefined) {
                    ol[ta][j].jsonsel = true
                    tem[ta].push(getMy(ol[ta][i], ne[ta][i], struct[ta][0]))
                  }
                }
              } else {
                // 沒有id的新增數據
                // 這裏是新增的獲取數據
                tem[ta].push(ne[ta][i])
              }
            }
    
            // 標示已經被刪除的
            for (let j = 0; j < ol[ta].length; j++) {
              if (!ol[ta][j].jsonsel) {
                tem[ta].push({
                  id: ol[ta][j].id,
                  isdeleted: 1
                })
              }
            }
          } else if (ne[ta] instanceof Object && !(ne[ta] instanceof Date)) {
            if (ne[ta].id) {
              if (ne[ta].id === ol[ta].id && ne[ta].id !== undefined && ol[ta].id !== undefined) {
                tem[ta] = getMy(ol[ta], ne[ta], struct[ta])
              }
            } else {
              // 這裏是新增的獲取數據
              tem[ta] = ne[ta]
            }
          } else {
            tem[ta] = ne[ta]
          }
        }
      }
      if (JSON.stringify(tem) === '{}') {} else { return tem }
    }
    function getchanged (ol, ne, delTimeStr) {
      var tem = {}
      for (var ta in ne) {
        if (ta === 'sel') continue
        if (ta === 'id') tem['id'] = ne[ta]
        if (ta === 'version') tem['version'] = ne[ta]
        // 刪除dateString結尾的字段=>for:fa
        if (delTimeStr && (ta.substring(ta.length - 9) === 'dateString' || ta.substring(ta.length - 9) === 'timeString')) {
          delete ne.ta
        }
        // eslint-disable-next-line eqeqeq
        if (ol[ta] != ne[ta] && (typeof (ol[ta]) != 'undefined')) {
          if (ne[ta] instanceof Array) {
            tem[ta] = []
            // 先判斷b的是否都在
            for (var i = 0; i < ne[ta].length; i++) {
              // 是不是新增的 新增的沒有id
              if (ne[ta][i].id) {
                for (let j = 0; j < ol[ta].length; j++) {
                  if (ol[ta][j].id && ne[ta][i].id === ol[ta][j].id) {
                    ol[ta][j].jsonsel = true
                    tem[ta].push(getchanged(ol[ta][j], ne[ta][i], delTimeStr))
                  }
                }
              } else {
                // 沒有id的新增數據
                // 這裏是新增的獲取數據
                tem[ta].push(ne[ta][i])
              }
            }
            // 標示已經被刪除的
            for (let j = 0; j < ol[ta].length; j++) {
              if (!ol[ta][j].jsonsel) {
                if (ol[ta][j].id) {
                  tem[ta].push({
                    id: ol[ta][j].id,
                    isdeleted: 1
                  })
                }
              }
            }
          } else if (ne[ta] instanceof Object && !(ne[ta] instanceof Date)) {
            if (ne[ta].id) {
              if (ne[ta].id === ol[ta].id && ne[ta].id !== undefined && ol[ta].id !== undefined) {
                tem[ta] = getchanged(ol[ta], ne[ta], delTimeStr)
              }
            } else {
              // 這裏是新增的獲取數據
              tem[ta] = ne[ta]
            }
          } else {
            tem[ta] = ne[ta]
          }
        }
      }
      if (JSON.stringify(tem) === '{}') {} else { return tem }
    }
    function getchang (ol, ne, delTimeStr) {
      var tem = {}
      for (var ta in ne) {
        if (ta === 'sel') continue
        if (ta === 'id') tem['id'] = ne[ta]
        if (ta === 'version') tem['version'] = ne[ta]
        // 刪除dateString結尾的字段=>for:fa
        if (delTimeStr && (ta.substring(ta.length - 9) === 'dateString' || ta.substring(ta.length - 9) === 'timeString')) {
          delete ne.ta
        }
        // eslint-disable-next-line eqeqeq
        if (ol[ta] != ne[ta] && (typeof (ol[ta]) != 'undefined')) {
          if (ne[ta] instanceof Array) {
            tem[ta] = []
            // 先判斷b的是否都在
            for (var i = 0; i < ne[ta].length; i++) {
              // 是不是新增的 新增的沒有id
              if (ne[ta][i].id) {
                for (let j = 0; j < ol[ta].length; j++) {
                  if (ol[ta][j].id && ne[ta][i].id === ol[ta][j].id) {
                    ol[ta][j].jsonsel = true
                    tem[ta].push(getchang(ol[ta][j], ne[ta][i], delTimeStr))
                  }
                }
              } else {
                // 沒有id的新增數據
                // 這裏是新增的獲取數據
                tem[ta].push(ne[ta][i])
              }
            }
            // 標示已經被刪除的
            for (let j = 0; j < ol[ta].length; j++) {
              if (ol[ta][j] instanceof Object) {
                if (!ol[ta][j].jsonsel) {
                  if (ne[ta][i].id) {
                    tem[ta].push({
                      id: ol[ta][j].id,
                      isdeleted: 1
                    })
                  } else {
                    // 無操做
                  }
                }
              }
            }
          } else if (ne[ta] instanceof Object && !(ne[ta] instanceof Date)) {
            if (ne[ta].id) {
              if (ne[ta].id === ol[ta].id && ne[ta].id !== undefined && ol[ta].id !== undefined) {
                tem[ta] = getchang(ol[ta], ne[ta], delTimeStr)
              }
            } else {
              // 這裏是新增的獲取數據
              tem[ta] = ne[ta]
            }
          } else {
            tem[ta] = ne[ta]
          }
        }
      }
      if (JSON.stringify(tem) === '{}') {} else { return tem }
    }
  4. checkEmpty( data, notEmpty ) 非空校驗,數據Model, 校驗提示數組數據結構

  5. 生成uuid
    uuid = function () {
      var s = []
      var hexDigits = '0123456789abcdef'
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
      }
      s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = '-'
    
      var uuid = s.join('')
      return uuid
    }

     

日期

  1. timestampToData(timestamp) 格式化時間戳爲日期dom

    // 格式化時間戳爲日期格式
    timestampToData = function (timestamp) {
      var date = new Date(timestamp)
      var Y = date.getFullYear()
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      return Y + '-' + M + '-' + D
    }
  2. dateCompate(startdate, enddate)起始結束日期對比ui

    // 起始結束時間對比
    dateCompare = function (startdate, enddate) {
      try {
        var arr = startdate.split('-')
        startdate = new Date(arr[0], arr[1] - 1, arr[2])
        var arrs = enddate.split('-')
        enddate = new Date(arrs[0], arrs[1] - 1, arrs[2])
        if (startdate.getTime() > enddate.getTime()) {
          return false
        } else {
          return true
        }
      } catch (e) {
        return false
      }
    }
  3. getNowTime() 獲取當前時間url

    getNowTime: function () {
                var d = new Date();
                return d.getFullYear() + "-" + date2pos(d.getMonth() + 1) + "-" + date2pos(d.getDate()) + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
            }
  4. getMonthStart() 獲取當月第一天spa

    getSysMonthStart: function (date) {
                var date= date ? new Date(date) : new Date();
                date.setDate(1);
                return $filter('date')(date, 'yyyy-MM-dd');
            }
  5. getMonthEnd() 獲取當月最後一天eslint

    // 得到當前月最後一天
            getSysMonthEnd: function (date) {
                var date= date ? new Date(date) : new Date();
                return $filter('date')(getCurrentMonthLast(date), 'yyyy-MM-dd');
                function getCurrentMonthLast(date){
                    var currentMonth=date.getMonth();
                    var nextMonth=++currentMonth;
                    var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
                    var oneDay=1000*60*60*24;
                    return new Date(nextMonthFirstDay-oneDay);
                }
            }

……code

Http請求

  1. doGet(data,url){ return new Promise(funciton(){ ……})}

  2. doPost(data,url){ return new Promise(funciton(){ ……})}

  3. doDelete(data,url){ return new Promise(funciton(){ ……})}

  4. doInsert(data,url){ return new Promise(funciton(){ ……})}

  5. doFormData(data,url){ return new Promise(funciton(){ ……})}

…… 

相關文章
相關標籤/搜索