常用js函数的封装集合,更新中...

普通冒泡排序

function bubbling(arr, isBigSmall) { // arr 数组 , isBigSmall 是否从大到小排练
    let num = arr.length
    for (let i = 0; i < num; i++) {
        for (let j = 0; j < num; j++) {
            if (arr[j] > arr[j + 1]) {
                let numNumber = arr[j]
                arr[j] = arr[j + 1]
                arr[j + 1] = numNumber
            }
        }
    }
    if (isBigSmall || isBigSmall === true) { //从大到小排练
        arr = arr.reverse()
    }
    return arr // 默认从小到大
}

快速排序

function rapidSort(arr, isBigSmall) { // arr 数组 , isBigSmall 是否从大到小排练
    let left = []
    let right = []
    let center = []
    let arrLength = arr.length
    for (let i = 0; i < arrLength; i++) {
        if (arr[i] === arr[0]) {
            center.push(arr[i])
        } else if (arr[i] > arr[0]) {
            right.push(arr[i])
        } else {
            left.push(arr[i])
        }
    }
//将数组分为3部分, 同时进行排练,最后合并 let leftnew
= bubblingSortList(left) let rightnew = bubblingSortList(right) let newArr = leftnew.concat(center, rightnew) if (isBigSmall || isBigSmall === true) { //从大到小排练 newArr = newArr.reverse() } return newArr }

function bubblingSortList(index) { let arrLength = index.length for (let i = 0; i < arrLength; i++) { for (let j = 0; j < arrLength; j++) { if (index[j] > index[j + 1]) { let num = index[j] index[j] = index[j + 1] index[j + 1] = num } } } return index }

常用的数组去重ES5

function outRepetitionES5(arr) {
    let newArr = []
    for (let i = 0; i < arr.length; i++) {
        if (newArr.indexOf(arr[i]) == -1) {
            newArr.push(arr[i])
        }
    }
    return newArr
}

数组去重ES6

function outRepetitionES6(arr) {
    return [...new Set(arr)]
}

获取范围随机数

function randomFun(min, max, arrLength) { // min 随机数最小范围 ,ma随机数最大范围 arrLength[可选参数] 是否以数组的形式返回多个
    if (arrLength) { // 没有长度返回单个随机数
        singleRandomFun(min, max)
    } else { // 返回一个指定长度的数组,每一个都是随机数
        let arr = []
        for (let i = 0; i < arrLength;i++) {
            arr.push(singleRandomFun(min, max))
        }
        return arr
    }
}
//获取单个随机数
function singleRandomFun(min, max) {
    return Math.floor(Math.random() * (max - min))
}

获取当前时间格式

function getDate(isTime, separatorFront,separatorDehind) { // isTime 是否需要详细时间 separatorFront 设置指定分隔符(年月日的) 默认 "-" separatorDehind 设置指定分隔符(时间的) 默认':'
var t = new Date();
var n = t.getFullYear();
var y = bianhuan(t.getMonth() + 1);
var r = bianhuan(t.getDate());
var s = bianhuan(t.getHours());
var f = bianhuan(t.getMinutes());
var m = bianhuan(t.getSeconds());
let front = "-"
let behind = ':'
if (separatorFront) { // 是否自定义设置年月日的分隔符
front = separatorFront
}
if (separatorDehind) { // 是否自定义设置时间的分隔符
behind = separatorDehind
}
if (!isTime) {
var time = `${n}${front}${y}${front}${r}`
} else {
var time = `${n}${front}${y}${front} ${r} ${s}${behind}${f}${behind}${m}`

}
return time;
}
// 加“0”判断
function bianhuan(s) {
s = s < 10 ? "0" + s : s;
return s;
}

判断星期几 

function getWeek(getDate) {
    if (/^[0-9]{4}-[0-1]?[0-9]{1}-[0-3]?[0-9]{1}$/.test(getDate)) { // 判断是日期格式
        let str = '星期'
        let week = getDate.getDay()
        let strNumber = ['', '', '', '', '', '', '']
        return str + strNumber[week]
    } else {
        return '非日期格式'
    }
}

生成指定位数的验证码

function specifiedYZM(numNumber, isLetter) { // 验证码长度[numNumber] 是否有字母 [isLetter] 
    if (numNumber) {
        let arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
        let arr2 = '1234567890qwertyuiopasdfghjklzxcvbnm'
        arr2 = arr2.split('')
        let arr = ''
        let arrLength = ''
        let tar = []
        if (isLetter) {
            arr = arr2
            arrLength = arr2.length
        } else {
            arr = arr1
            arrLength = arr1.length
        }
        for (let i = 0; i < numNumber; i++) {
            tar.push(arr[Math.floor(Math.random() * arrLength)])
        }
        tar = tar.join('')
        return tar
    } else {
        return '验证码长度不可为空'
    }
}

 密码强度

 function passStrength(stringText){
     let passLv = 0
    if(stringText.length >= 6){
        passLv++
    }
    if(/[0-9]/.test(stringText)) {
        passLv++
    }
    if(/[a-z]/.test(stringText)){
        passLv++
    }
    if(/[A-Z]/.test(stringText)){
        passLv++
    }
    if(/[.|-|_]/.test(stringText)){
        passLv++
    }
    return passLv
}

 localStroage.setItem()进行缓存

function cacheSetFun(cacheName, cacheContent) {
			localStorage.setItem(cacheName,JSON.stringify(cacheContent))
		}

localStroage.getItem()获取指定缓存

function cacheGetFun(cacheName) {
			return JSON.parse(localStorage.getItem(cacheName))
		}

 清空所有缓存

function clearFun(){
			localStorage.clear()
		}

  删除指定缓存

function deletecache(cacheName) {
			 localStorage.removeItem(cacheName); 
		}

  

原文地址:https://www.cnblogs.com/zxli/p/15044250.html