js方法笔记

//获取url参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if(r!=null)return  decodeURIComponent(r[2]); return '';
}
// 获取文件大小size

function getFileSize(fileSize) {
  let size = Number(fileSize)
  var fileSizeMsg = "";
  if (size < 1024) {
    fileSizeMsg = size.toFixed() + "B"
  }else if (1024 < size && size < 1048576) {
    fileSizeMsg = (size / 1024).toFixed() + "KB"
  } else if (size >= 1048576 && size < 1073741824) {
    fileSizeMsg = (size / 1024 / 1024).toFixed() + "MB"
  } else if (size >= 1073741824) {
    fileSizeMsg = (size / 1024 / 1024 / 1024).toFixed() + "GB";
  }
  return fileSizeMsg;
}
// 获取文件类型

function getFileCategory (fileName) {
  var index1 = fileName.lastIndexOf(".");
  var index2 = fileName.length;
  var suffix = fileName.substring(index1+1, index2);//后缀名
  let type = index1 > -1 ? suffix : 'unknown'
  const typeMap = {
    image: ['gif', 'jpg', 'jpeg', 'png'],
    video: ['mp4', 'rmvb', 'avi', 'flv'],
    audio: ['mp3'],
    text: ['txt'],
    pdf: ['pdf'],
    doc: ['doc', 'docx'],
    xls: ['xls', 'xlsx'],
    ppt: ['ppt', 'pptx'],
    zip: ['zip', 'rar']
  }
  Object.keys(typeMap).forEach((_type) => {
    const extensions = typeMap[_type]
    if (extensions.indexOf(type.toLowerCase()) > -1) {
      type = _type
    }
  })
  return type
}
//时间格式
Date.prototype.format = function(format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}

 

posted @ 2019-01-31 10:39  wjw_Dream  阅读(201)  评论(0编辑  收藏  举报