Last active
December 20, 2019 11:37
-
-
Save DScheglov/d78abc77cba995bea77084e9fbe6ab31 to your computer and use it in GitHub Desktop.
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
function MapValues<K extends string, S, D>( | |
source: { [k in K]: S }, | |
mapper: (value: S, key: K, src: { [k in K]: S }) => D | |
): { [k in K]: D } { | |
const dest = {} as { [k in K]: D }; | |
for (let key of Object.keys(source) as K[]) { | |
dest[key] = mapper(source[key], key, source) | |
} | |
return dest; | |
} |
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 mapValues = (source, mapper) => Object.keys(source).reduce( | |
(dest, key) => { | |
dest[key] = mapper(source[key], key, source); | |
return dest; | |
}, | |
{} | |
); |
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
const MapValues = <K extends string, S, D>( | |
source: { [k in K]: S }, | |
mapper: (value: S, key: K, src: { [k in K]: S }) => D | |
): { [key in K]: D } => (Object.keys(source) as K[]).reduce( | |
(dest: { [k in K]: D }, key: K): { [k in K]: D } => { | |
dest[key] = mapper(source[key], key, source); | |
return dest; | |
}, | |
{} as { [key in K]: D} | |
); |
alex-malyita
commented
Dec 20, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment