• 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;
    }
  • 相关阅读:
    自然语言处理(NLP)入门
    OpenSSL证书认证过程
    IAR EWARM安装时报Fatal Error[Cp001]: Copy protection check, No valid license found for this product [24]
    INTEL FPGA去隔行IP DEMO
    Windows “在此系统上禁止运行脚本”解决办法
    vue中axios请求本地json文件404
    vscode tab 按钮功能变为切换按键,改回缩进功能
    引用elementUi 字体文件丢失导致图标都显示小方块
    ubuntu 添加 ll 命令
    ubuntu sudo source 时 command not found 错误
  • 原文地址:https://www.cnblogs.com/wjunwei/p/10340896.html
Copyright © 2020-2023  润新知