-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
70 lines (70 loc) · 1.91 KB
/
utils.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
var addEvent = (function() {
if (window.addEventListener) {
return function(el, type, fn, capture) {
el.addEventListener(type, function(e) {
fn.call(el, e);
}, capture);
}
} else if (window.attachEvent) {
return function(el, type, fn, capture) {
el.attachEvent("on"+type, function(e) {
fn.call(el, e);
})
}
}
}());
/*
* return a random integer
* include start, include end
*/
var random = function (start, end) {
var res = end - start + 1;
return Math.floor( Math.random()*res ) + start;
}
/*
* return a random string
* the length depending on your argument
*/
var randomString = function (num) {
var result = "",
str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
len = str.length - 1;
while(num--) {
result += str.charAt(random( 0, len ));
}
return result;
};
/*
* return an array
* the length depending on your argument
*/
function currentToFutureDate(n) {
var date = +new Date();
var result = [];
for (var i = 0; i < n; i++) {
result.push(new Date(date+8.64e7*i).toLocaleDateString())
}
return result;
}
/*
* deep clone
*/
function deepClone (obj) {
var dist;
if( typeof obj !== "object" || obj === null){
return obj;
}else if(Object.prototype.toString.call(obj) === "[object Object]"){
dist = {};
for (var i in obj) {
dist[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
}
}else if(Object.prototype.toString.call(obj) === "[object Array]"){
dist = [];
for (var i = 0, len = obj.length; i < len; i++ ) {
dist[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
}
}else{
dist = obj;
}
return dist;
}