常用js方法封装使用
Posted 欢先森o
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了常用js方法封装使用相关的知识,希望对你有一定的参考价值。
// 冒泡排序 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方法封装使用的主要内容,如果未能解决你的问题,请参考以下文章