/*3.0.0-beta2*/ |
|
let ecDo = (function () { |
|
let ruleData = { |
|
checkType: { |
|
email(str){ |
|
return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str); |
|
}, |
|
mobile(str){ |
|
return /^1[3|4|5|7|8|9][0-9]{9}$/.test(str); |
|
}, |
|
tel(str){ |
|
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str); |
|
}, |
|
number(str){ |
|
return /^[0-9]$/.test(str); |
|
}, |
|
english(str){ |
|
return /^[a-zA-Z]+$/.test(str); |
|
}, |
|
text(str){ |
|
return /^\w+$/.test(str); |
|
}, |
|
chinese(str){ |
|
return /^[\u4E00-\u9FA5]+$/.test(str); |
|
}, |
|
lower(str){ |
|
return /^[a-z]+$/.test(str); |
|
}, |
|
upper(str){ |
|
return /^[A-Z]+$/.test(str); |
|
} |
|
} |
|
} |
|
|
|
function checkValue(val, vals) { |
|
let _val = val; |
|
if (Number.isNaN(val)) { |
|
_val = 'NaN' |
|
} |
|
return vals.indexOf(_val) !== -1; |
|
} |
|
|
|
return { |
|
/** |
|
* @description 擴展函數配置數據 |
|
*/ |
|
extend: { |
|
checkType(type, fn){ |
|
ruleData.checkType[type] = fn; |
|
}, |
|
filterStr(type, fn){ |
|
let fnName = 'filter' + ecDo.firstWordUpper(type); |
|
if (!ecDo[fnName]) { |
|
ecDo[fnName] = fn; |
|
} |
|
}, |
|
}, |
|
/** |
|
* @description 批量處理函數 |
|
*/ |
|
handle(){ |
|
let result=[],_arguments=[...arguments]; |
|
let fnName=_arguments.shift(); |
|
let arr=_arguments.shift(); |
|
for(let i=0;i<arr.length;i++){ |
|
_arguments.unshift(arr[i]); |
|
result.push(this[fnName].apply(this,_arguments)) |
|
_arguments.shift(); |
|
} |
|
return result; |
|
}, |
|
|
|
//***************字符串模塊**************************/ |
|
/** |
|
* @description 清除左右空格 |
|
*/ |
|
trim(str) { |
|
return str.replace(/(^\s*)|(\s*$)/g, ""); |
|
}, |
|
/** |
|
* @description 清除全部空格 |
|
*/ |
|
trimAll(str){ |
|
return str.replace(/\s+/g, ""); |
|
}, |
|
/** |
|
* @description 清除左空格 |
|
*/ |
|
trimLeft(str){ |
|
return str.replace(/(^\s*)/g, ""); |
|
}, |
|
/** |
|
* @description 清除右空格 |
|
*/ |
|
trimRight(str){ |
|
return str.replace(/(\s*$)/g, ""); |
|
}, |
|
/** |
|
* @description 大小寫切換 |
|
*/ |
|
toggleCase(str) { |
|
let itemText = "" |
|
str.split("").forEach(item => { |
|
if (/^([a-z]+)/.test(item)) { |
|
itemText += item.toUpperCase(); |
|
} else if (/^([A-Z]+)/.test(item)) { |
|
itemText += item.toLowerCase(); |
|
} else { |
|
itemText += item; |
|
} |
|
}); |
|
return itemText; |
|
}, |
|
/** |
|
* @description 首字母大寫 |
|
*/ |
|
firstWordUpper(str){ |
|
return str.replace(/\b\w+\b/g, word => word.substring(0, 1).toUpperCase() + word.substring(1)); |
|
}, |
|
/** |
|
* @description 首字母小寫 |
|
*/ |
|
firstWordLower(str){ |
|
return str.replace(/\b\w+\b/g, word => word.substring(0, 1).toLowerCase() + word.substring(1)); |
|
}, |
|
/** |
|
* @description 加密字符串 |
|
*/ |
|
encrypt(str,regIndex,ARepText = '*'){ |
|
let regText = '',Reg = null,_regIndex = regIndex.indexOf(',')===-1?'0,'+regIndex:regIndex,replaceText = ARepText; |
|
_regIndex = _regIndex.split(','); |
|
_regIndex[1]=_regIndex[1]||str.length-1; |
|
let result=''; |
|
str=regIndex.indexOf('-')===-1?str:str.split('').reverse().join(''); |
|
_regIndex = _regIndex.map(item => Math.abs(+item)); |
|
for(let i=0;i<str.length;i++){ |
|
if(i>=_regIndex[0]&&i<=_regIndex[1]){ |
|
result+=ARepText; |
|
} |
|
else{ |
|
result+=str[i]; |
|
} |
|
} |
|
result=regIndex.indexOf('-')===-1?result:result.split('').reverse().join(''); |
|
return result; |
|
}, |
|
/** |
|
* @description 加密字符串 |
|
*/ |
|
encryptStr(str, regIndex, ARepText = '*') { |
|
let regText = '', |
|
Reg = null, |
|
_regIndex = regIndex.split(','), |
|
replaceText = ARepText; |
|
_regIndex = _regIndex.map(item => +item); |
|
regText = '(\\w{' + _regIndex[0] + '})\\w{' + (1 + _regIndex[1] - _regIndex[0]) + '}'; |
|
Reg = new RegExp(regText); |
|
let replaceCount = replaceText.repeat((1 + _regIndex[1] - _regIndex[0])); |
|
return str.replace(Reg, '$1' + replaceCount); |
|
}, |
|
/** |
|
* @description 不加密字符串 |
|
*/ |
|
encryptUnStr(str, regIndex, ARepText = '*') { |
|
let regText = '', |
|
Reg = null, |
|
_regIndex = regIndex.split(','), |
|
replaceText = ARepText; |
|
_regIndex = _regIndex.map(item => +item); |
|
regText = '(\\w{' + _regIndex[0] + '})(\\w{' + (1 + _regIndex[1] - _regIndex[0]) + '})(\\w{' + (str.length - _regIndex[1] - 1) + '})'; |
|
Reg = new RegExp(regText); |
|
let replaceCount1 = replaceText.repeat(_regIndex[0]); |
|
let replaceCount2 = replaceText.repeat(str.length - _regIndex[1] - 1); |
|
return str.replace(Reg, replaceCount1 + '$2' + replaceCount2); |
|
}, |
|
/** |
|
* @description 字符串開始位置加密 |
|
*/ |
|
encryptStartStr(str, length, replaceText = '*'){ |
|
return this.encryptStr(str,'0,'+length,replaceText) |
|
}, |
|
/** |
|
* @description 字符串結束位置加密 |
|
*/ |
|
encryptEndStr(str, length, replaceText = '*'){ |
|
return this.encryptStartStr(str.split('').reverse().join(''), length, replaceText).split('').reverse().join(''); |
|
}, |
|
/** |
|
* @description 檢測字符串 |
|
*/ |
|
checkType(str, type){ |
|
return !!ruleData.checkType[type](str); |
|
}, |
|
/** |
|
* @description 檢測密碼強度 |
|
*/ |
|
checkPwdLevel(str,rules = [/[0-9]/, /[a-z]/, /[A-Z]/, /[\.|-|_]/]) { |
|
let nowLv = 0; |
|
for (let i = 0; i < rules.length; i++) { |
|
if (rules[i].test(str)) { |
|
nowLv++; |
|
} |
|
} |
|
return nowLv; |
|
}, |
|
/** |
|
* @description 隨機碼 |
|
*/ |
|
randomWord(count = 36) { |
|
return Math.random().toString(count).substring(2); |
|
}, |
|
/** |
|
* @description 統計特定字符串的次數 |
|
*/ |
|
countStr(str, strSplit) { |
|
return str.split(strSplit).length - 1 |
|
}, |
|
/** |
|
* @description 過濾特定類型字符串 |
|
*/ |
|
filterStr(str, type, replaceStr = ''){ |
|
let arr = Array.prototype.slice.call(arguments); |
|
let fnName = 'filter' + this.firstWordUpper(type); |
|
arr.splice(1, 1); |
|
return this[fnName] ? this[fnName].apply(null, arr) : false; |
|
}, |
|
/** |
|
* @description 過濾字符串的特殊符號 |
|
*/ |
|
filterSpecialStr(str, replaceStr = '', spStr){ |
|
let regText = '$()[]{}?\|^*+./\"\'+', pattern, _regText = "[^0-9A-Za-z\\s", nowStr; |
|
//是否有哪些特殊符號須要保留 |
|
if (spStr) { |
|
for (let j = 0, len = spStr.length; j < len; j++) { |
|
nowStr = ''; |
|
if (regText.indexOf(spStr[j]) === -1) { |
|
nowStr = '\\'; |
|
} |
|
_regText += nowStr + spStr[j]; |
|
} |
|
_regText += ']'; |
|
} |
|
else { |
|
_regText = "[^0-9A-Za-z\\s]"; |
|
} |
|
pattern = new RegExp(_regText, 'g'); |
|
return str = str.replace(pattern, replaceStr); |
|
}, |
|
/** |
|
* @description 過濾字符串的html標籤 |
|
*/ |
|
filterHtml(str, replaceStr = ''){ |
|
return str.replace(/<\/?[^>]*>/g, replaceStr); |
|
}, |
|
/** |
|
* @description 過濾字符串的表情 |
|
*/ |
|
filterEmjoy(str, replaceStr = ''){ |
|
return str.replace(/[^\u4e00-\u9fa5|\u0000-\u00ff|\u3002|\uFF1F|\uFF01|\uff0c|\u3001|\uff1b|\uff1a|\u3008-\u300f|\u2018|\u2019|\u201c|\u201d|\uff08|\uff09|\u2014|\u2026|\u2013|\uff0e]/ig, replaceStr); |
|
}, |
|
/** |
|
* @description 過濾字符串的大寫字母 |
|
*/ |
|
filterWordUpper(str, replaceStr = ''){ |
|
return str.replace(/[A-Z]/g, replaceStr); |
|
}, |
|
/** |
|
* @description 過濾字符串的小寫字母 |
|
*/ |
|
filterWordLower(str, replaceStr = ''){ |
|
return str.replace(/[a-z]/g, replaceStr); |
|
}, |
|
/** |
|
* @description 過濾字符串的數字 |
|
*/ |
|
filterNumber(str, replaceStr = ''){ |
|
return str.replace(/[1-9]/g, replaceStr); |
|
}, |
|
/** |
|
* @description 過濾字符串的中文 |
|
*/ |
|
filterChinese(str, replaceStr = ''){ |
|
return str.replace(/[\u4E00-\u9FA5]/g, replaceStr); |
|
}, |
|
/** |
|
* @description 格式化處理字符串 |
|
*/ |
|
formatText(str, size = 3, delimiter = ',') { |
|
let regText = '\\B(?=(\\w{' + size + '})+(?!\\w))'; |
|
let reg = new RegExp(regText, 'g'); |
|
return str.replace(reg, delimiter); |
|
}, |
|
/** |
|
* @description 找出最長單詞 |
|
* @param str |
|
* @param splitType |
|
* @return {{el: string, max: number}} |
|
*/ |
|
longestWord(str, splitType = /\s+/g) { |
|
let _max = 0, _item = ''; |
|
let strArr = str.split(splitType); |
|
strArr.forEach(item => { |
|
if (_max < item.length) { |
|
_max = item.length; |
|
_item = item; |
|
} |
|
}); |
|
return {el: _item, max: _max}; |
|
}, |
|
/** |
|
* @description 句中單詞首字母大寫 |
|
* @param str |
|
* @param splitType |
|
* @return {*} |
|
*/ |
|
titleCaseUp(str, splitType = /\s+/g) { |
|
//這個我也一直在糾結,英文標題,即便是首字母大寫,也未必每個單詞的首字母都是大寫的,可是又不知道哪些應該大寫,哪些不該該大寫 |
|
let strArr = str.split(splitType), |
|
result = ""; |
|
strArr.forEach(item => { |
|
result += this.firstWordUpper(item, 1) + ' '; |
|
}); |
|
return this.trimRight(result) |
|
}, |
|
//***************字符串模塊End**************************/ |
|
//***************數組模塊**************************/ |
|
/** |
|
* @description 數組去重 |
|
* @param arr |
|
* @return {[*]} |
|
*/ |
|
unique(arr) { |
|
return [...new Set(arr)] |
|
}, |
|
/** |
|
* @description 數組順序打亂 |
|
* @param arr |
|
* @return {Array.<T>} |
|
*/ |
|
upset(arr) { |
|
let j, _item; |
|
for (let i = 0; i < arr.length; i++) { |
|
j = Math.floor(Math.random() * i); |
|
_item = arr[i]; |
|
arr[i] = arr[j]; |
|
arr[j] = _item; |
|
} |
|
return arr; |
|
}, |
|
/** |
|
* @description 數組最大值(數值數組) |
|
* @param arr |
|
*/ |
|
max(arr) { |
|
return Math.max(...arr); |
|
}, |
|
/** |
|
* @description 數組最小值(數值數組) |
|
* @param arr |
|
*/ |
|
min(arr) { |
|
return Math.min(...arr); |
|
}, |
|
/** |
|
* @description 數組求和(數值數組) |
|
* @param arr |
|
*/ |
|
sum(arr) { |
|
return arr.reduce((pre, cur) => pre + cur) |
|
}, |
|
/** |
|
* @description 數組平均值,小數點可能會有不少位,這裏不作處理,處理了使用就不靈活了(數值數組) |
|
* @param arr |
|
*/ |
|
average(arr) { |
|
return this.sum(arr) / arr.length; |
|
}, |
|
/** |
|
* @description 深拷貝 |
|
* @param obj |
|
*/ |
|
clone(obj){ |
|
if (!obj && typeof obj !== 'object') { |
|
return; |
|
} |
|
let newObj = obj.constructor === Array ? [] : {}; |
|
for (let key in obj) { |
|
if (obj[key] && typeof obj[key] === 'object') { |
|
//遞歸 |
|
newObj[key] = this.clone(obj[key]); |
|
} else { |
|
newObj[key] = obj[key]; |
|
} |
|
} |
|
return newObj; |
|
}, |
|
/** |
|
* @description 從數組中隨機獲取元素 |
|
* @param arr |
|
* @param num |
|
* @return {*} |
|
*/ |
|
getRandom(arr, num = 1) { |
|
let _arr = this.clone(arr), nowIndex, result = []; |
|
for (let i = 0; i < num; i++) { |
|
nowIndex = Math.floor(Math.random() * _arr.length); |
|
result.push(_arr[nowIndex]); |
|
_arr.splice(nowIndex, 1); |
|
} |
|
return num > 1 ? result : result[0]; |
|
|
|
}, |
|
/** |
|
* @description 降序返回數組(字符串)每一個元素的出現次數 |
|
* @param arr |
|
* @param item |
|
* @return {Array|Number} |
|
*/ |
|
count(arr, item) { |
|
//是否只返回一個元素的次數 |
|
if (item) { |
|
let num = 0; |
|
for (let i = 0, len = obj.length; i < len; i++) { |
|
if (item === obj[i]) { |
|
num++; |
|
} |
|
} |
|
return num; |
|
} |
|
let obj = {}, k, arr1 = [] |
|
//記錄每一元素出現的次數 |
|
for (let i = 0, len = arr.length; i < len; i++) { |
|
k = arr[i]; |
|
if (obj[k]) { |
|
obj[k]++; |
|
} else { |
|
obj[k] = 1; |
|
} |
|
} |
|
//保存結果{el-'元素',count-出現次數} |
|
for (let o in obj) { |
|
arr1.push({el: o, count: obj[o]}); |
|
} |
|
return arr1; |
|
}, |
|
/** |
|
* @description 刪除值爲'val'的數組元素 |
|
* @param arr |
|
* @param val |
|
* @return {Array} |
|
*/ |
|
removeArrayByValue(arr, val) { |
|
return arr.filter(item => item !== val) |
|
}, |
|
/** |
|
* @description 刪除值含有'val'的數組元素 |
|
* @param arr |
|
* @param val |
|
* @return {Array} |
|
*/ |
|
removeArrayByLike(arr, val) { |
|
return arr.filter(item => item.indexOf(val) === -1); |
|
}, |
|
/** |
|
* @description 排除對象某些項 |
|
* @param obj |
|
* @param keys |
|
* @return {*} |
|
*/ |
|
filterKeys(obj, keys) { |
|
let _obj = {}; |
|
let _keys = keys.split(','); |
|
for (let key in obj) { |
|
//若是key不存在排除keys裏面,添加數據 |
|
if (_keys.indexOf(key) === -1) { |
|
_obj[key] = obj[key]; |
|
} |
|
} |
|
return _obj; |
|
}, |
|
/** |
|
* @description 對象數組排序 |
|
* @param arr |
|
* @param sortText |
|
* @return {*} |
|
*/ |
|
sortBy(arr, sortText) { |
|
if (!sortText) { |
|
return arr |
|
} |
|
let _sortText = sortText.split(',').reverse(), _arr = arr.slice(0); |
|
for (let i = 0, len = _sortText.length; i < len; i++) { |
|
_arr.sort((n1, n2) => { |
|
return n1[_sortText[i]] - n2[_sortText[i]] |
|
}) |
|
} |
|
return _arr; |
|
}, |
|
/** |
|
* @description 數組扁平化 |
|
* @param arr |
|
* @return {Array} |
|
*/ |
|
steamroller(arr) { |
|
let flattened = [].concat(...arr); |
|
return flattened.some(item => Array.isArray(item)) ? this.steamroller(flattened) : flattened; |
|
}, |
|
/** |
|
* @description 分割數組 |
|
* @param arr |
|
* @param num |
|
* @return {Array} |
|
*/ |
|
cut(arr, num){ |
|
let result = []; |
|
for (let i = 0; i < arr.length; i += num) { |
|
result.push(arr.slice(i, i + num)) |
|
} |
|
return result; |
|
}, |
|
//***************數組模塊END**************************/ |
|
|
|
|
|
//***************對象及其餘模塊**************************/ |
|
/** |
|
* @description 適配rem |
|
* @param _client 效果圖的寬度 |
|
*/ |
|
getFontSize(_client) { |
|
let doc = document, |
|
win = window; |
|
let docEl = doc.documentElement, |
|
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', |
|
countSize = function () { |
|
let clientWidth = docEl.clientWidth; |
|
if (!clientWidth) return; |
|
//若是屏幕大於750(750是根據我效果圖設置的,具體數值參考效果圖),就設置clientWidth=750,防止font-size會超過100px |
|
if (clientWidth > _client) { |
|
clientWidth = _client |
|
} |
|
//設置根元素font-size大小 |
|
docEl.style.fontSize = 100 * (clientWidth / _client) + 'px'; |
|
}; |
|
//屏幕大小改變,或者橫豎屏切換時,觸發函數 |
|
win.addEventListener(resizeEvt, countSize, false); |
|
//文檔加載完成時,觸發函數 |
|
doc.addEventListener('DOMContentLoaded', countSize, false); |
|
}, |
|
/** |
|
* @description 到某一個時間的倒計時 |
|
* @param endTime |
|
* @return {{d: number, h: number, m: number, s: number}} |
|
*/ |
|
getEndTime(endTime) { |
|
let t = +new Date(endTime) - +new Date(); //時間差的毫秒數 |
|
let d = 0, |
|
h = 0, |
|
m = 0, |
|
s = 0; |
|
if (t >= 0) { |
|
d = Math.floor(t / 1000 / 3600 / 24); |
|
h = Math.floor(t / 1000 / 60 / 60 % 24); |
|
m = Math.floor(t / 1000 / 60 % 60); |
|
s = Math.floor(t / 1000 % 60); |
|
} |
|
return {d, h, m, s}; |
|
}, |
|
/** |
|
* @description 時間格式化 |
|
* @param date |
|
* @param fmt |
|
* @return {*} |
|
*/ |
|
formatDate(date, fmt = 'yyyy-MM-dd hh:mm:ss') { |
|
let _date = new Date(date), _fmt = fmt; |
|
let o = { |
|
"M+": _date.getMonth() + 1, |
|
"d+": _date.getDate(), |
|
"h+": _date.getHours(), |
|
"m+": _date.getMinutes(), |
|
"s+": _date.getSeconds() |
|
}; |
|
if (/(y+)/.test(_fmt)) { |
|
_fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + "").substr(4 - RegExp.$1.length)); |
|
} |
|
for (let k in o) { |
|
if (new RegExp("(" + k + ")").test(_fmt)) { |
|
_fmt = _fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); |
|
} |
|
} |
|
return _fmt; |
|
}, |
|
/** |
|
* @description 返回某一個月份的最後一天的日期 |
|
* @param month 默認是當前月份 |
|
* @param year 默認是當前年份 |
|
* @return string yyyy/mm/dd |
|
*/ |
|
getDayByMonth(month = new Date().getMonth() + 1, year = new Date().getFullYear()){ |
|
return new Date(year, month, 0).toLocaleDateString(); |
|
}, |
|
/** |
|
* @description 隨機產生顏色 |
|
* @return {string} |
|
*/ |
|
randomColor(sum) { |
|
if (sum) { |
|
return '#' + Math.random().toString(16).substring(2).substr(0, 6); |
|
} |
|
else { |
|
return 'rgb(' + this.randomNumber(255) + ',' + this.randomNumber(255) + ',' + this.randomNumber(255) + ')'; |
|
} |
|
}, |
|
/** |
|
* @description 隨機返回一個範圍的數字 |
|
* @param n1 |
|
* @param n2 |
|
* @return {number} |
|
*/ |
|
randomNumber(n1, n2) { |
|
switch (arguments.length) { |
|
case 2: |
|
return Math.round(n1 + Math.random() * (n2 - n1)); |
|
case 1: |
|
return Math.round(Math.random() * n1); |
|
default: |
|
return Math.round(Math.random() * 100000000); |
|
} |
|
}, |
|
/** |
|
* @description 設置url參數 |
|
* @param url |
|
* @param obj |
|
* @return {string} |
|
*/ |
|
setUrlParam(url, obj) { |
|
let _rs = []; |
|
for (let p in obj) { |
|
if (obj[p] !== null && obj[p] !== '' && obj[p] !== undefined) { |
|
_rs.push(p + '=' + obj[p]) |
|
} |
|
} |
|
return url + '?' + _rs.join('&'); |
|
}, |
|
/** |
|
* @description 獲取url參數 |
|
* @param url |
|
* @return {Object} |
|
*/ |
|
getUrlParam(url = window.location.href) { |
|
let _param = url.substring(url.indexOf('?') + 1).split('&'), |
|
_rs = {}, pos; |
|
for (let i = 0, _len = _param.length; i < _len; i++) { |
|
pos = _param[i].split('='); |
|
if (pos.length === 2) { |
|
_rs[pos[0]] = pos[1]; |
|
} |
|
} |
|
return _rs; |
|
}, |
|
/** |
|
* @description 現金額大寫轉換函數 |
|
* @param n |
|
* @return {string} |
|
*/ |
|
upDigit(n) { |
|
let fraction = ['角', '分', '釐']; |
|
let digit = ['零', '壹', '貳', '叄', '肆', '伍', '陸', '柒', '捌', '玖']; |
|
let unit = [ |
|
['元', '萬', '億'], |
|
['', '拾', '佰', '仟'] |
|
]; |
|
let head = n < 0 ? '欠人民幣' : '人民幣'; |
|
n = Math.abs(n); |
|
let s = ''; |
|
for (let i = 0; i < fraction.length; i++) { |
|
s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ''); |
|
} |
|
s = s || '整'; |
|
n = Math.floor(n); |
|
for (let i = 0; i < unit[0].length && n > 0; i++) { |
|
let p = ''; |
|
for (let j = 0; j < unit[1].length && n > 0; j++) { |
|
p = digit[n % 10] + unit[1][j] + p; |
|
n = Math.floor(n / 10); |
|
} |
|
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s; |
|
//s = p + unit[0][i] + s; |
|
} |
|
return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整'); |
|
}, |
|
/** |
|
* @description 清除對象中值爲空的屬性 |
|
* @param obj |
|
* @param keepValues |
|
* @return {{}} |
|
*/ |
|
clearKeys(obj, keepValues = [0, false]) { |
|
keepValues.forEach((item, index) => { |
|
keepValues[index] = Number.isNaN(item) ? 'NaN' : item |
|
}); |
|
let _newPar = {}; |
|
for (let key in obj) { |
|
if (checkValue(obj[key], keepValues) || (obj[key] && obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== '')) { |
|
_newPar[key] = obj[key]; |
|
} |
|
} |
|
return _newPar; |
|
}, |
|
/** |
|
* @description 設置對象中值爲空的屬性的默認值 |
|
* @param obj |
|
* @param fillValues |
|
* @param val |
|
* @return {{}} |
|
*/ |
|
fillKeys(obj, fillValues = [null, undefined, ''], val = '--') { |
|
fillValues.forEach((item, index) => { |
|
fillValues[index] = Number.isNaN(item) ? 'NaN' : item |
|
}); |
|
let _newPar = {}; |
|
for (let key in obj) { |
|
_newPar[key] = checkValue(obj[key], fillValues) ? val : obj[key]; |
|
} |
|
return _newPar; |
|
}, |
|
/** |
|
* @description 數據類型判斷 |
|
* @param o |
|
* @param type |
|
* @return {*} |
|
*/ |
|
isType(o, type) { |
|
if (!type) { |
|
//return Object.prototype.toString.call(o).split(/\s/)[1].replace(']','') |
|
return Object.prototype.toString.call(o).match(/\s(.*)]/)[1] |
|
} |
|
let _types = type.toLowerCase().split(','); |
|
let typeObj = { |
|
'string': '[object String]', |
|
'number': '[object Number]', |
|
'boolean': '[object Boolean]', |
|
'null': '[object Null]', |
|
'function': '[object Function]', |
|
'array': '[object Array]', |
|
'object': '[object Object]', |
|
'symbol': '[object Symbol]' |
|
} |
|
let typeFn = { |
|
nan(){ |
|
return Number.isNaN(o); |
|
}, |
|
elements(){ |
|
return Object.prototype.toString.call(o).indexOf('HTML') !== -1; |
|
} |
|
} |
|
let _result; |
|
for (let item of _types) { |
|
if (typeObj[item]) { |
|
_result = Object.prototype.toString.call(o) === typeObj[item]; |
|
} |
|
else { |
|
_result = typeFn[item](); |
|
} |
|
if (_result) { |
|
return _result; |
|
} |
|
} |
|
return false; |
|
}, |
|
/** |
|
* @description 手機類型判斷 |
|
* @param type |
|
* @return {*} |
|
*/ |
|
getBrowserInfo(type) { |
|
let typeObj = { |
|
android: 'android', |
|
iphone: 'iphone', |
|
ipad: 'ipad', |
|
weixin: 'micromessenger' |
|
} |
|
return type ? navigator.userAgent.toLowerCase().indexOf(typeObj[type]) !== -1 : navigator.userAgent.toLowerCase(); |
|
}, |
|
/** |
|
* @description 函數節流 |
|
* @param fn 執行的函數 |
|
* @param delay 延遲的時間 |
|
* @param option 配置項 {first:true,last:true} |
|
*/ |
|
throttle(fn, delay, option = {}) { |
|
option = Object.assign({first: true, last: true}, option); |
|
let timer = null; |
|
let t_start = 0; |
|
return function () { |
|
let _this = this, args = arguments, t_cur = +new Date(); |
|
//先清理上一次的調用觸發(上一次調用觸發事件不執行) |
|
clearTimeout(timer); |
|
//首次觸發 |
|
if (!option.first && t_start === 0) { |
|
//fn.apply(_this, args); |
|
t_start = t_cur; |
|
} |
|
//若是當前時間-觸發時間大於等於的間隔時間(delay),觸發一次函數運行函數 |
|
if (t_cur - t_start >= delay) { |
|
fn.apply(_this, args); |
|
t_start = t_cur; |
|
} |
|
//最後一次 |
|
else { |
|
timer = setTimeout(() => { |
|
option.last && fn.apply(_this, args); |
|
t_start = 0; |
|
}, delay); |
|
} |
|
}; |
|
}, |
|
/** |
|
* @description 函數防抖(在事件被觸發n毫秒後再執行回調,若是在這n毫秒內又被觸發,則從新計時) |
|
* @param fn 執行函數 |
|
* @param delay 間隔時間 |
|
* @param first 間隔時間 |
|
*/ |
|
debounce(fn, delay, first = false){ |
|
let _first = first; |
|
return function () { |
|
clearTimeout(fn.id); |
|
if (_first) { |
|
fn.call(this, arguments); |
|
_first = !_first; |
|
} |
|
fn.id = setTimeout(() => { |
|
fn.call(this, arguments) |
|
}, delay) |
|
} |
|
}, |
|
//***************對象及其餘模塊END**************************/ |
|
//***************cookie模塊*******************************/ |
|
/** |
|
* @description 設置cookie |
|
*/ |
|
setCookie(name, val, iDay){ |
|
let oDate = new Date(); |
|
oDate.setDate(oDate.getDate() + iDay); |
|
document.cookie = name + '=' + val + ';expires=' + oDate; |
|
}, |
|
/** |
|
* @description 獲取cookie |
|
*/ |
|
getCookie(name){ |
|
let arr = document.cookie.split('; '), arr2; |
|
for (let i = 0; i < arr.length; i++) { |
|
arr2 = arr[i].split('='); |
|
if (arr2[0] === name) { |
|
return arr2[1]; |
|
} |
|
} |
|
return ''; |
|
}, |
|
/** |
|
* @description 刪除cookie |
|
*/ |
|
removeCookie(name){ |
|
this.setCookie(name, 1, -1); |
|
}, |
|
/** |
|
* @description 操做cookie |
|
*/ |
|
cookie(name, val, iDay){ |
|
if (arguments.length === 1) { |
|
return this.getCookie(name); |
|
} |
|
else { |
|
this.setCookie(name, val, iDay); |
|
} |
|
}, |
|
//***************cookie模塊END*******************************/ |
|
}; |
|
})(); |