-
Notifications
You must be signed in to change notification settings - Fork 572
/
valueUtil.tsx
92 lines (73 loc) · 2.06 KB
/
valueUtil.tsx
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
import { Key, DataIndex } from '../interface';
const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY';
function toArray<T>(arr: T | ReadonlyArray<T>): T[] {
if (arr === undefined || arr === null) {
return [];
}
// @ts-expect-error
return Array.isArray(arr) ? arr : [arr];
}
export function getPathValue<ValueType, ObjectType extends object>(
record: ObjectType,
path: DataIndex,
): ValueType {
// Skip if path is empty
if (!path && typeof path !== 'number') {
return (record as unknown) as ValueType;
}
const pathList = toArray(path);
let current: ValueType | ObjectType = record;
for (let i = 0; i < pathList.length; i += 1) {
if (!current) {
return null;
}
const prop = pathList[i];
current = current[prop];
}
return current as ValueType;
}
interface GetColumnKeyColumn {
key?: Key;
dataIndex?: DataIndex;
}
export function getColumnsKey(columns: ReadonlyArray<GetColumnKeyColumn>) {
const columnKeys: React.Key[] = [];
const keys: Record<React.Key, boolean> = {};
columns.forEach(column => {
const { key, dataIndex } = column || {};
let mergedKey = key || toArray(dataIndex).join('-') || INTERNAL_KEY_PREFIX;
while (keys[mergedKey]) {
mergedKey = `${mergedKey}_next`;
}
keys[mergedKey] = true;
columnKeys.push(mergedKey);
});
return columnKeys;
}
export function mergeObject<ReturnObject extends object>(
...objects: Partial<ReturnObject>[]
): ReturnObject {
const merged: Partial<ReturnObject> = {};
/* eslint-disable no-param-reassign */
function fillProps(obj: object, clone: object) {
if (clone) {
Object.keys(clone).forEach(key => {
const value = clone[key];
if (value && typeof value === 'object') {
obj[key] = obj[key] || {};
fillProps(obj[key], value);
} else {
obj[key] = value;
}
});
}
}
/* eslint-enable */
objects.forEach(clone => {
fillProps(merged, clone);
});
return merged as ReturnObject;
}
export function validateValue<T>(val: T) {
return val !== null && val !== undefined;
}