-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
225 lines (205 loc) · 5.57 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* 公共方法
*/
/**
* traver 深度优先遍历
* @param {Object} parent 需要遍历的数据
* @param {Function} cb callback函数
*/
function traver(parent, cb) { //深度优先遍历
var child;
for (var key in parent) { //初步循环
child = parent[key];
if (child && typeof(child) === 'object') {
cb(key, child, parent);
traver(child, cb);
}
}
}
/**
* isNone 判断是否存在
* @param {String/Function} d
* @return {Boolean}
*/
function isNone(d) {
return (d === null || d === undefined || isNaN(d));
}
var root = this
/**
* extend 合并方法
* @param {Objects} 需要扩展的对象
* @return {Objects} 扩展完成的对象
*
* @example
* merge(dest, source0, [...]);
*/
function merge(dest) {
var sources = Array.prototype.slice.call(arguments, 1),
i, j, len, src;
for (j = 0, len = sources.length; j < len; j++) {
src = sources[j] || {};
for (i in src) {
if (src.hasOwnProperty(i)) {
dest[i] = src[i];
}
}
}
return dest;
}
/**
* clone 浅复制
* @param {Object} obj 被复制的对象
* @return {Object} 复制后的对象
*/
function clone(obj){
var result = Array.isArray(obj)?[]:{};
//clone
for(var k in obj){
if (obj.hasOwnProperty(k)) {
result[k] = obj[k];
}
}
return result;
}
function deepClone(src) {
var input = Array.isArray(src)?[]:{};
return deepMerge(input, src);
}
function isNeedClone(d) {
if(!d) return false;
if(root.HTMLElement && d instanceof root.HTMLElement) return false;
if(root.HTMLElement && d[0] && d[0] instanceof HTMLElement) return false;
if(d.globalCompositeOperation) return false;//ctx的情况
//还需判断div 等节点
return true;
}
function _isObject(v){
return v && typeof v === 'object' && !(v instanceof Date);
}
var maxDepth = 8;
function deepMerge(dest, src, directs, depth) {
var i, j, len, src, depth = depth || 0;
var result = clone(dest);
// var result = isDirect ? dest : clone(dest);
if (depth >= maxDepth) {
console.log('层数过深, 全部继承');
return src;
}
depth++;
//
for (i in src) {
if (src.hasOwnProperty(i)) {
var value = src[i];
var destValue = dest[i];
if(value === destValue) continue;
if(value === undefined) continue;
if (_isObject(destValue) && _isObject(value)){
if (!isNeedClone(value) || (directs && i in directs)) {
result[i] = value;
continue;
}
if (Array.isArray(destValue) !== Array.isArray(value)) { // 继承和被继承的 一个是数组 一个是对象
value = deepClone(value);
result[i] = value;
continue;
}
result[i] = deepMerge(destValue, value, directs, depth);
continue;
}
if (_isObject(value) && isNeedClone(value)) value = deepClone(value);
result[i] = value;
}
}
return result;
}
function deepMergeWithoutArray(dest, src, directs, depth) {
var i, j, len, src, depth = depth || 0;
var result = clone(dest);
if (depth >= maxDepth) {
console.log('层数过深, 全部继承');
return src;
}
depth++;
//
for (i in src) {
if (src.hasOwnProperty(i)) {
var value = src[i];
var destValue = dest[i];
if(value === destValue) continue;
if(value === undefined) continue;
if (destValue && typeof (destValue) === 'object' && typeof (value) === 'object') {
if (Array.isArray(destValue) && Array.isArray(value)) {
result[i] = value;
continue;
}
if (!isNeedClone(value) || (directs && i in directs)) {
result[i] = value;
continue;
}
if (Array.isArray(destValue) !== Array.isArray(value)) { // 继承和被继承的 一个是数组 一个是对象
value = deepClone(value);
result[i] = value;
continue;
}
result[i] = deepMergeWithoutArray(destValue, value, directs, depth);
continue;
}
if (typeof (value) === 'object' && isNeedClone(value)) value = deepClone(value);
result[i] = value;
}
}
return result;
}
/**
* switchValue 如果是非函数 返回本身 如果是函数 执行之,常用于options内部的判断
* @param {Any} f 函数或数值
* @param {Any} a 参数1
* @param {Any} b 参数2
* @param {Any} c 参数3
* @param {Any} d 参数4
* @return {Any} 返回值
*/
function switchValue(f, a, b, c, d) {
if(typeof (f) === 'function') return f(a, b, c, d);
return f;
}
var root;
if (typeof window === 'object') {
root = window;
} else {
root = {};
}
function getContainer(container) {
if (root.HTMLElement && container instanceof root.HTMLElement) return container;
if (root.HTMLElement && container[0] && container[0] instanceof root.HTMLElement) return container[0];
if (typeof container === 'string') {
if (container.charAt(0) === '.') {
var className = container.slice(1);
return container = document.getElementsByClassName(className)[0];
} else {
var id;
if (container.charAt(0) !== '#') {
id = container;
} else {
id = container.slice(1);
}
return container = document.getElementById(id);
}
if (!container) {
throw '没有container ';
}
}
return container;
}
module.exports = {
'getContainer': getContainer,
'merge': merge,
'extend': merge,
'isNone': isNone,
'traver': traver,
'deepMerge': deepMerge,
'clone': clone,
'deepClone': deepClone,
'switchValue': switchValue,
'deepMergeWithoutArray': deepMergeWithoutArray
};