Guest User

Untitled

a guest
Nov 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. // ========
  2. // Usage
  3. // ========
  4. // > const { transposeObjects } = require('./utils');
  5. // undefined
  6. // > transposeObjects([
  7. // ... { key1: 'object1key1', key2: 'object1key2' },
  8. // ... { key1: 'object2key1', key2: 'object2key2' }
  9. // ... ])
  10. // { key1: [ 'object1key1', 'object2key1' ],
  11. // key2: [ 'object1key2', 'object2key2' ] }
  12.  
  13. const R = require('ramda');
  14.  
  15. const lensPropWithDefault = R.curry((defaultValue, key) => {
  16. return R.lens(R.propOr(defaultValue, key), R.assoc(key));
  17. });
  18.  
  19. const transposeObjects = (objects) => {
  20. const appendProperty = R.curry((object, acc, key) => {
  21. return R.over(
  22. lensPropWithDefault([], key),
  23. R.append(R.prop(key, object)),
  24. acc
  25. );
  26. });
  27.  
  28. const appendProperties = (acc, object) => {
  29. return R.reduce(appendProperty(object), acc, R.keys(object));
  30. };
  31.  
  32. return R.reduce(appendProperties, {}, objects);
  33. };
Add Comment
Please, Sign In to add comment