Created
April 19, 2018 14:49
-
-
Save titouancreach/391448ca159f990b88c402a5a5f6dc0a to your computer and use it in GitHub Desktop.
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
const state = { | |
dataset: [{ | |
name: 'dataset1', | |
template: 0 | |
}, { | |
name: 'dataset2', | |
template: 1 | |
}], | |
template: { | |
0: { | |
name: 'template1' | |
}, | |
1: { | |
name: 'template2', | |
properties: [0, 1, 2] | |
} | |
}, | |
property: { | |
0: { | |
name: 'Prop1' | |
}, | |
1: { | |
name: 'Prop2' | |
}, | |
2: { | |
name: 'Prop3' | |
} | |
} | |
}; | |
const denormalizeDataset = state => next => dataset => { | |
return next(dataset); | |
} | |
const denormalizeTemplate = state => next => templateId => { | |
const template = state.template[templateId]; | |
return next(template); | |
} | |
const denormalizeProperties = state => next => properties => { | |
return properties.map(id => { | |
const property = state.property[id]; | |
return next(property); | |
}) | |
} | |
const getFnMap = state => ({ | |
template: denormalizeTemplate(state), | |
dataset: denormalizeDataset(state), | |
properties: denormalizeProperties(state) | |
}); | |
const functionReturnEnhancer = f => { | |
return (...args) => { | |
const x = f(...args) | |
console.log('LOG', x); | |
return x; | |
} | |
}; | |
const denormalize = fnMap => { | |
const f = node => { | |
return Object.entries(node).map(([k, v]) => { | |
if (k in fnMap) { | |
return [k, fnMap[k](f)(v)] | |
} | |
return [k, v] | |
}).reduce((acc, [k, v]) => ({...acc, [k]: v}), {}) | |
} | |
return f; | |
}; | |
console.log(denormalize(getFnMap(state))(state.dataset[0])); | |
console.log(denormalize(getFnMap(state))(state.dataset[1])); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment