Skip to content

Instantly share code, notes, and snippets.

@benthepoet
Last active October 13, 2017 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benthepoet/3a99af75be024e91c2a6b25a7d73d5de to your computer and use it in GitHub Desktop.
Save benthepoet/3a99af75be024e91c2a6b25a7d73d5de to your computer and use it in GitHub Desktop.
ThreadItJS transform performed using Ramda
// 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