Created
February 16, 2018 12:48
-
-
Save Willmo36/620236e0105963b6561f41a608afe757 to your computer and use it in GitHub Desktop.
Typed deep merge with Ramda
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
type Primitive = string | number | boolean | Symbol | null | undefined; | |
const isObject = (x: any): x is Object => R.is(Object, x); | |
const isPrimitive = (x: any): x is Primitive => !isObject(x); | |
const deepMergeWith = <T>(fn: <U extends Primitive>(a: U, b: U) => U) => (a: T, b: T): T => { | |
//base case | |
if (isPrimitive(a) && isPrimitive(b)) { | |
return fn(a, b); | |
} | |
//inductive step | |
return R.mergeWith(deepMergeWith(fn), a, b); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment