1
|
|
|
import { List, fromJS } from 'immutable'; |
2
|
|
|
import { camelize } from './camelize'; |
3
|
|
|
|
4
|
|
|
export const getData = ( |
5
|
|
|
rowData = {}, columns = {}, colIndex = 0, editorValues = {} |
6
|
|
|
) => { |
7
|
|
|
|
8
|
|
|
const column = columns[colIndex]; |
9
|
|
|
|
10
|
|
|
if (!column) { |
11
|
|
|
return undefined; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
const dataIndex = column.dataIndex || null; |
15
|
|
|
|
16
|
|
|
if (!dataIndex) { |
17
|
|
|
throw new Error('No dataIndex found on column', column); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
if (editorValues && editorValues[dataIndex] !== undefined) { |
21
|
|
|
return editorValues[dataIndex]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (typeof dataIndex === 'string') { |
25
|
|
|
return rowData |
26
|
|
|
&& rowData[dataIndex] !== undefined |
27
|
|
|
? rowData[dataIndex] |
28
|
|
|
: null; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
else if (Array.isArray(dataIndex)) { |
|
|
|
|
32
|
|
|
return getValueFromDataIndexArr(rowData, dataIndex); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
}; |
36
|
|
|
|
37
|
|
|
export const setKeysInData = (data) => { |
38
|
|
|
|
39
|
|
|
if (List.isList(data)) { |
40
|
|
|
|
41
|
|
|
if (data.getIn([0, '_key'])) { |
42
|
|
|
return data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return data.map((item, i) => item.set('_key', `row-${i}`)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!data || !Array.isArray(data)) { |
49
|
|
|
return List([]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (data[0] && data[0]._key === undefined) { |
53
|
|
|
data.forEach((row, i) => { |
54
|
|
|
row._key = `row-${i}`; |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return fromJS(data); |
59
|
|
|
}; |
60
|
|
|
|
61
|
|
|
export const getRowKey = (columns, rowValues, suffix) => { |
62
|
|
|
|
63
|
|
|
const uniqueCol = columns.filter(col => col.createKeyFrom); |
64
|
|
|
let val = rowValues._key; |
65
|
|
|
|
66
|
|
|
if (uniqueCol.length > 1) { |
67
|
|
|
throw new Error('Only one column can declare createKeyFrom'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (uniqueCol.length > 0) { |
71
|
|
|
const dataIndex = nameFromDataIndex(uniqueCol[0]); |
72
|
|
|
val = rowValues && rowValues[dataIndex] |
73
|
|
|
? rowValues[dataIndex] |
74
|
|
|
: rowValues._key; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (suffix) { |
78
|
|
|
val = `${val}-${suffix}`; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return val; |
82
|
|
|
}; |
83
|
|
|
|
84
|
|
|
export const setDataAtDataIndex = (rowData = {}, dataIndex, val) => { |
85
|
|
|
|
86
|
|
|
if (typeof dataIndex === 'string') { |
87
|
|
|
rowData[dataIndex] = val; |
88
|
|
|
return rowData; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
let temp = rowData; |
92
|
|
|
|
93
|
|
|
for (let i = 0; i < dataIndex.length - 1; i++) { |
94
|
|
|
temp = temp[dataIndex[i]]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!temp[dataIndex[[dataIndex.length - 1]]]) { |
98
|
|
|
throw new Error('Invalid key path'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
temp[dataIndex[dataIndex.length - 1]] = val; |
102
|
|
|
|
103
|
|
|
return rowData; |
104
|
|
|
}; |
105
|
|
|
|
106
|
|
|
export const getValueFromDataIndexArr = (rowData, dataIndex) => { |
107
|
|
|
let temp = rowData; |
108
|
|
|
|
109
|
|
|
for (let i = 0; i < dataIndex.length; i++) { |
110
|
|
|
|
111
|
|
|
if (!temp[dataIndex[i]]) { |
112
|
|
|
// preferring silent failure on get |
113
|
|
|
// but we throw an error on the update |
114
|
|
|
// if the key path is invalid |
115
|
|
|
return ''; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
temp = temp[dataIndex[i]]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return temp; |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
export const nameFromDataIndex = (column) => { |
125
|
|
|
|
126
|
|
|
if (!column) { |
127
|
|
|
return ''; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (typeof column.dataIndex === 'string') { |
131
|
|
|
return column.dataIndex; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (Array.isArray(column.dataIndex)) { |
135
|
|
|
return column.dataIndex[column.dataIndex.length - 1]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (!column.dataIndex) { |
|
|
|
|
139
|
|
|
return camelize(column.name); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
}; |
143
|
|
|
|
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.