-
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
0678b11
commit 1361b70
Showing
5 changed files
with
201 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: 5, | ||
}, | ||
}; | ||
const expectedObj = { | ||
props: { | ||
children: null, | ||
className: 'class1 class2', | ||
parent: 'parent', | ||
style: { | ||
backgroundColor: 'black', | ||
color: 'white', | ||
}, | ||
}, | ||
state: { | ||
items: [1, 2, 3, 4, 5], | ||
itemsSize: 5, | ||
}, | ||
}; | ||
|
||
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,30 @@ | ||
const isObject = (obj) => obj && typeof obj === 'object'; | ||
|
||
/** | ||
* 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 pVal = prev[key]; | ||
const oVal = obj[key]; | ||
|
||
if (Array.isArray(pVal) && Array.isArray(oVal)) { | ||
newObject[key] = pVal.concat(...oVal); | ||
} else if (isObject(pVal) && isObject(oVal)) { | ||
newObject[key] = mergeDeep(pVal, oVal); | ||
} else { | ||
newObject[key] = oVal; | ||
} | ||
}); | ||
|
||
return newObject; | ||
}, {}); |