forked from alibaba-fusion/next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
361 lines (319 loc) · 9.28 KB
/
util.js
File metadata and controls
361 lines (319 loc) · 9.28 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { Children } from 'react';
/**
* util module
*/
/**
* 是否是单选模式
* @param {string} mode
* @return {boolean} is single mode
*/
export function isSingle(mode) {
return !mode || mode === 'single';
}
/**
* 在 Select 中,认为 null 和 undefined 都是空值
* @param {*} n any object
* @return {boolean}
*/
export function isNull(n) {
return n === null || n === undefined;
}
/**
* 将字符串中的正则表达式关键字符添加转义
* @param {string} str
* @return {string}
*/
export function escapeForReg(str) {
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
}
/**
* filter by key
* @param {string} key filter key
* @param {object} item item object
* @return {boolean} it's filtered
*/
export function filter(key, item) {
const _key = escapeForReg(`${key}`);
const regExp = new RegExp(`(${_key})`, 'ig');
return regExp.test(`${item.value}`) || regExp.test(`${item.label}`);
}
/**
* loop map
* @param {Array} dataSource
* @param {function} callback
* @return {Array}
* ----
* @callback ~loopCallback
* @param {object} option
*/
export function loopMap(dataSource, callback) {
const result = [];
dataSource.forEach(option => {
if (option.children) {
const children = loopMap(option.children, callback);
result.push({
...option,
children,
});
} else {
// eslint-disable-next-line callback-return
const tmp = callback(option);
tmp && result.push(tmp);
}
});
return result;
}
/**
* Parse dataSource from MenuItem
* @static
* @param {Array<Element>} children
* @param {number} [deep=0] recursion deep level
*/
export function parseDataSourceFromChildren(children, deep = 0) {
const source = [];
Children.forEach(children, (child, index) => {
if (!child) {
return;
}
const { type, props: childProps } = child;
const item2 = { deep };
let isOption = false;
let isOptionGroup = false;
if ((typeof type === 'function' && type._typeMark === 'next_select_option') || type === 'option') {
isOption = true;
}
if ((typeof type === 'function' && type._typeMark === 'next_select_option_group') || type === 'optgroup') {
isOptionGroup = true;
}
if (!isOption && !isOptionGroup) {
return;
}
if (isOption) {
// option
// If children is a string, it can be used as value
const isStrChild = typeof childProps.children === 'string';
// value > key > string children > index
item2.value =
'value' in childProps
? childProps.value
: 'key' in childProps
? childProps.key
: isStrChild
? childProps.children
: `${index}`;
item2.label = childProps.label || childProps.children || `${item2.value}`;
if ('title' in childProps) {
item2.title = childProps.title;
}
childProps.disabled === true && (item2.disabled = true);
// You can put your extra data here, and use it in `itemRender` or `labelRender`
Object.assign(item2, childProps['data-extra'] || {});
} else if (isOptionGroup && deep < 1) {
// option group
item2.label = childProps.label || 'Group';
// parse children nodes
item2.children = parseDataSourceFromChildren(childProps.children, deep + 1);
}
source.push(item2);
});
return source;
}
/**
* Normalize dataSource
* @static
* @param {Array} dataSource
* @param {number} [deep=0] recursion deep level
* ----
* value priority: value > 'index'
* label priority: label > 'value' > 'index'
* disabled: disabled === true
*/
export function normalizeDataSource(dataSource, deep = 0, showDataSourceChildren = true) {
const source = [];
dataSource.forEach((item, index) => {
// enable array of basic type
if (/string|boolean|number/.test(typeof item) || item === null || item === undefined) {
item = { label: `${item}`, value: item };
}
// filter off addon item
if (item && item.__isAddon) {
return;
}
const item2 = { deep };
// deep < 1: only 2 level allowed
if (Array.isArray(item.children) && deep < 1 && showDataSourceChildren) {
// handle group
item2.label = item.label || item.value || `Group ${index}`;
// parse children
item2.children = normalizeDataSource(item.children, deep + 1);
} else {
const { value, label, disabled, title, ...others } = item;
// undefined 认为是没传取 index 值替代
item2.value = typeof value !== 'undefined' ? value : `${index}`;
item2.label = label || `${item2.value}`;
if ('title' in item) {
item2.title = title;
}
disabled === true && (item2.disabled = true);
Object.assign(item2, others);
}
source.push(item2);
});
return source;
}
/**
* Get flatten dataSource
* @static
* @param {Array} dataSource structured dataSource
* @return {Array}
*/
export function flattingDataSource(dataSource) {
const source = [];
dataSource.forEach(item => {
if (Array.isArray(item.children)) {
source.push(...flattingDataSource(item.children));
} else {
source.push(item);
}
});
return source;
}
export function filterDataSource(dataSource, key, filter, addonKey) {
if (!Array.isArray(dataSource)) {
return [];
}
if (typeof key === 'undefined' || key === null) {
return [].concat(dataSource);
}
let addKey = true;
const menuDataSource = loopMap(dataSource, option => {
if (key === `${option.value}`) {
addKey = false;
}
return filter(key, option) && !option.__isAddon && option;
});
// if key not in menuDataSource, add key to dataSource
if (addonKey && key && addKey) {
menuDataSource.unshift({
value: key,
label: key,
__isAddon: true,
});
}
return menuDataSource;
}
function getKeyItemByValue(value, valueMap) {
let item;
if (typeof value === 'object') {
if (value.hasOwnProperty('value')) {
item = value;
} else {
item = {
value: '',
...value,
};
}
} else {
item = valueMap[`${value}`] || {
value,
label: value,
};
}
return item;
}
/**
* compute valueDataSource by new value
* @param {Array/String} value 数据
* @param {Object} mapValueDS 上个value的缓存数据 value => {value,label} 的映射关系表
* @param {*} mapMenuDS 通过 dataSource 建立 value => {value,label} 的映射关系表
* @returns {Object} value: [value]; valueDS: [{value,label}]; mapValueDS: {value: {value,label}}
*/
export function getValueDataSource(value, mapValueDS, mapMenuDS) {
if (isNull(value)) {
return {};
}
const newValue = [];
const newValueDS = [];
const newMapValueDS = {};
const _newMapDS = Object.assign({}, mapValueDS, mapMenuDS);
if (Array.isArray(value)) {
value.forEach(v => {
const item = getKeyItemByValue(v, _newMapDS);
newValueDS.push(item);
newMapValueDS[`${item.value}`] = item;
newValue.push(item.value);
});
return {
value: newValue, // [value]
valueDS: newValueDS, // [{value,label}]
mapValueDS: newMapValueDS, // {value: {value,label}}
};
} else {
const item = getKeyItemByValue(value, _newMapDS);
return {
value: item.value,
valueDS: item,
mapValueDS: {
[`${item.value}`]: item,
},
};
}
}
/**
* Get flatten dataSource
* @static
* @param {any} value structured dataSource
* @return {String}
*/
export function valueToSelectKey(value) {
let val;
if (typeof value === 'object' && value.hasOwnProperty('value')) {
val = value.value;
} else {
val = value;
}
return `${val}`;
}
/**
* UP Down 改进双向链表方法
*/
// function DoubleLinkList(element){
// this.prev = null;
// this.next = null;
// this.element = element;
// }
//
// export function mapDoubleLinkList(dataSource){
//
// const mapDS = {};
// let doubleLink = null;
//
// let head = null;
// let tail = null;
//
// function append(element) {
// if (!doubleLink) {
// doubleLink = new DoubleLinkList(element);
// head = doubleLink;
// tail = doubleLink;
// return doubleLink;
// }
//
// const node = new DoubleLinkList(element);
// tail.next = node;
// node.prev = tail;
// tail = node;
//
// return tail;
// }
//
// dataSource.forEach((item => {
// if (item.disabled) {
// return;
// }
// mapDS[`${item.value}`] = append(item);
// }));
//
// return mapDS;
// }
//