Skip to content

Instantly share code, notes, and snippets.

@Willmo36
Created February 16, 2018 12:48
Show Gist options
  • Save Willmo36/620236e0105963b6561f41a608afe757 to your computer and use it in GitHub Desktop.
Save Willmo36/620236e0105963b6561f41a608afe757 to your computer and use it in GitHub Desktop.
Typed deep merge with Ramda
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