-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_colection.js
198 lines (185 loc) · 5 KB
/
func_colection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//ready
function ready(callBack){
var bReady = true;
if(document.addEventListener){
document.addEventListener('DOMContentLoaded',function(){
if(!bReady) return;
bReady = false;
callBack && callBack();
},false);
document.addEventListener('load',function(){
if(!bReady) return;
bReady = false;
callBack && callBack();
},false);
}else if(document.attachEvent){
document.attachEvent('onload',function(){
if(!bReady) return;
bReady = false;
callBack && callBack();
});
document.attachEvent('onreadystatechange',function(){
if(!bReady) return;
if(document.readyState=='complete'){
bReady = false;
callBack && callBack();
};
});
if(!window.frameElement) next();
function next(){
if(!bReady) return;
try{
document.documentElement.doScroll('left');
bReady = false;
callBack && callBack();
}catch(e){
setTimeout(next,1);
};
};
};
};
//other ready
function onReady(fn) {
var readyState = document.readyState;
if (readyState === 'interactive' || readyState === 'complete') {
fn();
} else {
window.addEventListener('DOMContentLoaded', fn);
}
}
function onReady(fn) {
var readyState = document.readyState;
if (readyState === 'interactive' || readyState === 'complete') {
setTimeout(fn, 0);
} else {
window.addEventListener('DOMContentLoaded', fn);
}
}
function onReadyPromise() {
return new Promise(function (resolve, reject) {
var readyState = document.readyState;
if (readyState === 'interactive' || readyState === 'complete') {
resolve();
} else {
window.addEventListener('DOMContentLoaded', resolve);
}
});
}
onReadyPromise().then(function () {
console.log('DOM fully loaded and parsed');
});
console.log('==Starting==');
//addEvent
function addEvent(target,type,handler){
if(target.addEventListener){
target.addEventListener(type,handler,false);
}else{
target.attachEvent("on"+type,function(event){
return handler.call(target,event);
});
}
}
//removeEvent
function removeEvent(target, type, handler){
target.removeEventListener ? target.removeEventListener(type, handler, false) : target.detachEvent("on" + type, function(event){
return handler.call(target,event);
});
}
//cancelHandler
function prevent(event){
var event = event || window.event;
if(event.preventDefault) event.preventDefault();
if(event.returnValue) event.returnValue = false;
return false;
}
//stopBubble
function stopBubble(event) {
if ( event && event.stopPropagation ) event.stopPropagation();
else window.event.cancelBubble = true;
}
//handle URL
function getQueryStringSearch(){
var qs = (location.search.length > 0 ? location.search.substring(1) : "");
var args = {};
var items = qs.length ? qs.split("&") : [];
var item = null;
var name = null;
var value = null;
for(var i = 0 , len = items.length ; i < len ; ++i){
item = items[i].split("=");
name = decodeURIComponent(item[0]);
value = decodeURIComponent(item[1]);
if(name.length){
args[name] = value;
}
}
return args;
}
//英文单词占1B,中文2B
function getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (val[i].match(/[^x00-xff]/ig) != null) //全角
len += 2;
else
len += 1;
};
return len;
}
//得到当前日期前后delta天的日期
function getFormatTimeAfterDay(delta){
var date = new Date();
if(delta){
date.setTime(date.getTime()+delta*(1000*60*60*24));
}
var formatYear = date.getFullYear();
var formatMonth = date.getMonth()+1;
if(formatMonth<10) formatMonth = "0" + formatMonth;
var formatDate = date.getDate();
if(formatDate<10) formatDate = "0" + formatDate;
var formatTime = formatYear + "-" + formatMonth + "-" + formatDate;
return formatTime;
}
//判断日期是否合法
function isLegalDate(year,month,date){
var monthList = [31,28,31,30,31,30,31,31,30,31,30,31];
if(arguments.length<3){
return false;
}
for(var len=arguments.length-1 ; len>=0 ; --len){
if(arguments[len] <= 0){
return false;
}
}
if(year%4===0 && year%100!==0 || year%400 == 0){
monthList[1]=29;
}
if(month <= 12 && date <= monthList[month-1]){
return true;
}
return false;
}
//驼峰转下划线
var str = "selectHomeClass";
function formatStr(str){
var reg = /([A-Z])/g;
return str.replace(reg,function(re){
return "_"+re.toLowerCase();
});
}
function formatStr2(str){
return str.replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();
}
console.log(formatStr(str));
console.log(formatStr2(str));
//结尾是否出现了循环模式
function f(str){
var res = str.match(/(([01]+?)[01]*?)\1+\2$/);
return {
src: str,
tail: res[0],
sub: res[1],
len: res[0].length / res[1].length
}
}
console.log(f('11111111101001001001001001'));