Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { toUpper, toLower, curry, assoc, contains, prop, compose, inc } = R;
- const filterProps = ["name", "strokeColor", "strokeWidth", "surname", "address"];
- const propsMap = {
- strokeColor: "color",
- strokeWidth: "width",
- name: ["name", "fullName"],
- surname: ["surname", "fullName"]
- };
- const transforms = {
- name: compose(toUpper, prop("name")),
- width: compose(inc, prop("strokeWidth")),
- surname: compose(toLower, prop("surname")),
- fullName: ({ name, surname }) => `${name} ${surname}`
- };
- const transformProp = curry(function transformProperties(filters, mappers, transforms, propName, ...args) {
- const keys = contains(propName, filters) ? getKeys(propName, mappers) : [];
- return keys.reduce((changes, key) => {
- const transform = prop(key, transforms) || prop(propName);
- return assoc(key, transform(...args), changes);
- }, {});
- });
- function getKeys(propName, mappers) {
- const res = prop(propName, mappers) || propName;
- return Array.isArray(res) ? res : [res];
- }
- const mapSeriosProp = transformProp(filterProps, propsMap, transforms);
- const what = {
- name: "Johh",
- surname: "Doe",
- strokeWidth: 3,
- strokeColor: "red",
- foo: "bar",
- address: "nowhere"
- };
- const chSet1 = mapSeriosProp("name", what);
- const chSet2 = mapSeriosProp("strokeWidth", what);
- const chSet3 = mapSeriosProp("strokeColor", what);
- const chSet4 = mapSeriosProp("foo", what);
- const chSet5 = mapSeriosProp("surname", what);
- const chSet6 = mapSeriosProp("address", what);
- console.log(chSet1);
- console.log(chSet2);
- console.log(chSet3);
- console.log(chSet4);
- console.log(chSet5);
- console.log(chSet6);
Add Comment
Please, Sign In to add comment