Ramda: pick after applySpec picks keys with undefined value

Created on 2 Aug 2019  Â·  3Comments  Â·  Source: ramda/ramda

Hello,

According to the documentation, R.pick

Returns a partial copy of an object containing only the keys specified. If the key does not exist, the property is ignored.

However, if there is R.applySpec before — it affects its behaviour:

R.pipe(
  R.applySpec({
    foo: R.prop('foo'),
    bar: R.prop('bar'),
  }),
  R.pick(['foo', 'bar']),
  // R.pickBy(R.complement(R.isNil)), // workaround
)({ foo: 'FOO' })

// returns {"bar": undefined, "foo": "FOO"}
// expecting {"foo": "FOO"}

Here it's reproduced in REPL

Most helpful comment

undefined is such a strange value in JS. If null was the billion-dollar mistake, it is at least an understandable version of, "hey, we all know that there's no value here". undefined is billion-shrug mistake: "hey, I dunno, we can pretend it's this value if you like..."

All 3 comments

We can see what's happening here if we split up the two operations:

R.applySpec({
    foo: R.prop('foo'),
    bar: R.prop('bar'),
  })({foo: 'FOO'})

// {foo: 'FOO', bar: undefined}

R.pick(['foo', 'bar'])( {foo: 'FOO', bar: undefined} )

//The same as the input

So because applySpec actually _adds in_ the bar property, it's then picked up by pick.

If you're looking for something to remove properties if their value is undefined you could use R.reject(isNil)(obj).

`reject will remove any item matching the predicate, which should take care of your edge case here.

undefined is such a strange value in JS. If null was the billion-dollar mistake, it is at least an understandable version of, "hey, we all know that there's no value here". undefined is billion-shrug mistake: "hey, I dunno, we can pretend it's this value if you like..."

Oh, ok, I can see why I made a wrong assumption.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielFGray picture DanielFGray  Â·  3Comments

zhaoyao91 picture zhaoyao91  Â·  3Comments

FranzSkuffka picture FranzSkuffka  Â·  3Comments

DanielTimLee picture DanielTimLee  Â·  3Comments

Luxcium picture Luxcium  Â·  3Comments