Skip to content

Instantly share code, notes, and snippets.

@josete89
Created November 14, 2017 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josete89/a6ba035f7a92fb3283830a0aa4929d97 to your computer and use it in GitHub Desktop.
Save josete89/a6ba035f7a92fb3283830a0aa4929d97 to your computer and use it in GitHub Desktop.
Data mapping with sanctuary
var data = {"id":1,"orderName":"Order from spain","teamName":"Portland penguins","orderType":"lock","discount":5,"contactName":"John doe","contactPhonePrefix":"+32","contactPhoneNumber":"3423232","contactEmail":"jhon.doe@mycompany.com","contactAddress":"830 Southwest","dealerId":"123","dealerNote":"Lorem ipsum","createdAt":"2017-11-13T16:40:12.000Z","updatedAt":"2017-11-13T16:40:12.000Z","products":[{"id":5,"externalId":"C77124","createdAt":"2017-11-13T16:40:12.000Z","updatedAt":"2017-11-13T16:40:12.000Z","OrderProduct":{"createdAt":"2017-11-13T16:40:12.000Z","updatedAt":"2017-11-13T16:40:12.000Z","orderId":1,"productId":5},"selectedSizes":[{"id":1,"technicalSize":520,"quantity":5,"createdAt":"2017-11-13T16:40:12.000Z","updatedAt":"2017-11-13T16:40:12.000Z","ProductSelectedSize":{"createdAt":"2017-11-13T16:40:12.000Z","updatedAt":"2017-11-13T16:40:12.000Z","productId":5,"selectedSizeId":1}}]}]}
var transformation = {"id":"id","order_name":"orderName","team_name":"teamName","order_type":"orderType","discount":"discount","contact.name":"contactName","contact.phone_number.prefix":"contactPhonePrefix","contact.phone_number.number":"contactPhoneNumber","contact.email":"contactEmail","contact.address":"contactAddress","product_list":["products",{"id":"id","product_id":"externalId","selected_sizes":["selectedSizes",{"id":"id","technical_size":"technicalSize","quantity":"quantity"}]}],"dealer_id":"dealerId","dealer_note":"dealerNote"}
function mappingStuff (data,transformation) {
let keys = R.keys(transformation)
let mapSourceToTarget = R.curry(fillObject)(data,transformation)
let value = S.reduce_((acc,val) => {
return S.chain(x =>{
let result = mapSourceToTarget(x,val)
return result
} ,acc)
},S.Right({}),keys)
return value
}
function propertyExists(object,property){
if(Object.prototype.hasOwnProperty.call(object,property)){
return S.Right(object[property])
}
return S.Left("Property not exists "+ property)
}
function fillObject(dbData,transform,acc,currentKey) {
let dbPropName = propertyExists(transform,currentKey)
let propertyForData = R.curry(propertyExists)(dbData)
let dbValue = S.chain(propertyForData,dbPropName)
return S.chain(x => {
if (S.is(Array,x)){
return handleArrayProperty(dbData,transform,acc,currentKey,x)
}
if(S.isLeft(dbValue)) {
return dbValue
} else {
let applyToNested = R.curry(setNested)(acc)(dbValue.value)
let applyToNormal = R.curry(setNormal)(acc)(dbValue.value)
let val = S.ifElse( checkIfNested , applyToNested , applyToNormal, currentKey )
return S.Right(val)
}
},dbPropName)
}
function handleArrayProperty(dbData,transform,acc,currentKey,dbPropName){
let dataArray = transform[currentKey]
let dataPropName = S.get(S.is(String), '0', dataArray).value
let dataArr = dbData[dataPropName]
let transfArr = R.drop(1,dataArray)
let transf = transfArr[0]
let res = []
let applyToNormal = R.curry(setNormal)(acc)
R.forEach(x=>{
let newKeys = R.keys(transf)
let recursiveFilling = R.curry(fillObject)(x,transf)
let value = S.reduce_((acc,val) => {
return S.chain(y =>{
let result = recursiveFilling(y,val)
return result
} ,acc)
},S.Right({}),newKeys)
res.push(value)
},dataArr)
return S.Right(applyToNormal(res,currentKey))
}
function checkIfNested(key){
return key.indexOf('.') > -1
}
function setNested(currentObject,value,key){
let keyLevels = key.split('.')
return R.assocPath(keyLevels,value,currentObject)
}
function setNormal(currentObject,value,key) {
return R.assoc(key,value,currentObject)
}
mappingStuff(data,transformation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment