日常操作判断

  1. es6 async方法实现异步传参数
function timeout(value,ms) {
  return new Promise((resolve, reject) => {
    setTimeout(function() {
      resolve(value);
    }, ms);
  });
}
async function asyncPrint(value, ms) {
  let b = 0;
  await timeout(value,ms).then(function(a) { b = a; });
  console.log(b);
}
asyncPrint('hello world', 5000);
  1. es6删除数组某一项
arr.splice(arr.findIndex(item => item.id === 8), 1)
  1. JSON格式判断
const jsonParse = function(data) {
    if(typeof data == 'string') {
        try {
            return JSON.parse(data);
        } catch(e) {
            return;
        }
    }
}
  1. 序号格式化
const orderNumFn = function(num, len) {
    return (Array(len).join('0') + num).slice(-len);
}
  1. 深度复制对象
const cloneObj = function(obj) {
  if(obj!= null && typeof obj == 'object') {
    return JSON.parse(JSON.stringify(obj));
  }
}
  1. 获取数据类型(通用包括Object,Array,Null,undefined)
const getType = function(v) {
    let type = Object.prototype.toString.call(v).toLowerCase(),
        regex = /^\[object\s+([a-z]+)\]$/;
    return type.match(regex)[1];
}
  1. 自定义遍历方法
const customEach = function(opts,callback) {
  if(getType(opts.data) == 'array') {
    opts.data.forEach(function(item,index) {
        callback.call(this,item,index);
    });
  } else if(getType(opts.data) == 'object') {
    for(let i in opts.data) {
      callback.call(this,opts.data[i],i);
    }
  }
  return opts.data;
}
  1. 格式化数据(在数量前添加'+'/'-')
const numFormat = function(v,status,type) {
    let formatStr = '';
        v = parseInt(v);
    if(v !== 0) {
        formatStr = status + v;
    }else if(v === 0) {
        formatStr = v;
    }
    return formatStr;
}
  1. 判断内容为空时显示 '--'
const isEmpty = function(str,empty) {
    if(typeof str === 'undefined' || str === null || str.length < 1) {
        return empty || '----';
    } else {
        return str;
    }
}
  1. 遍历更新删除数组
/*
    遍历更新删除数据
    opts {}
    oldData    需要被更新的数据
    updateData 需要删除更新到opts.oldData里的数据
    type       数据更新类型(如init,add,update,del)
    idNameStr  数据项的id(如point_id,fid)
*/
const eachData = function(opts) {
    let updateData = opts.updateData,
        oldData = opts.oldData,
        type = opts.type,
        idNameStr = opts.idNameStr;

    $.each(updateData,function(upIndex,upItem){
        let oldVal = '',
            oldIndex = '',
            newIndex = '';
        oldData.forEach(function(oItem,oIndex) {
            // 删除
            if(type === 'delete' && oItem[idNameStr] === upIndex) {
                oldData.splice(oIndex,1);
            } else {
            // 更新和新增
                if(type === 'add') {
                    oldVal = upItem;
                }else if(type === 'update' && oItem[idNameStr] === upIndex) {
                    $.extend(oItem,upItem);                 
                    oldVal = oItem;
                    oldIndex = oIndex;                                                                                                                                                                                              ;
                }
                if(upItem.insert_fid_below == 0) {
                    newIndex = 0;
                } else if(oItem[idNameStr] == upItem.insert_fid_below) {
                    if(newIndex < oldData.length) {
                        newIndex = oIndex + 1;
                    } else {
                        newIndex = curIndx;
                    }
                }
            }

        });
        if(type === 'update' && oldVal !== '' && newIndex !== '') {
            if(oldIndex > newIndex) {
                oldIndex = oldIndex + 1;
            }
            oldData.splice(newIndex,0,oldVal);
            oldData.splice(oldIndex,1); 
        } else if(type === 'add' && oldVal !== '' && newIndex !== '') {
            oldData.splice(newIndex,0,oldVal)
        }

    });
}
/*
    refreshData 刷新数据
    opts {}
    oldData           需要被刷新的数据
    updateCount       需要更新的数量
    updateData        需要增删改到opts.oldData里的数据
    idNameStr         数据项的id(如point_id,fid)
*/
const refreshData = function(opts) {
    let oldData = opts.oldData,
        updateData = opts.updateData,
        idNameStr = opts.idNameStr;
    // 赋值id
    if(opts.updateId) oldData.id = opts.updateId;

    // 赋值count
    if(opts.updateCount || opts.updateCount == 0) oldData.count = opts.updateCount; 

    // 初始化数据
    if(updateData.init) {
        oldData.data = updateData.init;
    }

    // 添加数据
    if($.isPlainObject(updateData.add)) {
        eachData({
            idNameStr : idNameStr,
            oldData : oldData.data,
            updateData : updateData.add,
            type : 'add'
        });
    }

    // 更新数据
    if(updateData.update) {
        eachData({
            idNameStr : idNameStr,
            oldData : oldData.data,
            updateData : updateData.update,
            type : 'update'
        });
    }

    // 删除数据
    if(updateData.del) {
        eachData({
            idNameStr : idNameStr,
            oldData : oldData.data,
            updateData : updateData.del,
            type : 'delete'
        });
    }
}
  1. 表头固定
const cellFixedFn = function(e,fixedCell,sHeight) {
    let curEle = $(e.currentTarget),
        _fixedCell = curEle.find(fixedCell),
        scrollTop = curEle[0].scrollTop;            
    _fixedCell.css({
        '-webkit-transform': 'translateY('+scrollTop+'px)',
        '-ms-transform': 'translateY('+scrollTop+'px)',
        'transform': 'translateY('+scrollTop+'px)'
    });
}
  1. 随机id
createUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
      return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16);
    });
},

你可能感兴趣的:(日常操作判断)