Last active
January 24, 2018 12:36
-
-
Save jonaswide/f3cc3fbf2ab4ef219605a19237c01b90 to your computer and use it in GitHub Desktop.
Currying utility for concatenating either arrays or objects to an array
This file contains hidden or 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
| export const concatCurryer = pre => cur => { | |
| const preToConcat = Array.isArray(pre) ? pre : [pre] | |
| const curToConcat = Array.isArray(cur) ? cur : [cur] | |
| return [...preToConcat, ...curToConcat] | |
| } | |
| // Examples | |
| const arr1 = [{ val: "a" }, { val: "b" }] | |
| const arr2 = [{ val: "c" }, { val: "d" }] | |
| const obj1 = { val: 1 } | |
| const obj2 = { val: 2 } | |
| concatCurryer(arr1)(arr2) // [{ val: "a" }, { val: "b" }, { val: "c" }, { val: "d" }] | |
| concatCurryer(obj1)(obj2) // [{ val: 1 }, { val: 2 }] | |
| concatCurryer(arr1)(obj1) // [{ val: "a" }, { val: "b" }, { val: 1 }] | |
| axios("http://some/api") | |
| .then(firstResult => { | |
| const firstResp = firstResult.data // { val: "first" } | |
| const concatter = concatCurryer(firstResp) | |
| axios(`http://some/other/api/${firstResp.val}`) | |
| .then(secondResult => { | |
| const secondResp = secondResult.data // { val: "second" } | |
| return concatter(secondResult.data) // [{ val: "first" }, { val: "second" }] | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment