-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpjs.js
147 lines (147 loc) · 3.79 KB
/
helpjs.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
"use strict";
var HelpJS = {
Array: {
select: function (array, action) {
var result = [];
for (var i = 0, length = array.length; i < length; i++) {
result.push(action(array[i]));
}
return result;
},
where: function (array, action) {
var result = [];
for (var i = 0, length = array.length; i < length; i++) {
if(action(array[i])){
result.push(array[i]);
}
}
return result;
},
sum: function (array, action) {
var sum = 0;
for (var i = 0, length = array.length; i < length; i++) {
sum += action(array[i]);
}
return sum;
},
clear: function (array) {
while (array.length) {
array.pop();
}
},
limit: function (array, limit) {
var result = [];
for (var i = 0, length = array.length; i < limit && i < length; i++) {
result.push(array[i]);
}
return result;
},
keys: function (array) {
result = [];
for (var key in Object.keys(array)) {
if (arr.hasOwnProperty(key)) {
result.push(array[i]);
}
}
return result;
}
},
Number: {
pad: function (str, max) {
str = str.toString();
return str.length < max ? HelpJS.Number.pad("0" + str, max) : str;
}
},
String: {
replaceAll: function (str, find, replace) {
return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
}
},
Http: {
Cookie: {
set: function(key,value,life){
var d = new Date();
d.setTime(d.getTime() + (life*86400000));
var expires = "expires="+d.toGMTString();
document.cookie = key + "=" + value + "; " + expires;
},
get: function(key){
var cookies = document.cookie.split(";");
for(var i = cookies.length-1; i > -1; i--) {
var cookieData = cookies[i].replace(" ","").split("=");
if(cookieData[0] === key) {
return cookieData[1];
}
}
return null;
},
remove: function(key){
var d = new Date();
d.setTime(d.getTime() + -1);
var expires = "expires="+d.toGMTString();
document.cookie = key + "=''; " + expires;
},
has: function(key){
rgx = new RegExp(/;?[ ]?([a-z]+)=/gi);
while((array = rgx.exec(document.cookie)) != null) {
if(array[1] === key)
return true;
}
return false;
},
},
methods: {
OPTIONS: "OPTIONS",
GET: "GET",
HEAD: "HEAD",
POST: "POST",
PUT: "PUT",
DELETE: "DELETE",
TRACE: "TRACE",
CONNECT: "CONNECT"
},
parametersToObject: function(url){
var result = {};
var splited = url.split("#")[0].split("?")[1].split("&");
for(var propertie in splited){
var propertie = splited[propertie];
result[propertie.split("=")[0]] = unescape(propertie.split("=")[1]);
}
return result;
},
objectToParameters: function(object){
var result = [];
for(var propertie in object){
result.push(propertie + "=" + object[propertie]);
}
return "?"+result.join("&");
},
send: function(request,sucessCallback,errorCallback){
request = {
parameters: (request.parameters?HelpJS.Http.objectToParameters(request.parameters):""),
url: request.url,
method: request.method||"GET",
async: request.async||true,
user: request.user||"",
password: request.password||"",
data: request.data,
headers: request.headers||{}
};
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4){
if(xmlhttp.status>=200 && xmlhttp.status<300){
sucessCallback(xmlhttp.responseText, xmlhttp.status);
}else {
errorCallback(xmlhttp.responseText, xmlhttp.status);
}
}
};
xmlhttp.open(request.method, request.url + request.parameters, request.async, request.user, request.password);
for(var propertie in request.headers){
xmlhttp.setRequestHeader(propertie, request.headers[propertie]);
}
(request.data)?xmlhttp.send(request.data):xmlhttp.send();
}
}
};