Guest User

Untitled

a guest
Mar 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. const { toUpper, toLower, curry, assoc, contains, prop, compose, inc } = R;
  2.  
  3. const filterProps = ["name", "strokeColor", "strokeWidth", "surname", "address"];
  4.  
  5. const propsMap = {
  6. strokeColor: "color",
  7. strokeWidth: "width",
  8. name: ["name", "fullName"],
  9. surname: ["surname", "fullName"]
  10. };
  11.  
  12. const transforms = {
  13. name: compose(toUpper, prop("name")),
  14. width: compose(inc, prop("strokeWidth")),
  15. surname: compose(toLower, prop("surname")),
  16. fullName: ({ name, surname }) => `${name} ${surname}`
  17. };
  18.  
  19. const transformProp = curry(function transformProperties(filters, mappers, transforms, propName, ...args) {
  20. const keys = contains(propName, filters) ? getKeys(propName, mappers) : [];
  21. return keys.reduce((changes, key) => {
  22. const transform = prop(key, transforms) || prop(propName);
  23. return assoc(key, transform(...args), changes);
  24. }, {});
  25. });
  26.  
  27. function getKeys(propName, mappers) {
  28. const res = prop(propName, mappers) || propName;
  29. return Array.isArray(res) ? res : [res];
  30. }
  31.  
  32. const mapSeriosProp = transformProp(filterProps, propsMap, transforms);
  33.  
  34. const what = {
  35. name: "Johh",
  36. surname: "Doe",
  37. strokeWidth: 3,
  38. strokeColor: "red",
  39. foo: "bar",
  40. address: "nowhere"
  41. };
  42. const chSet1 = mapSeriosProp("name", what);
  43. const chSet2 = mapSeriosProp("strokeWidth", what);
  44. const chSet3 = mapSeriosProp("strokeColor", what);
  45. const chSet4 = mapSeriosProp("foo", what);
  46. const chSet5 = mapSeriosProp("surname", what);
  47. const chSet6 = mapSeriosProp("address", what);
  48. console.log(chSet1);
  49. console.log(chSet2);
  50. console.log(chSet3);
  51. console.log(chSet4);
  52. console.log(chSet5);
  53. console.log(chSet6);
Add Comment
Please, Sign In to add comment