Created
November 5, 2019 16:59
-
-
Save tjjfvi/6675e3a7fd5d284937b4b3979f722f45 to your computer and use it in GitHub Desktop.
Object map in Flow
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 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