forked from react-component/util
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeProps.ts
More file actions
33 lines (32 loc) · 961 Bytes
/
mergeProps.ts
File metadata and controls
33 lines (32 loc) · 961 Bytes
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
/**
* Merges multiple props objects into one. Unlike `Object.assign()` or `{ ...a, ...b }`, it skips
* properties whose value is explicitly set to `undefined`.
*
* @example
* ```ts
* const { a, b } = mergeProps(defaults, config, props);
* ```
*/
function mergeProps<A, B>(a: A, b: B): B & A;
function mergeProps<A, B, C>(a: A, b: B, c: C): C & B & A;
function mergeProps<A, B, C, D>(a: A, b: B, c: C, d: D): D & C & B & A;
function mergeProps(...items: any[]) {
const ret: any = {};
for (const item of items) {
if (item) {
for (const key of Object.keys(item)) {
if (item[key] !== undefined) {
if (key === 'className') {
ret[key] = `${ret[key] || ''} ${item[key] || ''}`.trim();
} else if (key === 'style') {
ret[key] = { ...ret[key], ...item[key] };
} else {
ret[key] = item[key];
}
}
}
}
}
return ret;
}
export default mergeProps;