Skip to content

Instantly share code, notes, and snippets.

@reeddunkle
Created September 9, 2019 19:49
Show Gist options
  • Select an option

  • Save reeddunkle/017914be42658568b857c2ddc0f0c594 to your computer and use it in GitHub Desktop.

Select an option

Save reeddunkle/017914be42658568b857c2ddc0f0c594 to your computer and use it in GitHub Desktop.
/*
TODO: Change API to make `paths` an array of strings | object
*/
const pickAs = (obj, paths) => {
return Object.entries(paths).reduce((accumulator, [path, definition]) => {
const resultValue = get(obj, path);
if (definition === true) {
accumulator[path] = resultValue;
} else if (isFunction(definition)) {
accumulator[definition(resultValue, path, obj)] = resultValue;
} else {
accumulator[definition] = resultValue;
}
return accumulator;
}, {});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment