var ecDo = {
trim: function (str, type) {
switch (type) {
case 1:
return str.replace(/\s+/g, "");
case 2:
return str.replace(/(^\s*)|(\s*$)/g, "");
case 3:
return str.replace(/(^\s*)/g, "");
case 4:
return str.replace(/(\s*$)/g, "");
default:
return str;
}
},
changeCase: function (str, type) {
function ToggleCase(str) {
var itemText = ""
str.split("").forEach(
function (item) {
if (/^([a-z]+)/.test(item)) {
itemText += item.toUpperCase();
} else if (/^([A-Z]+)/.test(item)) {
itemText += item.toLowerCase();
} else {
itemText += item;
}
});
return itemText;
}
switch (type) {
case 1:
return str.replace(/\b\w+\b/g, function (word) {
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
});
case 2:
return str.replace(/\b\w+\b/g, function (word) {
return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
});
case 3:
return ToggleCase(str);
case 4:
return str.toUpperCase();
case 5:
return str.toLowerCase();
default:
return str;
}
},
repeatStr: function (str, count) {
var text = '';
for (var i = 0; i < count; i++) {
text += str;
}
return text;
},
replaceAll: function (str, AFindText, ARepText) {
raRegExp = new RegExp(AFindText, "g");
return str.replace(raRegExp, ARepText);
},
replaceStr: function (str, regArr, type, ARepText) {
var regtext = '',
Reg = null,
replaceText = ARepText || '*';
if (regArr.length === 3 && type === 0) {
regtext = '(\\w{' + regArr[0] + '})\\w{' + regArr[1] + '}(\\w{' + regArr[2] + '})'
Reg = new RegExp(regtext);
var replaceCount = this.repeatStr(replaceText, regArr[1]);
return str.replace(Reg, '$1' + replaceCount + '$2')
}
else if (regArr.length === 3 && type === 1) {
regtext = '\\w{' + regArr[0] + '}(\\w{' + regArr[1] + '})\\w{' + regArr[2] + '}'
Reg = new RegExp(regtext);
var replaceCount1 = this.repeatStr(replaceText, regArr[0]);
var replaceCount2 = this.repeatStr(replaceText, regArr[2]);
return str.replace(Reg, replaceCount1 + '$1' + replaceCount2)
}
else if (regArr.length === 1 && type == 0) {
regtext = '(^\\w{' + regArr[0] + '})'
Reg = new RegExp(regtext);
var replaceCount = this.repeatStr(replaceText, regArr[0]);
return str.replace(Reg, replaceCount)
}
else if (regArr.length === 1 && type == 1) {
regtext = '(\\w{' + regArr[0] + '}$)'
Reg = new RegExp(regtext);
var replaceCount = this.repeatStr(replaceText, regArr[0]);
return str.replace(Reg, replaceCount)
}
},
checkType: function (str, type) {
switch (type) {
case 'email':
return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
case 'phone':
return /^1[3|4|5|7|8][0-9]{9}$/.test(str);
case 'tel':
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
case 'number':
return /^[0-9]$/.test(str);
case 'english':
return /^[a-zA-Z]+$/.test(str);
case 'text':
return /^\w+$/.test(str);
case 'chinese':
return /^[\u4E00-\u9FA5]+$/.test(str);
case 'lower':
return /^[a-z]+$/.test(str);
case 'upper':
return /^[A-Z]+$/.test(str);
default:
return true;
}
},
checkPwd: function (str) {
var nowLv = 0;
if (str.length < 6) {
return nowLv
}
if (/[0-9]/.test(str)) {
nowLv++
}
if (/[a-z]/.test(str)) {
nowLv++
}
if (/[A-Z]/.test(str)) {
nowLv++
}
if (/[\.|-|_]/.test(str)) {
nowLv++
}
return nowLv;
},
randomWord: function (count) {
return Math.random().toString(count).substring(2);
},
countStr: function (str, strSplit) {
return str.split(strSplit).length - 1
},
removeRepeatArray: function (arr) {
return arr.filter(function (item, index, self) {
return self.indexOf(item) == index;
});
},
upsetArr: function (arr) {
return arr.sort(function () {
return Math.random() - 0.5
});
},
maxArr: function (arr) {
return Math.max.apply(null, arr);
},
minArr: function (arr) {
return Math.min.apply(null, arr);
},
sumArr: function (arr) {
return arr.reduce(function(pre,cur){return pre+cur})
},
covArr: function (arr) {
return this.sumArr(arr) / arr.length;
},
randomOne: function (arr) {
return arr[Math.floor(Math.random() * arr.length)];
},
getEleCount: function (obj, ele) {
var num = 0;
for (var i = 0, len = obj.length; i < len; i++) {
if (ele == obj[i]) {
num++;
}
}
return num;
},
getCount: function (arr, rank, ranktype) {
var obj = {},
k, arr1 = []
for (var i = 0, len = arr.length; i < len; i++) {
k = arr[i];
if (obj[k]) {
obj[k]++;
} else {
obj[k] = 1;
}
}
for (var o in obj) {
arr1.push({el: o, count: obj[o]});
}
arr1.sort(function (n1, n2) {
return n2.count - n1.count
});
if (ranktype === 1) {
arr1 = arr1.reverse();
}
var rank1 = rank || arr1.length;
return arr1.slice(0, rank1);
},
getArrayNum: function (arr, n1, n2) {
var arr1=arr.slice(n1,n2);
return arr1;
},
removeArrayForValue: function (arr, val, type) {
arr.filter(function (item) {
return type === '%' ? item.indexOf(val) !== -1 : item !== val
})
},
getFontSize: function () {
var doc = document,
win = window;
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if (clientWidth > 750) {
clientWidth = 750
}
docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
};
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
},
getEndTime: function (endTime) {
var startDate = new Date();
var endDate = new Date(endTime);
var t = endDate.getTime() - startDate.getTime();
var 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 + " 秒";
},
randomColor: function () {
return '#' + Math.random().toString(16).substring(2).substr(0, 6);
},
randomNumber: function (n1, n2) {
if (arguments.length === 2) {
return Math.round(n1 + Math.random() * (n2 - n1));
}
else if (arguments.length === 1) {
return Math.round(Math.random() * n1)
}
else {
return Math.round(Math.random() * 255)
}
},
setUrlPrmt: function (obj) {
var _rs = [];
for (var p in obj) {
if (obj[p] != null && obj[p] != '') {
_rs.push(p + '=' + obj[p])
}
}
return _rs.join('&');
},
getUrlPrmt: function (url) {
url = url ? url : window.location.href;
var _pa = url.substring(url.indexOf('?') + 1),
_arrS = _pa.split('&'),
_rs = {};
for (var i = 0, _len = _arrS.length; i < _len; i++) {
var pos = _arrS[i].indexOf('=');
if (pos == -1) {
continue;
}
var name = _arrS[i].substring(0, pos),
value = window.decodeURIComponent(_arrS[i].substring(pos + 1));
_rs[name] = value;
}
return _rs;
},
upDigit: function (n) {
var fraction = ['角', '分', '厘'];
var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
var unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟']
];
var head = n < 0 ? '欠人民币' : '人民币';
n = Math.abs(n);
var s = '';
for (var 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 (var i = 0; i < unit[0].length && n > 0; i++) {
var p = '';
for (var 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;
}
return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
},
filterParams: function (obj) {
var _newPar = {};
for (var key in obj) {
if ((obj[key] === 0 || obj[key]) && obj[key].toString().replace(/(^\s*)|(\s*$)/g, '') !== '') {
_newPar[key] = obj[key];
}
}
return _newPar;
},
setCookie: function (name, value, iDay) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + iDay);
document.cookie = name + '=' + value + ';expires=' + oDate;
},
getCookie: function (name) {
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('=');
if (arr2[0] == name) {
return arr2[1];
}
}
return '';
},
removeCookie: function (name) {
this.setCookie(name, 1, -1);
},
hasClass: function (obj, classStr) {
if(obj.className&&this.trim(obj.className,1)!==""){
var arr = obj.className.split(/\s+/);
return (arr.indexOf(classStr) == -1) ? false : true;
}
else{
return false;
}
},
addClass: function (obj, classStr) {
if((this.istype(obj,'array')||this.istype(obj,'elements'))&&obj.length>=1){
for(var i=0,len=obj.length;i<len;i++){
if (!this.hasClass(obj[i], classStr)) {
obj[i].className += " " + classStr;
}
}
}
else{
if (!this.hasClass(obj, classStr)) {
obj.className += " " + classStr;
}
}
},
removeClass: function (obj, classStr) {
if((this.istype(obj,'array')||this.istype(obj,'elements'))&&obj.length>1){
for(var i=0,len=obj.length;i<len;i++){
if (this.hasClass(obj[i], classStr)) {
var reg = new RegExp('(\\s|^)' + classStr + '(\\s|$)');
obj[i].className = obj[i].className.replace(reg, '');
}
}
}
else{
if (this.hasClass(obj, classStr)) {
var reg = new RegExp('(\\s|^)' + classStr + '(\\s|$)');
obj.className = obj.className.replace(reg, '');
}
}
},
replaceClass: function (obj, newName, oldName) {
this.removeClass(obj, oldName);
this.addClass(obj, newName);
},
siblings: function (obj,opt) {
var a = [];
var p = obj.previousSibling;
while (p) {
if (p.nodeType === 1) {
a.push(p);
}
p = p.previousSibling
}
a.reverse()
var n = obj.nextSibling;
while (n) {
if (n.nodeType === 1) {
a.push(n);
}
n = n.nextSibling;
}
if(opt){
var b=[];
if(opt[0]==='.'){
b=a.filter(function(item){return item.className===opt});
}
else if(opt[0]==='#'){
b=a.filter(function(item){return item.id===opt});
}
else{
b=a.filter(function(item){return item.tagName.toLowerCase()===opt});
}
return b;
}
return a;
},
css: function (obj, json) {
for (var attr in json) {
obj.style[attr] = json[attr];
}
},
html: function (obj) {
if (arguments.length == 0) {
return this.innerHTML;
} else if (arguments.length == 1) {
this.innerHTML = arguments[0];
}
},
show: function (obj) {
obj.style.display = "";
},
hide: function (obj) {
obj.style.display = "none";
},
ajax: function (obj) {
obj = obj || {};
obj.type = obj.type.toUpperCase() || 'POST';
obj.url = obj.url || '';
obj.async = obj.async || true;
obj.data = obj.data || null;
obj.success = obj.success || function () {
};
obj.success = obj.error || function () {
};
var xmlHttp = null;
if (XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
var params = [];
for (var key in obj.data) {
params.push(key + '=' + obj.data[key]);
}
var postData = params.join('&');
if (obj.type.toUpperCase() === 'POST') {
xmlHttp.open(obj.type, obj.url, obj.async);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
xmlHttp.send(postData);
} else if (obj.type.toUpperCase() === 'GET') {
xmlHttp.open(obj.type, obj.url + '?' + postData, obj.async);
xmlHttp.send(null);
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
obj.success(xmlHttp.responseText);
} else {
obj.error(xmlHttp.responseText);
}
};
},
getOptionArray: function (arr, keys, type) {
var newArr = []
if (!keys) {
return arr
}
if (type === 1) {
for (var i = 0, len = arr.length; i < len; i++) {
newArr.push(arr[i][keys])
}
return newArr;
}
var _keys = keys.split(','), newArrOne = {};
for (var i = 0, len = arr.length; i < len; i++) {
newArrOne = {};
for (var j = 0, len1 = _keys.length; j < len1; j++) {
newArrOne[_keys[j]] = arr[i][_keys[j]]
}
newArr.push(newArrOne);
}
return newArr
},
filterOptionArray: function (arr, keys) {
var newArr = []
var _keys = keys.split(','), newArrOne = {};
for (var i = 0, len = arr.length; i < len; i++) {
newArrOne = {};
for (var key in arr[i]) {
if (_keys.indexOf(key) === -1) {
newArrOne[key] = arr[i][key];
}
}
newArr.push(newArrOne);
}
return newArr
},
aftLoadImg: function (obj, url, cb) {
var oImg = new Image(),_this=this;
oImg.src = url;
oImg.onload = function () {
obj.src = oImg.src;
if (cb && _this.istype(cb, 'function')) {
cb(obj);
}
}
},
loadImg: function (className, num) {
var _className = className || 'ec-load-img', _num = num || 0,_this=this;
var oImgLoad = document.getElementsByClassName(_className);
for (var i = 0, len = oImgLoad.length; i < len; i++) {
if (document.documentElement.clientHeight + document.body.scrollTop > oImgLoad[i].offsetTop - _num && !oImgLoad[i].isLoad) {
oImgLoad[i].isLoad = true;
oImgLoad[i].style.cssText = "transition: ''; opacity: 0;"
if (oImgLoad[i].dataset) {
this.aftLoadImg(oImgLoad[i], oImgLoad[i].dataset.src, function (o) {
setTimeout(function () {
if (o.isLoad) {
_this.removeClass(o, _className);
o.style.cssText = "";
}
}, 1000)
});
} else {
this.aftLoadImg(oImgLoad[i], oImgLoad[i].getAttribute("data-src"), function (o) {
setTimeout(function () {
if (o.isLoad) {
_this.removeClass(o, _className);
o.style.cssText = "";
}
}, 1000)
});
}
(function (i) {
setTimeout(function () {
oImgLoad[i].style.cssText = "transition:all 1s; opacity: 1;";
}, 16)
})(i);
}
}
},
arraySort: function (arr, sortText) {
if (!sortText) {
return arr
}
var _sortText = sortText.split(',').reverse(), _arr = arr.slice(0);
for (var i = 0, len = _sortText.length; i < len; i++) {
_arr.sort(function (n1, n2) {
return n1[_sortText[i]] - n2[_sortText[i]]
})
}
return _arr;
},
steamroller: function (arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
newArr.push.apply(newArr, steamroller(arr[i]));
} else {
newArr.push(arr[i]);
}
}
return newArr;
},
createKeyExp: function (strArr) {
var str = "";
for (var i = 0; i < strArr.length; i++) {
if (i != strArr.length - 1) {
str = str + strArr[i] + "|";
} else {
str = str + strArr[i];
}
}
return "(" + str + ")";
},
findKey: function (str, key, el) {
var arr = null,
regStr = null,
content = null,
Reg = null,
_el = el || 'span';
arr = key.split(/\s+/);
regStr = this.createKeyExp(arr);
content = str;
Reg = new RegExp(regStr, "g");
content = content;
content=content.replace(/<\/?[^>]*>/g, '')
return content.replace(Reg, "<" + _el + ">$1</" + _el + ">");
},
istype: function (o, type) {
if(_type){
var _type = type.toLowerCase();
}
switch (_type) {
case 'string':
return Object.prototype.toString.call(o) === '[object String]';
case 'number':
return Object.prototype.toString.call(o) === '[object Number]';
case 'boolean':
return Object.prototype.toString.call(o) === '[object Boolean]';
case 'undefined':
return Object.prototype.toString.call(o) === '[object Undefined]';
case 'null':
return Object.prototype.toString.call(o) === '[object Null]';
case 'function':
return Object.prototype.toString.call(o) === '[object Function]';
case 'array':
return Object.prototype.toString.call(o) === '[object Array]';
case 'object':
return Object.prototype.toString.call(o) === '[object Object]';
case 'nan':
return isNaN(o);
case 'elements':
return Object.prototype.toString.call(o).indexOf('HTML')!==-1
default:
return Object.prototype.toString.call(o)
}
},
longestWord: function (str, splitType) {
var _splitType = splitType || /\s+/g,
_max = 0;
var strArr = str.split(_splitType);
strArr.forEach(function (item) {
if (_max < item.length) {
_max = item.length
}
})
return _max;
},
titleCaseUp: function (str, splitType) {
var _splitType = splitType || /\s+/g;
var strArr = str.split(_splitType),
result = "",_this=this
strArr.forEach(function (item) {
result += _this.changeCase(item, 1) + ' ';
})
return this.trim(result, 4)
},
browserInfo: function (type) {
switch (type) {
case 'android':
return navigator.userAgent.toLowerCase().indexOf('android') !== -1
case 'iphone':
return navigator.userAgent.toLowerCase().indexOf('iphone') !== -1
case 'ipad':
return navigator.userAgent.toLowerCase().indexOf('ipad') !== -1
case 'weixin':
return navigator.userAgent.toLowerCase().indexOf('MicroMessenger') !== -1
default:
return navigator.userAgent.toLowerCase()
}
},
filterStr:function(str,type,restr,spstr){
var typeArr=type.split(','),_str=str;
for(var i=0,len=typeArr.length;i<len;i++){
if(typeArr[i]==='special'){
var pattern,regText='$()[]{}?\|^*+./\"\'+';
if(spstr){
var _spstr=spstr.split(""),_regText="[^0-9A-Za-z\\s";
for(var i=0,len=_spstr.length;i<len;i++){
if(regText.indexOf(_spstr[i])===-1){
_regText+=_spstr[i];
}
else{
_regText+='\\'+_spstr[i];
}
}
_regText+=']'
pattern = new RegExp(_regText,'g');
}
else{
pattern = new RegExp("[^0-9A-Za-z\\s]",'g')
}
}
var _restr=restr||'';
switch(typeArr[i]){
case 'special': _str=_str.replace(pattern,_restr);break;
case 'html': _str=_str.replace(/<\/?[^>]*>/g, _restr);break;
case 'emjoy': _str=_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]/g,_restr);break;
case 'word': _str=_str.replace(/[a-z]/g,_restr);break;
case 'WORD': _str=_str.replace(/[A-Z]/g,_restr);break;
case 'number':_str= _str.replace(/[0-9]/g,_restr);break;
case 'chinese': _str=_str.replace(/[\u4E00-\u9FA5]/g,_restr);break;
}
}
return _str;
}
}