Last active
October 26, 2017 21:27
-
-
Save ca057/efa4bdcf816807c5277c8a4a49040788 to your computer and use it in GitHub Desktop.
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
// @flow | |
import { compose, objOf, mergeAll, map, view, lensProp } from 'ramda'; | |
type MergeByKey = (key: string) => (Array<Object>) => Object; | |
/** | |
* Provided a key, this function returns a function which merges an array of nested objects. | |
* | |
* @example | |
* // Given two objects: | |
* const data1 = { key: { subStructure1: value1 } }; | |
* const data2 = { key: { subStructure2: value2 } }; | |
* | |
* // they can be merged together like the following: | |
* mergeByKey('key')([data1, data2]); | |
* // which results in { key: { subStructure1: value1, subStructure2: value2 }} | |
* | |
* @param {*} key | |
*/ | |
export const mergeByKey: MergeByKey = key => | |
compose( | |
// wrap result in the key | |
objOf(key), | |
// merge all objects together | |
mergeAll, | |
// extract objects from every input by the specific key | |
map(view(lensProp(key))), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment