-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
143 lines (138 loc) · 4.32 KB
/
tools.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
/**
* Created by tanjiasheng on 2017/5/4.
*/
const _t = {
toString(obj) {
return Object.prototype.toString.call(obj);
},
isArray(obj) {
return this.toString(obj) === '[object Array]';
},
isObject(obj) {
return this.toString(obj) === '[object Object]' && obj !== null;
},
isNumber(obj) {
return this.toString(obj) === '[object Number]';
},
isString(obj) {
return this.toString(obj) === '[object String]';
},
isFunction(obj) {
return this.toString(obj) === '[object Function]';
},
isElement(obj) {
let eleReg = /\[object HTML(?:\w+?)Element\]/,
str = this.toString(obj);
return eleReg.test(str) || str === '[object DocumentFragment]' || str === '[object Text]';
},
isVoid(obj) {
return obj === null || obj === void 0 || obj !== obj;
},
isArrayLike(obj) {
return this.isArray(obj) || (!this.isVoid(obj) && !this.isVoid(obj.length) && !this.isVoid(obj[obj.length - 1]));
},
isEmptyStr(str) {
return this.isString(str) && str == '';
},
isEmptyArr(arr) {
return this.isArray(arr) && arr.length === 0;
},
callFun(fun, ctx) {
let args = arguments,
argArr = args[2];
return fun.apply(ctx, this.isArrayLike(argArr)
? argArr
: Array.prototype.slice.call(args, 2));
},
each(arr, cb) {
if (this.isArrayLike(arr)) {
for (let i = 0; i < arr.length; i++) {
this.callFun(cb, arr[i], [arr[i], i, arr]);
}
} else if (this.isObject(arr)) {
for (let o in arr) {
if (arr.hasOwnProperty(o)) {
this.callFun(cb, arr[o], [arr[o], o]);
}
}
}
},
dMap(arr, cb) {
let resArr = new Array(arr.length);
if (this.isArrayLike(arr)) {
for (let i = 0; i < arr.length; i++) {
resArr[i] = this.callFun(cb, arr[i], [arr[i], i, arr]);
}
} else if (this.isObject(arr)) {
for (let o in arr) {
if (arr.hasOwnProperty(o)) {
resArr[o] = this.callFun(cb, arr[o], arr[o]);
}
}
}
return resArr;
},
extend(tar, obj) {
for (let p in obj) {
tar[p] = obj[p];
}
return tar;
},
http(opts = {}) {
let url = opts.url,
method = opts.method || 'get',
data = opts.data,
success = opts.success,
fail = opts.fail,
client = new XMLHttpRequest(),
responseType = opts.resType || 'text';
if (method == 'get' && !_t.isVoid(data)) {
url += /\?/.test(url) ? `&${data}` : `?${data}`;
data = null;
}
client.open(method, url, true);
client.onreadystatechange = handler;
try {
client.responseType = responseType;
} catch (err) {
client.setRequestHeader('responseType', responseType);
}
client.setRequestHeader("Content-Type", opts.contentType || "application/x-www-form-urlencoded;charset=utf-8");
client.send(data);
function handler() {
let response;
if (client.readyState !== 4) {
return;
}
if (client.status === 200) {
response = client.response;
if (responseType == 'json') {
_t.isString(response) && (response = JSON.parse(response));
}
success(response);
} else {
fail(new Error(client.statusText));
}
};
},
defProp(obj, key, getter, setter) {
return (this.isFunction(getter) && this.isFunction(setter)) ? Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
set: setter,
get: getter
}) : Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
writable: true,
value: getter
});
},
createEle(tName) {
return document.createElement(tName);
},
createTxtNode(txt) {
return document.createTextNode(txt);
}
};
export default _t;