-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce usage of nested
RUIProvider
(#541)
- Loading branch information
1 parent
2ce015e
commit 7f33c70
Showing
5 changed files
with
239 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { mergeDeep } from '../mergeDeep'; | ||
|
||
describe('mergeDeep', () => { | ||
it('adds new attributes', () => { | ||
const obj1 = {}; | ||
const obj2 = { | ||
props: { | ||
className: 'class', | ||
style: { | ||
color: 'white', | ||
}, | ||
}, | ||
state: { | ||
items: [1, 2], | ||
itemsSize: 2, | ||
}, | ||
}; | ||
const expectedObj = { | ||
props: { | ||
className: 'class', | ||
style: { | ||
color: 'white', | ||
}, | ||
}, | ||
state: { | ||
items: [1, 2], | ||
itemsSize: 2, | ||
}, | ||
}; | ||
|
||
expect(mergeDeep(obj1, obj2)).toEqual(expectedObj); | ||
}); | ||
|
||
it('merges with existing attributes', () => { | ||
const obj1 = { | ||
props: { | ||
children: ['child1', 'child2'], | ||
className: 'class', | ||
parent: 'parent', | ||
style: { | ||
color: 'white', | ||
}, | ||
}, | ||
state: { | ||
items: [1, 2], | ||
itemsSize: 2, | ||
}, | ||
}; | ||
const obj2 = { | ||
props: { | ||
children: null, | ||
className: 'class1 class2', | ||
style: { | ||
backgroundColor: 'black', | ||
}, | ||
}, | ||
state: { | ||
items: [3, 4, 5], | ||
itemsSize: 3, | ||
}, | ||
}; | ||
const expectedObj = { | ||
props: { | ||
children: null, | ||
className: 'class1 class2', | ||
parent: 'parent', | ||
style: { | ||
backgroundColor: 'black', | ||
color: 'white', | ||
}, | ||
}, | ||
state: { | ||
items: [3, 4, 5], | ||
itemsSize: 3, | ||
}, | ||
}; | ||
|
||
expect(mergeDeep(obj1, obj2)).toEqual(expectedObj); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const isObject = (obj) => obj && typeof obj === 'object' && !Array.isArray(obj); | ||
|
||
/** | ||
* Performs a deep merge of objects and returns new object. | ||
* | ||
* @param {...object} objects | ||
* @returns {object} | ||
*/ | ||
export const mergeDeep = (...objects) => objects.reduce((prev, obj) => { | ||
if (obj == null) { | ||
return prev; | ||
} | ||
|
||
const newObject = { ...prev }; | ||
|
||
Object.keys(obj).forEach((key) => { | ||
const previousVal = prev[key]; | ||
const currentVal = obj[key]; | ||
|
||
if (isObject(previousVal) && isObject(currentVal)) { | ||
newObject[key] = mergeDeep(previousVal, currentVal); | ||
} else { | ||
newObject[key] = currentVal; | ||
} | ||
}); | ||
|
||
return newObject; | ||
}, {}); |