-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
196 lines (184 loc) 路 4.65 KB
/
Copy pathcommon.js
File metadata and controls
196 lines (184 loc) 路 4.65 KB
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
/**
* @fileOverview Utility for F2
* @author dxq613@gmail.com
*/
const DomUtil = require('./dom');
const Util = {
upperFirst: require('@antv/util/lib/string/upperFirst'),
lowerFirst: require('@antv/util/lib/string/lowerFirst'),
isString: require('@antv/util/lib/type/isString'),
isNumber: require('@antv/util/lib/type/isNumber'),
isBoolean: require('@antv/util/lib/type/isBoolean'),
isFunction: require('@antv/util/lib/type/isFunction'),
isDate: require('@antv/util/lib/type/isDate'),
isArray: require('@antv/util/lib/type/isArray'),
isNil: require('@antv/util/lib/type/isNil'),
isObject: require('@antv/util/lib/type/isObject'),
isPlainObject: require('@antv/util/lib/type/isPlainObject'),
deepMix: require('@antv/util/lib/deepMix'),
mix: require('@antv/util/lib/mix'),
each: require('@antv/util/lib/each'),
isObjectValueEqual(a, b) {
// for vue
a = Object.assign({}, a);
b = Object.assign({}, b);
const aProps = Object.getOwnPropertyNames(a);
const bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (let i = 0, len = aProps.length; i < len; i++) {
const propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
},
wrapBehavior(obj, action) {
if (obj['_wrap_' + action]) {
return obj['_wrap_' + action];
}
const method = e => {
obj[action](e);
};
obj['_wrap_' + action] = method;
return method;
},
getWrapBehavior(obj, action) {
return obj['_wrap_' + action];
},
parsePadding(padding) {
let top;
let right;
let bottom;
let left;
if (Util.isNumber(padding) || Util.isString(padding)) {
top = bottom = left = right = padding;
} else if (Util.isArray(padding)) {
top = padding[0];
right = !Util.isNil(padding[1]) ? padding[1] : padding[0];
bottom = !Util.isNil(padding[2]) ? padding[2] : padding[0];
left = !Util.isNil(padding[3]) ? padding[3] : right;
}
return [ top, right, bottom, left ];
}
};
Util.Array = {
merge(dataArray) {
let rst = [];
for (let i = 0, len = dataArray.length; i < len; i++) {
rst = rst.concat(dataArray[i]);
}
return rst;
},
values(data, name) {
const rst = [];
const tmpMap = {};
for (let i = 0, len = data.length; i < len; i++) {
const obj = data[i];
const value = obj[name];
if (!Util.isNil(value)) {
if (!Util.isArray(value)) {
if (!tmpMap[value]) {
rst.push(value);
tmpMap[value] = true;
}
} else {
Util.each(value, val => {
if (!tmpMap[val]) {
rst.push(val);
tmpMap[val] = true;
}
});
}
}
}
return rst;
},
firstValue(data, name) {
let rst = null;
for (let i = 0, len = data.length; i < len; i++) {
const obj = data[i];
const value = obj[name];
if (!Util.isNil(value)) {
if (Util.isArray(value)) {
rst = value[0];
} else {
rst = value;
}
break;
}
}
return rst;
},
group(data, fields, appendConditions = {}) {
if (!fields) {
return [ data ];
}
const groups = Util.Array.groupToMap(data, fields);
const array = [];
if (fields.length === 1 && appendConditions[fields[0]]) {
const values = appendConditions[fields[0]];
Util.each(values, value => {
value = '_' + value;
array.push(groups[value]);
});
} else {
for (const i in groups) {
array.push(groups[i]);
}
}
return array;
},
groupToMap(data, fields) {
if (!fields) {
return {
0: data
};
}
const callback = function(row) {
let unique = '_';
for (let i = 0, l = fields.length; i < l; i++) {
unique += row[fields[i]] && row[fields[i]].toString();
}
return unique;
};
const groups = {};
for (let i = 0, len = data.length; i < len; i++) {
const row = data[i];
const key = callback(row);
if (groups[key]) {
groups[key].push(row);
} else {
groups[key] = [ row ];
}
}
return groups;
},
remove(arr, obj) {
if (!arr) {
return;
}
const index = arr.indexOf(obj);
if (index !== -1) {
arr.splice(index, 1);
}
},
getRange(values) {
if (!values.length) {
return {
min: 0,
max: 0
};
}
const max = Math.max.apply(null, values);
const min = Math.min.apply(null, values);
return {
min,
max
};
}
};
Util.mix(Util, DomUtil);
module.exports = Util;