Last active
August 5, 2016 04:44
-
-
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
This file contains 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
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