These are chat archives for ramda/ramda

4th
Jul 2017
Julio Borja Barra
@juboba
Jul 04 2017 06:23
what do you guys use when you need to do some transformation on a value, but still need the original value in a pipe?
i.e. (something receives an object and a 'name'):
pipe(
prop('name'),
something(object)
)(object)
if my function receives two arguments I use useWith, but it's kinda ugly to use it with the same argument twice: useWith(...)(object, object)
Bijoy Thomas
@bijoythomas
Jul 04 2017 06:48
@juboba ap sounds suitable in your case
ap(equals, toLower)('X') as an example
Francisco
@franciscotln
Jul 04 2017 07:31

Hey guys. I'm trying to achieve something like this: given the task object, I want to pick some properties from it, but if the property has a value of null or undefined I want to get a default value instead.

const task = {
  id: 1,
  name: 'Task A',
  startdate: '25/04/2017',
  duedate: null,
  todosCount: undefined,
};

const pickWithDft = curryN(3, (deftAttrs, attrs, obj) =>
  attrs.reduce((prev, curr, idx) => {
    if (obj.hasOwnProperty(curr)) {
      prev[curr] = obj[curr] == null ? deftAttrs[idx] : obj[curr];
    }
    return prev;
  }, {})
);

pickWithDft(
  ['', '', 0],
  ['duedate', 'startdate', 'todosCount']
)(task);

// => { duedate: '', startdate: '25/04/2017', todosCount: 0 }

I wrote a function on my own but I wonder if you have a cleaner solution using ramda's functions instead.

Galileo Sanchez
@galileopy
Jul 04 2017 13:57
is that a game?
xD
Rick Medina
@rickmed
Jul 04 2017 15:39
@franciscotln reduce seems like a good choice, I would place defaults in a dictionary though
@ram-bot
const task = {
  id: 1,
  name: 'Task A',
  startdate: '25/04/2017',
  duedate: null,
  todosCount: undefined,
};

const defs = {
  startdate: '',
  duedate: '',
  todosCount: 0,
}

const pickWithDft = defs => (attrs, obj) =>
reduce((ac, x) => R.assoc(x, defaultTo(defs[x])(obj[x]), ac), {})(attrs)

pickWithDft(defs)(['duedate', 'startdate', 'todosCount'], task)
Julio Borja Barra
@juboba
Jul 04 2017 16:03
@bijoythomas thnx!
anybody here using ramda-fantasy?
Francisco
@franciscotln
Jul 04 2017 17:04
@rickmed yeap that's also an option :-) I just wanted the defaults to match the props array so they look the same, but an object with defaults is a good solution, thx
Rick Medina
@rickmed
Jul 04 2017 17:32
@franciscotln in object avoids getting arrays out of sync and defaults scattered through out the code base :)
Francisco
@franciscotln
Jul 04 2017 22:11
Yes, you're right @rickmed
BTW: check this out : -)
https://goo.gl/8q4VK7
it's a link to ramda's REPL
Rick Medina
@rickmed
Jul 04 2017 22:52
:thumbsup: