-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathutils.js
More file actions
154 lines (140 loc) · 3.62 KB
/
utils.js
File metadata and controls
154 lines (140 loc) · 3.62 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
import hoistStatics from 'hoist-non-react-statics';
import warning from 'warning';
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'WrappedComponent';
}
export function argumentContainer(Container, WrappedComponent) {
/* eslint no-param-reassign:0 */
Container.displayName = `Form(${getDisplayName(WrappedComponent)})`;
Container.WrappedComponent = WrappedComponent;
return hoistStatics(Container, WrappedComponent);
}
export function identity(obj) {
return obj;
}
export function flattenArray(arr) {
return Array.prototype.concat.apply([], arr);
}
export function treeTraverse(path = '', tree, isLeafNode, errorMessage, callback) {
if (isLeafNode(path, tree)) {
callback(path, tree);
} else if (tree === undefined || tree === null) {
// Do nothing
} else if (Array.isArray(tree)) {
tree.forEach((subTree, index) => treeTraverse(
`${path}[${index}]`,
subTree,
isLeafNode,
errorMessage,
callback
));
} else { // It's object and not a leaf node
if (typeof tree !== 'object') {
warning(false, errorMessage);
return;
}
Object.keys(tree).forEach(subTreeKey => {
const subTree = tree[subTreeKey];
treeTraverse(
`${path}${path ? '.' : ''}${subTreeKey}`,
subTree,
isLeafNode,
errorMessage,
callback
);
});
}
}
export function flattenFields(maybeNestedFields, isLeafNode, errorMessage) {
const fields = {};
treeTraverse(undefined, maybeNestedFields, isLeafNode, errorMessage, (path, node) => {
fields[path] = node;
});
return fields;
}
export function normalizeValidateRules(validate, rules, validateTrigger) {
const validateRules = validate.map((item) => {
const newItem = {
...item,
trigger: item.trigger || [],
};
if (typeof newItem.trigger === 'string') {
newItem.trigger = [newItem.trigger];
}
return newItem;
});
if (rules) {
validateRules.push({
trigger: validateTrigger ? [].concat(validateTrigger) : [],
rules,
});
}
return validateRules;
}
export function getValidateTriggers(validateRules) {
return validateRules
.filter(item => !!item.rules && item.rules.length)
.map(item => item.trigger)
.reduce((pre, curr) => pre.concat(curr), []);
}
export function getValueFromEvent(e) {
// To support custom element
if (!e || !e.target) {
return e;
}
const { target } = e;
return target.type === 'checkbox' || target.type === 'radio' ? target.checked : target.value;
}
export function getErrorStrs(errors) {
if (errors) {
return errors.map((e) => {
if (e && e.message) {
return e.message;
}
return e;
});
}
return errors;
}
export function getParams(ns, opt, cb) {
let names = ns;
let options = opt;
let callback = cb;
if (cb === undefined) {
if (typeof names === 'function') {
callback = names;
options = {};
names = undefined;
} else if (Array.isArray(names)) {
if (typeof options === 'function') {
callback = options;
options = {};
} else {
options = options || {};
}
} else {
callback = options;
options = names || {};
names = undefined;
}
}
return {
names,
options,
callback,
};
}
export function isEmptyObject(obj) {
return Object.keys(obj).length === 0;
}
export function hasRules(validate) {
if (validate) {
return validate.some((item) => {
return item.rules && item.rules.length;
});
}
return false;
}
export function startsWith(str, prefix) {
return str.lastIndexOf(prefix, 0) === 0;
}