码迷,mamicode.com
首页 > Web开发 > 详细

常用js方法封装使用

时间:2019-12-10 16:39:31      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:async   封装   doc   过滤   child   index   port   lse   test   

// 冒泡排序
export function bubbleSort(arr) {
  let i = arr.length - 1;
  while (i > 0) {
    let maxIndex = 0;
    for (let j = 0; j < i; j++) {
      if (arr[j] > arr[j + 1]) {
        let temp = arr[j + 1];
        arr[j + 1] = arr[j];
        arr[j] = temp;
        maxIndex = j;
      }
    }
    i = maxIndex;
  }
  return arr;
}
// 通过属性值冒泡排序
export function bubbleSortByProp(arr, prop) {
  let i = arr.length - 1;
  while (i > 0) {
    let maxIndex = 0;
    for (let j = 0; j < i; j++) {
      if (arr[j][prop] > arr[j + 1][prop]) {
        let temp = arr[j + 1];
        arr[j + 1] = arr[j];
        arr[j] = temp;
        maxIndex = j;
      }
    }
    i = maxIndex;
  }
  return arr;
}
// 获取指定月份天数
export function getDateByMon(year, month){
  let d = new Date(year, month, 0);
  return d.getDate();
}
// 判断时间是不是今天
export function isTodayDate(time) {
  if (time.toDateString() === new Date().toDateString()) {
    return true;
  } else {
    return false;
  }
}
// 对象判空
export function isObjEmpty(obj) {
  if (obj && obj.constructor === Object) {
    return Object.keys(obj).length;
  }
  if (obj && obj.constructor === Array) {
    return obj.length;
  }
}
// 首字母大写
export const capitalizeFirstLetter = (string) => {
  return string.charAt(0).toUpperCase() + string.slice(1);
};
// 过滤值为空字符串或者为null的参数
export function getQuery(query) {
  let res = {};
  for (let [key, val] of Object.entries(query)) {
    if (typeof val === ‘string‘) {
      if (!val.trim() || val === ‘null‘) continue;
    } else {
      if (val === null || val === undefined) continue;
    }
    res[key] = val;
  };
  return res;
}
// 判断文件类型
export function isImage(fileName) { if (typeof fileName !== ‘string‘) return; let name = fileName.toLowerCase(); return name.endsWith(‘.png‘) || name.endsWith(‘.jpeg‘) || name.endsWith(‘.jpg‘) || name.endsWith(‘.png‘) || name.endsWith(‘.bmp‘); } export function isH5Video(fileName) { if (typeof fileName !== ‘string‘) return; let name = fileName.toLowerCase(); return name.endsWith(‘.mp4‘) || name.endsWith(‘.webm‘) || name.endsWith(‘.ogg‘); } export function isPdf(fileName) { if (typeof fileName !== ‘string‘) return; let name = fileName.toLowerCase(); return name.endsWith(‘.pdf‘); } export function isWord(fileName) { if (typeof fileName !== ‘string‘) return; let name = fileName.toLowerCase(); return name.endsWith(‘.doc‘) || name.endsWith(‘.docx‘); } export function isExcel(fileName) { if (typeof fileName !== ‘string‘) return; let name = fileName.toLowerCase(); return name.endsWith(‘.xlsx‘) || name.endsWith(‘.xls‘); }
// 数字2位转换
export function toDouble(num) {
  if (num < 10) {
    return ‘0‘ + num;
  } else {
    return num;
  }
}
// 动态加载js
export function loadApi(src) {
  return new Promise((resolve, reject) => {
    const script = document.createElement(‘script‘);
    script.type = ‘‘;
    script.src = src;
    script.async = ‘async‘;
    document.head.appendChild(script);

    script.onload = () => { resolve(); };
    script.onerror = () => { reject(); };
  });
}

// 动态加载css
export function loadCss(src) {
  return new Promise((resolve, reject) => {
    const link = document.createElement(‘link‘);
    link.rel = ‘stylesheet‘;
    link.href = src;
    document.head.appendChild(link);
  });
}

 

常用js方法封装使用

标签:async   封装   doc   过滤   child   index   port   lse   test   

原文地址:https://www.cnblogs.com/troublehuan/p/12017481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!