24、js工具38-43

76 阅读2分钟

38、

/**
     * @method arraySortByKey() 针对数组对象做排序
     * @param {array} list 需要排序的数组对象
     * @param {String} key 排序的基准key值
     */
    arraySortByKey(list, key) {
        function charCompare(charA, charB) {
            // 谁为非法值谁在前面
            if (charA === undefined || charA === null || charA === '' || charA === ' ' || charA === ' ') {
                return -1
            }
            if (charB === undefined || charB === null || charB === '' || charB === ' ' || charB === ' ') {
                return 1
            }
            // 如果都为英文或者都为汉字则直接对比
            if ((notChinese(charA) && notChinese(charB)) || (!notChinese(charA) && !notChinese(charB))) {
                return charA.localeCompare(charB)
            } else {
                // 如果不都为英文或者汉字,就肯定有一个是英文,如果a是英文,返回-1,a在前,否则就是b是英文,b在前
                if (notChinese(charA)) {
                    return -1
                } else {
                    return 1
                }
            }
        }

        function notChinese(char) {
            const charCode = char.charCodeAt(0)
            return charCode >= 0 && charCode <= 128
        }

        if (list === undefined || list === null) return []
        list.sort((a, b) => {
            let strA = a[key]
            let strB = b[key]
            // 谁为非法值谁在前面
            if (strA === undefined || strA === null || strA === '' || strA === ' ' || strA === ' ') {
                return -1
            }
            if (strB === undefined || strB === null || strB === '' || strB === ' ' || strB === ' ') {
                return 1
            }
            // 如果a和b中全部都是汉字,或者全部都非汉字
            if ((strA.split('').every(char => notChinese(char)) && strB.split('').every(char => notChinese(char))) ||
                (strA.split('').every(char => !notChinese(char)) && strB.split('').every(char => !notChinese(char)))) {
                return strA.localeCompare(strB)
            } else {
                const charAry = strA.split('')
                for (const i in charAry) {
                    if ((charCompare(strA[i], strB[i]) !== 0)) {
                        return charCompare(strA[i], strB[i])
                    }
                }
                // 如果通过上面的循环对比还比不出来,就无解了,直接返回-1
                return -1
            }
        })
        return list
    },

39、节流、防抖

//节流, 一段时间内只执行第一次请求
    throttle(fn, delay = 1000) {
        let that = this
        const begin = this.begin || 0
        this.begin = Date.now()
        return function (...args) {
            let current = Date.now()
            if (current - begin > delay) {
                fn.apply(that, args)
            }
        }
    },
    //防抖, 一段时间内只执行最后一次请求
    debounce(fn, delay = 1000) {
        let that = this
        let timer = this.timer
        return function (...args) {
            clearTimeout(timer)
            that.timer = setTimeout(function () {
                fn.apply(that, args);
            }, delay)
        }
    },

40、 加密

/**@augments
     * @method encryption()  加密
     * @param {String}
     * publicKey:公钥;password:密码
     */
    encryption(publicKey, password) {
        let encryptor = new JSEncrypt()
        encryptor.setPublicKey(publicKey)
        let psw = encryptor.encrypt(password)
        return psw
    },

41、blob转json

/**
	 * @method blobtoJson() blob转json
	 * @param {Object} blobData blob数据
	 * @return
	 */
	blobtoJson(blobData) {
	    return new Promise((resolve, reject) => {
	        const reader = new FileReader()
	        reader.onload = function (event) {
	            const message = JSON.parse(reader.result)
	            resolve(message)
	        }
	        reader.readAsText(blobData)
	    })
	},

42、获取地址栏参数,用于解析token

/**
     * 获取地址栏参数,用于解析token
     * @returns {Object}
     */
    getUrlParms: function () {
        var args = {};
        // 获取查询串
        var query = location.search.split('?')[1];
        if (query) {
            // 在逗号处断开
            var pairs = query.split("&");
            var len = pairs.length;
            for (var i = 0; i < len; i++) {
                // 查找name=value
                var pos = pairs[i].indexOf('=');
                // 如果没有找到就跳过
                if (pos === -1) {
                    continue;
                }
                // 存为属性
                args[pairs[i].substring(0, pos)] = unescape(pairs[i].substring(pos + 1));
            }
        }
        return args;
    },

43、跳转到login页

/**
     * 跳转到login页
     */
    toLogin: function () {
        if(window.location.hash === '#/login') {
            return
        }
        window.location.href = "#/login"
        if(!!window.ActiveXObject || "ActiveXObject" in window)   {
            window.location.reload()
        }
    },