These are chat archives for ramda/ramda

1st
Oct 2018
the repl https://ramdajs.com/repl/ doesn't work
looks like the cdn doesn't work at all https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
Nahuel Scotti
@singuerinc
Oct 01 2018 11:25
Question: I found often that I want to "evolve/map" an object changing the prop names. I think that's not not possible with evolve, is there any other function to do that?
const obj = {_id: 1};
const res = {
  id: prop('_id')
};

evolve(res, obj) // expected: {id: 1} - actual {_id: 1}
Alex Deas
@alex-deas_gitlab
Oct 01 2018 13:39

You can use a combination of prop and objOf,

const mapObj = pairs => obj => {
  var reassigned = pairs.map(([oldKey, newKey]) => R.pipe(
    R.prop(oldKey),
    R.objOf(newKey)
  )(obj))
  return R.apply(Object.assign, reassigned)
}

mapObj([
  ["_id", "id"],
  ["foo", "bar"]
])({ _id: 1, foo: 2 })

Although you’d end up with a lot of boilerplate for values which don’t need remapping

Which could be worked around like so:
  const mapObj = pairs => obj => {
  var untouched = Object.entries(obj).reduce((acc, [k, v]) => {
    const isReassigned = !!pairs.find(([oldKey]) => oldKey !== k)
    return isReassigned ? { ...acc, [k]: v } : acc
  }, {})

  var reassigned = pairs.map(([oldKey, newKey]) => R.pipe(
    R.prop(oldKey),
    R.objOf(newKey)
  )(obj))
  return R.reduce(R.merge, untouched, reassigned)
}

mapObj([
  ["_id", "id"],
])({ _id: 1, foo: 2, baz: 3 })
But at that point you can just push the reassignment up into the original map over the object and do away with using Ramda for it
Alex Deas
@alex-deas_gitlab
Oct 01 2018 13:47
Like so:
const mapObj = pairs => obj =>
  Object.entries(obj).reduce((acc, [k, v]) => {
    const found = pairs.find(([oldKey]) => oldKey === k)
    return found ? { ...acc, [found[0]]: v} : { ...acc, [k]: v }
  }, {})

mapObj([
  ["_id", "id"],
])({ _id: 1, foo: 2, baz: 3 })
Nahuel Scotti
@singuerinc
Oct 01 2018 14:02
Thanks @alex-deas_gitlab - You are right, it's a ton of code for something that you can solve with spread operator, my idea was to apply some kind of transformation like evolve does, but seems that there is no quick way of doing so
Brad Compton (he/him)
@Bradcomp
Oct 01 2018 14:20
Try applySpec
Mike Lambert
@lax4mike
Oct 01 2018 15:04
@adrice727 that's an interesting example that shows that transducers run backwards to what you might think. Here's Rich Hickey talking about that: https://www.youtube.com/watch?v=6mTbuzafcII&feature=youtu.be&t=25m28s
Aaron Rice
@adrice727
Oct 01 2018 15:32
@lax4mike Thanks. I'll take a look.
Nahuel Scotti
@singuerinc
Oct 01 2018 18:24
@alex-deas_gitlab @Bradcomp you are right!
R.applySpec({
  id: R.prop('_id'),
  name: R.prop('label')
})({
  _id: 1,
  label: 'hello'
});
// => {"id": 1, "name": "hello"}