Last active
October 13, 2017 18:22
-
-
Save benthepoet/3a99af75be024e91c2a6b25a7d73d5de to your computer and use it in GitHub Desktop.
ThreadItJS transform performed using Ramda
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
// Build the pipelines | |
const isRoot = where({ parentId: isNil }); | |
const isNotRoot = complement(isRoot); | |
const assembleLookup = (accumulator, current) => { | |
const key = current.parentId; | |
const lookup = compose(append(current), propOr([], key)); | |
return assoc(key, lookup(accumulator), accumulator); | |
}; | |
const lookupPipeline = compose(reduce(assembleLookup, {}), filter(isNotRoot)); | |
const transform = curry(data => ({ | |
lookups: lookupPipeline(data), | |
root: find(isRoot, data) | |
})); | |
// Apply the data to the pipeline | |
const input = [ | |
{ id: 1 }, | |
{ id: 2, parentId: 1 }, | |
{ id: 3, parentId: 2 }, | |
{ id: 4, parentId: 2 }, | |
{ id: 5, parentId: 3 } | |
]; | |
transform(input); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment