Skip to content

Instantly share code, notes, and snippets.

@emyann
Last active August 26, 2016 00:09
Show Gist options
  • Save emyann/668ea9746c420304b0fe08e7913d77cc to your computer and use it in GitHub Desktop.
Save emyann/668ea9746c420304b0fe08e7913d77cc to your computer and use it in GitHub Desktop.
This curried function return a function that allows to transform any collection of object towards another collection by using a translation schema
// This curried function return a function that allows to transform any collection of object towards another collection by using a translation schema
// use it like like
// let extractSomething = fieldMapper({ wantedProp:'TargetProperty', wantedProp1: 'target.property'});
// let transformedCollection = extractSomething(dataToTransform)
fieldMapper(schema) {
let _schema = _.clone(schema);
return (items) => {
return _.chain(items)
.map((obj) => {
return _.mapValues(_schema, (path) => {
return _.get(obj, path);
});
}).value();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment