wankdanker/node-object-mapper

Empty string changes to null

Opened this issue · 0 comments

I have the following mapper:

objectMapper({
  name: undefined
}, {
  name: [{
    key: 'user.firstName',
    default: '',
    transform: (val) => val.split(' ')[0];
  }, {
    key: 'user.lastName',
    default: '',
    transform: (val) => val.split(' ')[1];
  }]
});

The code throws an error that val is undefined, However is should be empty string ('').

After some investigation, I find out that the _mapKey function pipes the value with null:
_default = toKey.default || null and _default = toKey[2] || null;
This means that the default value for default is null, which I think is not a good idea.

'' || null === null

The result is the same if I set the default value to undefined or zero.

undefined || null === null
0 || null === null

There are scenarios where I wish to know whether a value was really undefined or null.

The only hack I could come up with, for the time being, was to use a function for default which returns empty string or undefined.

default: () => ('')
or
default: () => (undefined)
or
default: () => (0)

The suggestion is to remove || null.