Skip to content

Instantly share code, notes, and snippets.

@tjjfvi
Created November 5, 2019 16:59
Show Gist options
  • Save tjjfvi/6675e3a7fd5d284937b4b3979f722f45 to your computer and use it in GitHub Desktop.
Save tjjfvi/6675e3a7fd5d284937b4b3979f722f45 to your computer and use it in GitHub Desktop.
Object map in Flow
type ObjMapF<T> = <V:$Values<T>, K:$Keys<T>>(V, K, T)=>mixed;
type ObjMap<T:{}, F:ObjMapF<T>> = $ObjMapi<T, <K, V>(K, V)=>$Call<F, V, K, T>> ;
const objMap = <T:{}, F:ObjMapF<T>=*>(o: T, f: F): ObjMap<T, F> => {
let keys: Array<$Keys<T>> = Object.keys(o);
let obj = keys.map(k => {
let v = o[k];
type K = typeof k;
type V = typeof v;
let r = f<K, V>(o[k], k, o)
return r;
}).reduce((a, b) => Object.assign({}, a, b));
// $FlowFixMe
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment