Skip to content

Instantly share code, notes, and snippets.

@emyann
Last active August 5, 2016 04:44
Show Gist options
  • Save emyann/b3fba7e6f9e105779afb30eec4921aa3 to your computer and use it in GitHub Desktop.
Save emyann/b3fba7e6f9e105779afb30eec4921aa3 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
import * as _ from 'lodash';
// 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 = DataMapper({ wantedProp:'TargerProperty', wantedProp1: 'target.property'});
// let transformedCollection = extractSomething(dataToTransform)
// or
// let transformedData = DataMapper(schema, data);
//
export function DataMapper(schema: any, items?: any): any {
let _schema = _.clone(schema);
let transformer = (items) => {
return _.chain(items)
.map((obj) => {
return _.mapValues(_schema, (path) => {
return _.get(obj, path);
});
}).value();
};
if (items === undefined) {
return (items) => {
return transformer(items);
}
} else {
return transformer(items);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment